Key_handle的学习

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

一切尽在不言中

#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

 

### ESP32 NVS 学习教程 #### 非易失性存储(NVS)简介 非易失性存储(NVS)允许应用程序以键值对形式保存数据到闪存中,即使设备断电后也能保持这些数据不变。对于ESP32来说,这是一项非常有用的功能,可以用来储存配置参数或其他重要信息[^1]。 #### 初始化NVS分区 为了能够读取和写入NVS中的数据,在操作之前需要先初始化相应的分区: ```c #include "nvs_flash.h" void app_main(void){ // 初始化默认的NVS命名空间 esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { // 如果NVS尚未被初始化,则擦除并重新初始化 ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); } } ``` 这段代码展示了如何安全地初始化NVS分区,处理可能遇到的一些错误情况,比如当没有可用页面或者发现了新的版本时会执行擦除再重试的操作[^4]。 #### 向NVS中写入整数型数据 下面是一个简单的例子来展示怎样把一个`int32_t`类型的数值储存在指定的名字空间下,并赋予特定的关键字名称: ```c #include "nvs.h" #include "nvs_flash.h" static void write_data_to_nvs(const char *namespace_name, const char *key, int32_t value){ nvs_handle my_handle; esp_err_t err; // 打开给定名字的空间以便于写入 err = nvs_open(namespace_name, NVS_READWRITE, &my_handle); if (err != ESP_OK) return; // 将value作为名为key的数据项写入打开的手柄所指向的位置 err = nvs_set_i32(my_handle, key, value); // 提交更改至flash nvs_commit(my_handle); // 关闭当前使用的句柄 nvs_close(my_handle); } ``` 此函数接收三个参数:要访问的名字空间字符串、代表目标变量名的字符串以及待存储的实际数值。通过调用`nvs_open()`获取对应区域的一个手柄对象;接着利用该手柄配合`nvs_set_i32()`完成实际赋值工作;最后记得关闭这个连接以防资源泄露[^3]。 #### 从NVS中读取整数型数据 同样地,这里也给出了一段示范性的C语言程序片段用于提取先前已经存在的记录: ```c static bool read_data_from_nvs(const char *namespace_name, const char *key, int32_t *out_value){ nvs_handle my_handle; esp_err_t err; // 获取name_space对应的handle err = nvs_open(namespace_name, NVS_READONLY, &my_handle); if (err != ESP_OK) return false; // 查询是否存在key对应的值 err = nvs_get_i32(my_handle, key, out_value); // 不管成功与否都要释放掉handel nvs_close(my_handle); return err == ESP_OK ? true : false; } ``` 上述方法接受两个输入参数——所属的名字空间与查询关键字,还有一个指针形参用来返回找到的结果。如果一切顺利的话将会更新传入的指针位置处的内容为匹配上的那个整数值;反之则不会改变它原本的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值