1313
1414namespace margelo ::nitro::mmkv {
1515
16- HybridMMKV::HybridMMKV (const Configuration& config) : HybridObject(TAG) {
16+ HybridMMKV::HybridMMKV (const Configuration& config) : _config(config), HybridObject(TAG) {
1717 std::string path = config.path .has_value () ? config.path .value () : " " ;
1818 std::string encryptionKey = config.encryptionKey .has_value () ? config.encryptionKey .value () : " " ;
1919 bool hasEncryptionKey = encryptionKey.size () > 0 ;
@@ -29,12 +29,12 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
2929 }
3030
3131#ifdef __APPLE__
32- instance = MMKV::mmkvWithID (config.id , mode, encryptionKeyPtr, pathPtr);
32+ _instance = MMKV::mmkvWithID (config.id , mode, encryptionKeyPtr, pathPtr);
3333#else
34- instance = MMKV::mmkvWithID (config.id , DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
34+ _instance = MMKV::mmkvWithID (config.id , DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
3535#endif
3636
37- if (instance == nullptr ) [[unlikely]] {
37+ if (_instance == nullptr ) [[unlikely]] {
3838 // Check if instanceId is invalid
3939 if (config.id .empty ()) [[unlikely]] {
4040 throw std::runtime_error (" Failed to create MMKV instance! `id` cannot be empty!" );
@@ -55,11 +55,14 @@ HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
5555 }
5656}
5757
58+ Configuration HybridMMKV::getConfig () {
59+ return _config;
60+ }
5861double HybridMMKV::getSize () {
59- return instance ->actualSize ();
62+ return _instance ->actualSize ();
6063}
6164bool HybridMMKV::getIsReadOnly () {
62- return instance ->isReadOnly ();
65+ return _instance ->isReadOnly ();
6366}
6467
6568// helper: overload pattern matching for lambdas
@@ -78,32 +81,32 @@ void HybridMMKV::set(const std::string& key, const std::variant<bool, std::share
7881 // Pattern-match each potential value in std::variant
7982 bool didSet = std::visit (overloaded{[&](bool b) {
8083 // boolean
81- return instance ->set (b, key);
84+ return _instance ->set (b, key);
8285 },
8386 [&](const std::shared_ptr<ArrayBuffer>& buf) {
8487 // ArrayBuffer
8588 MMBuffer buffer (buf->data (), buf->size (), MMBufferCopyFlag::MMBufferNoCopy);
86- return instance ->set (std::move (buffer), key);
89+ return _instance ->set (std::move (buffer), key);
8790 },
8891 [&](const std::string& string) {
8992 // string
90- return instance ->set (string, key);
93+ return _instance ->set (string, key);
9194 },
9295 [&](double number) {
9396 // number
94- return instance ->set (number, key);
97+ return _instance ->set (number, key);
9598 }},
9699 value);
97100 if (!didSet) {
98101 throw std::runtime_error (" Failed to set value for key \" " + key + " \" !" );
99102 }
100103
101104 // Notify on changed
102- MMKVValueChangedListenerRegistry::notifyOnValueChanged (instance ->mmapID (), key);
105+ MMKVValueChangedListenerRegistry::notifyOnValueChanged (_instance ->mmapID (), key);
103106}
104107std::optional<bool > HybridMMKV::getBoolean (const std::string& key) {
105108 bool hasValue;
106- bool result = instance ->getBool (key, /* defaultValue */ false , &hasValue);
109+ bool result = _instance ->getBool (key, /* defaultValue */ false , &hasValue);
107110 if (hasValue) {
108111 return result;
109112 } else {
@@ -112,7 +115,7 @@ std::optional<bool> HybridMMKV::getBoolean(const std::string& key) {
112115}
113116std::optional<std::string> HybridMMKV::getString (const std::string& key) {
114117 std::string result;
115- bool hasValue = instance ->getString (key, result, /* inplaceModification */ true );
118+ bool hasValue = _instance ->getString (key, result, /* inplaceModification */ true );
116119 if (hasValue) {
117120 return result;
118121 } else {
@@ -121,7 +124,7 @@ std::optional<std::string> HybridMMKV::getString(const std::string& key) {
121124}
122125std::optional<double > HybridMMKV::getNumber (const std::string& key) {
123126 bool hasValue;
124- double result = instance ->getDouble (key, /* defaultValue */ 0.0 , &hasValue);
127+ double result = _instance ->getDouble (key, /* defaultValue */ 0.0 , &hasValue);
125128 if (hasValue) {
126129 return result;
127130 } else {
@@ -132,11 +135,11 @@ std::optional<std::shared_ptr<ArrayBuffer>> HybridMMKV::getBuffer(const std::str
132135 MMBuffer result;
133136#ifdef __APPLE__
134137 // iOS: Convert std::string to NSString* for MMKVCore pod compatibility
135- bool hasValue = instance ->getBytes (@(key.c_str ()), result);
138+ bool hasValue = _instance ->getBytes (@(key.c_str ()), result);
136139#else
137140 // Android/other platforms: Use std::string directly (converts to
138141 // std::string_view)
139- bool hasValue = instance ->getBytes (key, result);
142+ bool hasValue = _instance ->getBytes (key, result);
140143#endif
141144 if (hasValue) {
142145 return std::make_shared<ManagedMMBuffer>(std::move (result));
@@ -145,49 +148,49 @@ std::optional<std::shared_ptr<ArrayBuffer>> HybridMMKV::getBuffer(const std::str
145148 }
146149}
147150bool HybridMMKV::contains (const std::string& key) {
148- return instance ->containsKey (key);
151+ return _instance ->containsKey (key);
149152}
150153bool HybridMMKV::remove (const std::string& key) {
151- bool wasRemoved = instance ->removeValueForKey (key);
154+ bool wasRemoved = _instance ->removeValueForKey (key);
152155 if (wasRemoved) {
153156 // Notify on changed
154- MMKVValueChangedListenerRegistry::notifyOnValueChanged (instance ->mmapID (), key);
157+ MMKVValueChangedListenerRegistry::notifyOnValueChanged (_instance ->mmapID (), key);
155158 }
156159 return wasRemoved;
157160}
158161std::vector<std::string> HybridMMKV::getAllKeys () {
159- return instance ->allKeys ();
162+ return _instance ->allKeys ();
160163}
161164void HybridMMKV::clearAll () {
162165 auto keysBefore = getAllKeys ();
163- instance ->clearAll ();
166+ _instance ->clearAll ();
164167 for (const auto & key : keysBefore) {
165168 // Notify on changed
166- MMKVValueChangedListenerRegistry::notifyOnValueChanged (instance ->mmapID (), key);
169+ MMKVValueChangedListenerRegistry::notifyOnValueChanged (_instance ->mmapID (), key);
167170 }
168171}
169172void HybridMMKV::recrypt (const std::optional<std::string>& key) {
170173 bool successful = false ;
171174 if (key.has_value ()) {
172175 // Encrypt with the given key
173- successful = instance ->reKey (key.value ());
176+ successful = _instance ->reKey (key.value ());
174177 } else {
175178 // Remove the encryption key by setting it to a blank string
176- successful = instance ->reKey (std::string ());
179+ successful = _instance ->reKey (std::string ());
177180 }
178181 if (!successful) {
179182 throw std::runtime_error (" Failed to recrypt MMKV instance!" );
180183 }
181184}
182185void HybridMMKV::trim () {
183- instance ->trim ();
184- instance ->clearMemoryCache ();
186+ _instance ->trim ();
187+ _instance ->clearMemoryCache ();
185188}
186189
187190Listener HybridMMKV::addOnValueChangedListener (const std::function<void (const std::string& /* key */ )>& onValueChanged) {
188191 // Add listener
189- auto mmkvID = instance ->mmapID ();
190- auto listenerID = MMKVValueChangedListenerRegistry::addListener (instance ->mmapID (), onValueChanged);
192+ auto mmkvID = _instance ->mmapID ();
193+ auto listenerID = MMKVValueChangedListenerRegistry::addListener (_instance ->mmapID (), onValueChanged);
191194
192195 return Listener ([=]() {
193196 // remove()
0 commit comments