Tencent / MMKV

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
Other
17.2k stars 1.89k forks source link

feat: Also expose C++ `set`, `getBytes`, `getVector` methods #1260

Closed mrousavy closed 4 months ago

mrousavy commented 4 months ago

Also exposes these methods to Apple Platforms:

    bool set(const char *value, MMKVKey_t key);
    bool set(const char *value, MMKVKey_t key, uint32_t expireDuration);

    bool set(const std::string &value, MMKVKey_t key);
    bool set(const std::string &value, MMKVKey_t key, uint32_t expireDuration);

    bool set(const mmkv::MMBuffer &value, MMKVKey_t key);
    bool set(const mmkv::MMBuffer &value, MMKVKey_t key, uint32_t expireDuration);

    bool set(const std::vector<std::string> &vector, MMKVKey_t key);
    bool set(const std::vector<std::string> &vector, MMKVKey_t key, uint32_t expireDuration);

    // inplaceModification is recommended for faster speed
    bool getString(MMKVKey_t key, std::string &result, bool inplaceModification = true);

    mmkv::MMBuffer getBytes(MMKVKey_t key);

    bool getBytes(MMKVKey_t key, mmkv::MMBuffer &result);

    bool getVector(MMKVKey_t key, std::vector<std::string> &result);

Previously, those methods were hidden under a #ifdef APPLE / #else branch, so that on Apple you could only use the NSObject<NSCoding> classes.

In my C++ environment (where I already had strings), it was a redundant extra step to convert from std::string <> NSString, and from MMBuffer <> NSData, so this PR removes that intermediate conversion step.

Unfortunately I couldn't do the same thing for allKeys, as only the return type changes (NSArray* vs std::vector<std::string>), so it cannot be overloaded.

We could introduce an additional method that is available whether MMKV_APPLE is true or not, but that's up to you if you want to do that @lingol.

lingol commented 4 months ago

First thing first, thanks for your contribution.

  1. We don't accept PRs to the master branch, it must goes to the dev branch first.
  2. These methods were not there for iOS before. You can't just simply expose them. https://github.com/Tencent/MMKV/blob/master/Core/MMKV.cpp#L606
mrousavy commented 4 months ago

Ah, my bad - I missed that they are not built either. I'll get back to you later today, will do some testing - this PR should've been draft in the first place as I didn't test yet.