代码
一切尽在不言中
#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