libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
interface.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5
6#include "libxr_def.hpp"
7#include "libxr_mem.hpp"
8#include "libxr_type.hpp"
9
10namespace LibXR
11{
12
17static constexpr uint16_t LIBXR_DATABASE_VERSION = 3;
18
24{
25 public:
30 class KeyBase
31 {
32 public:
33 const char* name_;
35
42 KeyBase(const char* name, RawData raw_data) : name_(name), raw_data_(raw_data) {}
43 };
44
50 template <typename Data>
51 class Key : public KeyBase
52 {
53 public:
54 Data data_;
56
69 Key(Database& database, const char* name, Data init_value)
70 : KeyBase(name, RawData(data_)), database_(database)
71 {
72 ErrorCode status = database.Get(*this);
73 if (status != ErrorCode::OK)
74 {
75 data_ = init_value;
76 if (status == ErrorCode::NOT_FOUND)
77 {
78 [[maybe_unused]] const auto add_result = database.Add(*this);
79 REQUIRE(add_result == ErrorCode::OK);
80 }
81 }
82 }
83
95 Key(Database& database, const char* name)
96 : KeyBase(name, RawData(data_)), database_(database)
97 {
98 ErrorCode status = database.Get(*this);
99 if (status != ErrorCode::OK)
100 {
101 Memory::FastSet(&data_, 0, sizeof(Data));
102 if (status == ErrorCode::NOT_FOUND)
103 {
104 [[maybe_unused]] const auto add_result = database.Add(*this);
105 REQUIRE(add_result == ErrorCode::OK);
106 }
107 }
108 }
109
115 Key(const Key& other) = delete;
116
122 Key(Key&& other) = delete;
123
130 Key& operator=(const Key& other) = delete;
131
138 Key& operator=(Key&& other) = delete;
139
145 operator Data() { return data_; }
146
152 ErrorCode Save() { return database_.Set(*this, this->raw_data_); }
153
160 ErrorCode Set(Data data)
161 {
162 data_ = data;
163 return Save();
164 }
165
171 ErrorCode Load() { return database_.Get(*this); }
172
179 ErrorCode operator=(Data data) { return Set(data); } // NOLINT
180 };
181
182 private:
189 virtual ErrorCode Get(KeyBase& key) = 0;
190
198 virtual ErrorCode Set(KeyBase& key, RawData data) = 0;
199
206 virtual ErrorCode Add(KeyBase& key) = 0;
207};
208
209} // namespace LibXR
键的基类,存储键名及其数据 Base class for keys, storing key name and associated data.
Definition interface.hpp:31
RawData raw_data_
原始数据 / Raw data associated with the key.
Definition interface.hpp:34
KeyBase(const char *name, RawData raw_data)
构造函数,初始化键名和原始数据 Constructor to initialize key name and raw data.
Definition interface.hpp:42
const char * name_
键名 / Key name.
Definition interface.hpp:33
模板类,表示数据库中的具体键 Template class representing a specific key in the database.
Definition interface.hpp:52
Database & database_
关联的数据库对象 / Reference to the associated database.
Definition interface.hpp:55
ErrorCode Save()
保存当前键值到数据库 Save the current key value to the database.
ErrorCode Set(Data data)
设置键的值并更新数据库 Set the key's value and update the database.
Key & operator=(const Key &other)=delete
禁止拷贝赋值数据库键对象 Copy assignment is disabled for database keys.
Key(Database &database, const char *name, Data init_value)
构造函数,初始化键并从数据库加载数据 Constructor to initialize key and load data from the database.
Definition interface.hpp:69
Data data_
键存储的数据 / The data stored in the key.
Definition interface.hpp:54
ErrorCode operator=(Data data)
赋值运算符,设置键的值 Assignment operator to set the key's value.
Key(Key &&other)=delete
禁止移动数据库键对象 Move construction is disabled for database keys.
Key & operator=(Key &&other)=delete
禁止移动赋值数据库键对象 Move assignment is disabled for database keys.
ErrorCode Load()
从数据库加载键的值 Load the key's value from the database.
Key(const Key &other)=delete
禁止拷贝数据库键对象 Copy construction is disabled for database keys.
Key(Database &database, const char *name)
构造函数,初始化键,并在数据库不存在时赋默认值 Constructor to initialize key, assigning default value if not found in the da...
Definition interface.hpp:95
数据库接口,提供键值存储和管理功能 Database interface providing key-value storage and management.
Definition interface.hpp:24
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
可写原始数据视图 / Mutable raw data view
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ NOT_FOUND
未找到 / Not found
@ OK
操作成功 / Operation successful
static constexpr uint16_t LIBXR_DATABASE_VERSION
数据库存储格式版本号 Database storage-format version.
Definition interface.hpp:17