Added MStorage::merge function

This commit is contained in:
MultiMote 2017-05-09 12:59:28 +03:00
parent a87e63a277
commit d6b8759ee2
3 changed files with 16 additions and 0 deletions

@ -350,6 +350,16 @@ void MStorage::setGroup(const std::string &key, MStorage *group) {
insertRaw(key, group);
}
void MStorage::merge(MStorage *group, bool replaceValues) {
MValueMapIterator it = group->begin();
while(it != group->end()) {
if(replaceValues || !containsKey(it->first)) {
insertRaw(it->first, it->second);
}
it++;
}
}
int32_t MStorage::getInt(const std::string &key) {
IMStorageBasicType *bval = getBasicType(key);
return bval ? bval->getInt() : 0;

@ -147,6 +147,7 @@ public:
void setByte(const std::string &key, uint8_t val);
void setString(const std::string &key, std::string val);
void setGroup(const std::string &key, MStorage *group);
void merge(MStorage *group, bool replaceValues);
int32_t getInt(const std::string &key);
int32_t* getIntArray(const std::string &key, uint16_t *len);

@ -40,6 +40,11 @@ int main(int argc, char const *argv[]) {
int32_t _ints[] = {42, 34, 32323, 5573453};
group->setIntArray("someIntArray", _ints, 4); // array is being copied
MStorage *groupToMerge = new MStorage();
groupToMerge->setDouble("someDouble2", 1.23);
group->merge(groupToMerge, false);
storage->setGroup("someGroup", group);
print_fieds(storage);