libxr  1.0
Want to be the best embedded framework
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
68 Key(Database& database, const char* name, Data init_value)
69 : KeyBase(name, RawData(data_)), database_(database)
70 {
71 ErrorCode status = database.Get(*this);
72 if (status != ErrorCode::OK)
73 {
74 data_ = init_value;
75 if (status == ErrorCode::NOT_FOUND)
76 {
77 REQUIRE(database.Add(*this) == ErrorCode::OK);
78 }
79 }
80 }
81
93 Key(Database& database, const char* name)
94 : KeyBase(name, RawData(data_)), database_(database)
95 {
96 ErrorCode status = database.Get(*this);
97 if (status != ErrorCode::OK)
98 {
99 Memory::FastSet(&data_, 0, sizeof(Data));
100 if (status == ErrorCode::NOT_FOUND)
101 {
102 REQUIRE(database.Add(*this) == ErrorCode::OK);
103 }
104 }
105 }
106
112 Key(const Key& other) = delete;
113
119 Key(Key&& other) = delete;
120
127 Key& operator=(const Key& other) = delete;
128
135 Key& operator=(Key&& other) = delete;
136
142 operator Data() { return data_; }
143
149 ErrorCode Save() { return database_.Set(*this, this->raw_data_); }
150
157 ErrorCode Set(Data data)
158 {
159 data_ = data;
160 return Save();
161 }
162
168 ErrorCode Load() { return database_.Get(*this); }
169
176 ErrorCode operator=(Data data) { return Set(data); } // NOLINT
177 };
178
179 private:
186 virtual ErrorCode Get(KeyBase& key) = 0;
187
195 virtual ErrorCode Set(KeyBase& key, RawData data) = 0;
196
203 virtual ErrorCode Add(KeyBase& key) = 0;
204};
205
206} // 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:68
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 d...
Definition interface.hpp:93
数据库接口,提供键值存储和管理功能 (Database interface providing key-value storage and management).
Definition interface.hpp:24
virtual ErrorCode Add(KeyBase &key)=0
添加新键到数据库 (Add a new key to the database).
virtual ErrorCode Get(KeyBase &key)=0
从数据库获取键的值 (Retrieve the key's value from the database).
virtual ErrorCode Set(KeyBase &key, RawData data)=0
设置数据库中的键值 (Set the key's value in the database).
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
可写原始数据视图 / Mutable raw data view
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ NOT_FOUND
未找到 | Not found
@ OK
操作成功 | Operation successful
static constexpr uint16_t LIBXR_DATABASE_VERSION
数据库存储格式版本号 (Database storage-format version).
Definition interface.hpp:17