Key_handle的学习

这段代码展示了一个名为KeyHandle的C++类,它使用模板和内存管理技巧来处理内部数据。KeyHandle类包含了构造函数、析构函数以及赋值操作符的删除,以防止不必要的复制。同时,它提供了用于创建空实例的方法make(),以及通过模板cast()函数获取内部数据指针。此外,还实现了将对象转换为C风格和64位整型句柄以便外部访问的方法。

代码

一切尽在不言中

#pragma once

#include "common/common.h"
#include "sdf/sdf.h"

#include <memory>

namespace sdf {
    namespace algorithm {

        class KeyHandle {
        public:
            using erased_internal_data_t = char; //使用erased_internal_data_t等效于char,此处用法相当于typedef

            KeyHandle() = default;//构造函数,使用默认参数
            virtual ~KeyHandle() = default;//析构函数,使用默认参数

            KeyHandle(const KeyHandle &) = delete;//赋值构造函数
			//参考链接 https://blog.youkuaiyun.com/TanJiaLiang_/article/details/86691437?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.compare&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.compare
            KeyHandle &operator=(const KeyHandle &) = delete;//复制赋值
			//https://zh.cppreference.com/w/cpp/language/operators

            /**
             * @brief construct an empty key handle  //摘要
             *
             * @tparam T internal type               //参数
             * @return unique_ptr to KeyHandle which contains empty internal data
             */
            template <typename T> static std::unique_ptr<KeyHandle> make() {
				//template <typename T> 模板类
				//static std::unique_ptr https://mp.youkuaiyun.com/console/editor/html/109365196
                auto key_handle = std::make_unique<KeyHandle>();
				//std::make_unique https://zh.cppreference.com/w/cpp/memory/unique_ptr/make_unique
                // erase internal type
                key_handle->data.reset(reinterpret_cast<char *>(new T()),
                                       internal_data_deleter<T>);
				//reinterpret_cast  https://blog.youkuaiyun.com/iflyme/article/details/83378697
				//data.reset data属于std::shared_ptr<erased_internal_data_t>类型  https://zh.cppreference.com/w/cpp/memory/shared_ptr/reset
                key_handle->data_length = sizeof(T);
                return key_handle;
            }

            /**
             * @brief cast to internal type for accessing raw key data
             *
             * @tparam T internal type
             * @return pointer to internal data
             */
            template <typename T> T *cast() { return reinterpret_cast<T *>(data.get()); }

            /**
             * @brief cast to internal type for accessing raw key data
             *
             * @tparam T internal type
             * @return const pointer to internal data
             */
            template <typename T> const T *cast() const {
                return reinterpret_cast<const T *>(data.get());
            }

            /**
             * @brief convert to native handle representation for public c style apis
             *
             * @return sdf_handle_t type pointer to this
             */
            sdf_handle_t native_handle() const {
                return reinterpret_cast<sdf_handle_t>(const_cast<KeyHandle *>(this));
            }

            /**
             * @brief convert to native 64-bits representation for grpc conversions
             *
             * @return uint64_t type number equals to address of this
             */
            uint64_t native_handle_64ull() const {
                return reinterpret_cast<uint64_t>(this);
            }

            /**
             * @brief access key handle via native handle
             *
             * @param native_handle public c style handle
             * @return pointer to KeyHandle
             */
            static KeyHandle *from_native_handle(sdf_handle_t native_handle) {
                return reinterpret_cast<KeyHandle *>(native_handle);
            }

            /**
             * @brief access key handle via native 64-bits representation
             *
             * @param native_handle_64ull native 64-bits representation
             * @return pointer to KeyHandle
             */
            static KeyHandle *from_native_handle_64ull(uint64_t native_handle_64ull) {
                return reinterpret_cast<KeyHandle *>(native_handle_64ull);
            }

        private:
            template <typename T>
            static void internal_data_deleter(const erased_internal_data_t *ptr) {
                delete reinterpret_cast<const T *>(ptr);
            }

        protected:
            std::shared_ptr<erased_internal_data_t> data; ///< internal data
            size_t data_length = 0;                       ///< length of internal data
        };

    } // namespace algorithm
} // namespace sdf

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值