Key_handle的学习

代码

一切尽在不言中

#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.csdn.net/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.csdn.net/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.csdn.net/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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值