-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.cpp
197 lines (169 loc) · 4.67 KB
/
wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "MMKV.h"
#include <vector>
#include <string>
// C ABI
extern "C" MMKV *get_mmkv_instance(MMKVMode mode, const char *cryptKey)
{
std::string *tmp = nullptr;
if (cryptKey != nullptr)
{
tmp = new std::string(cryptKey);
}
auto mmkv = MMKV::defaultMMKV(mode, tmp);
if (tmp != nullptr)
{
delete tmp;
tmp = nullptr;
}
return mmkv;
}
extern "C" MMKV *get_mmkv_instance_with_id(const char *mmap_id, MMKVMode mode, const char *cryptKey)
{
std::string id(mmap_id);
std::string *tmp = nullptr;
if (cryptKey != nullptr)
{
tmp = new std::string(cryptKey);
}
auto mmkv = MMKV::mmkvWithID(id, mode, tmp);
if (tmp != nullptr)
{
delete tmp;
tmp = nullptr;
}
return mmkv;
}
extern "C" void init_mmkv(const char *dir, MMKVLogLevel logLevel, mmkv::LogHandler handler)
{
std::string tmp(dir);
MMKV::initializeMMKV(tmp, logLevel, handler);
}
extern "C" const char *get_mmap_id(MMKV *mmkv)
{
std::string result = mmkv->mmapID();
char *resultCStr = new char[result.size() + 1];
std::strcpy(resultCStr, result.c_str());
return resultCStr;
}
extern "C" void set_bool(MMKV *mmkv, bool v, const char *k)
{
std::string tmp(k);
mmkv->set(v, tmp);
}
extern "C" bool get_bool(MMKV *mmkv, const char *k)
{
std::string tmp(k);
return mmkv->getBool(tmp);
}
extern "C" void set_string(MMKV *mmkv, const char *v, const char *k)
{
std::string value(v);
std::string key(k);
mmkv->set(value, key);
}
extern "C" const char *get_string(MMKV *mmkv, const char *k)
{
std::string tmp(k);
std::string result;
mmkv->getString(tmp, result);
char *resultCStr = new char[result.size() + 1];
std::strcpy(resultCStr, result.c_str());
return resultCStr;
}
extern "C" void enable_auto_key_expire(MMKV *mmkv, uint32_t expire) { mmkv->enableAutoKeyExpire(expire); }
extern "C" void set_double(MMKV *mmkv, double v, const char *k)
{
std::string tmp(k);
mmkv->set(v, tmp);
}
extern "C" double get_double(MMKV *mmkv, const char *k)
{
std::string tmp(k);
return mmkv->getDouble(tmp);
}
extern "C" void set_string_list(MMKV *mmkv, const char **vec, size_t length, const char *key)
{
std::vector<std::string> v(vec, vec + length);
std::string tmp(key);
mmkv->set(v, tmp);
}
extern "C" const char **get_string_list(MMKV *mmkv, size_t *length, const char *key)
{
std::vector<std::string> v;
std::string tmp(key);
mmkv->getVector(tmp, v);
static std::vector<const char *> c_strings(v.size());
for (size_t i = 0; i < v.size(); ++i)
{
c_strings[i] = v[i].c_str();
}
*length = v.size();
return c_strings.data();
}
extern "C" void remove_value_for_key(MMKV *mmkv, const char *key)
{
std::string tmp(key);
mmkv->removeValueForKey(tmp);
}
extern "C" void remove_values_for_keys(MMKV *mmkv, const char **vec, size_t length)
{
std::vector<std::string> v(vec, vec + length);
mmkv->removeValuesForKeys(v);
}
extern "C" bool contains_key(MMKV *mmkv, const char *key)
{
std::string tmp(key);
return mmkv->containsKey(tmp);
}
extern "C" const char **all_keys(MMKV *mmkv, size_t *length)
{
auto v = mmkv->allKeys();
static std::vector<const char *> c_strings(v.size());
for (size_t i = 0; i < v.size(); ++i)
{
c_strings[i] = v[i].c_str();
}
*length = v.size();
return c_strings.data();
}
extern "C" size_t get_actual_size(MMKV *mmkv) { return mmkv->actualSize(); }
extern "C" size_t get_total_size(MMKV *mmkv) { return mmkv->totalSize(); }
extern "C" size_t get_value_size(MMKV *mmkv, const char *key, bool actual)
{
std::string tmp(key);
return mmkv->getValueSize(tmp, actual);
}
extern "C" void clear_all(MMKV *mmkv, bool keep_space) { mmkv->clearAll(keep_space); }
extern "C" void clear_memory_cache(MMKV *mmkv, bool keep_space) { mmkv->clearMemoryCache(keep_space); }
extern "C" size_t count(MMKV *mmkv, bool filter_expire) { return mmkv->count(filter_expire); }
extern "C" size_t back_up(const char *root_dir, const char *mmap_id)
{
std::string dir(root_dir);
if (mmap_id == nullptr)
{
return MMKV::backupAllToDirectory(dir);
}
else
{
std::string id(mmap_id);
return MMKV::backupOneToDirectory(id, dir);
}
}
extern "C" size_t restore(const char *root_dir, const char *mmap_id)
{
std::string dir(root_dir);
if (mmap_id == nullptr)
{
return MMKV::restoreAllFromDirectory(dir);
}
else
{
std::string id(mmap_id);
return MMKV::restoreOneFromDirectory(id, dir);
}
}
extern "C" void remove_storage(const char *mmap_id)
{
std::string tmp(mmap_id);
MMKV::removeStorage(tmp);
}