// // ToyValueStorage.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 13.06.2017. // // #include #include "ToyValueStorage.h" const std::string ToyValueStorage::DefaultContainer = "DefaultContainer"; std::string ToyValueStorage::storeValue(const rapidjson::Value& value, const std::string& container) { auto key = this->generateNextKey(container); this->storeValueWithKey(value, key, container); return key; } void ToyValueStorage::storeValueWithKey(const rapidjson::Value& value, const std::string& key, const std::string& container) { if(_storedValues[container].find(key) == _storedValues[container].end()){ rapidjson::Value* copy = new rapidjson::Value(); copy->CopyFrom(value, _valueStorage->GetAllocator()); _storedValues[container].insert({key, copy}); } } rapidjson::Value* ToyValueStorage::getStoredValue(const std::string& key, const std::string& container) { auto& containerMap = _storedValues[container]; if(containerMap.find(key) != containerMap.end()){ if(containerMap.find(key) != containerMap.end()){ return containerMap[key]; } } return nullptr; } void ToyValueStorage::removeStoredValue(const std::string& key, const std::string& container){ //return; auto& containerMap = _storedValues[container]; if(containerMap.find(key) != containerMap.end()){ // cocos2d::log("deleting value for key: %s container %s", key.c_str(), container.c_str()); delete containerMap[key]; containerMap.erase(key); } } void ToyValueStorage::removeAllStoredValues(const std::string& container) { //return; if(container != ""){ auto& containerMap = _storedValues[container]; for(auto& pair : containerMap){ // cocos2d::log("deleting value for key: %s in container %s", pair.first.c_str(), container.c_str()); delete pair.second; } _storedValues.erase(container); } else { for(auto& containerMap : _storedValues){ for(auto& pair : containerMap.second){ delete pair.second; } } _storedValues.clear(); } } std::string ToyValueStorage::storeFunction(std::function callback, const std::string& container) { auto key = this->generateNextKey(container); this->storeFunctionWithKey(callback, key, container); return key; } void ToyValueStorage::storeFunctionWithKey(std::function callback, const std::string& key, const std::string& container) { if(_storedFunctions[container].find(key) == _storedFunctions[container].end()){ _storedFunctions[container].insert({key, callback}); // cocos2d::log("stored function for key %s continer %s", key.c_str(), container.c_str()); } } std::function ToyValueStorage::getStoredFunction(const std::string& key, const std::string& container) { if(container != ""){ auto& containerMap = _storedFunctions[container]; if(containerMap.find(key) != containerMap.end()){ return containerMap[key]; } } return nullptr; } void ToyValueStorage::runStoredFunction(const std::string& key, const std::string& container) { // cocos2d::log("running function for key %s container %s", key.c_str(), container.c_str()); if(container != ""){ auto& containerMap = _storedFunctions[container]; if(containerMap.find(key) != containerMap.end()){ // cocos2d::log("success running function for key %s container %s", key.c_str(), container.c_str()); containerMap[key](); } } } void ToyValueStorage::runAndRemoveStoredFunction(const std::string& key, const std::string& container) { if(container != ""){ auto& containerMap = _storedFunctions[container]; if(containerMap.find(key) != containerMap.end()){ containerMap[key](); containerMap.erase(key); // cocos2d::log("1 removed function for key %s container %s", key.c_str(), container.c_str()); } } } void ToyValueStorage::removeStoredFunction(const std::string& key, const std::string& container) { //return; if(container != ""){ auto& containerMap = _storedFunctions[container]; if(containerMap.find(key) != containerMap.end()){ containerMap.erase(key); // cocos2d::log("2 removed function for key %s container %s", key.c_str(), container.c_str()); } } } void ToyValueStorage::removeAllStoredFunctions(const std::string& container) { //return; // cocos2d::log("removing all stored functions for container %s", container.c_str()); if(container != ""){ _storedFunctions.erase(container); } else { _storedFunctions.clear(); } } std::string ToyValueStorage::storeToySimpleValue(const ToySimpleValue& value, const std::string& container) { auto key = this->generateNextKey(container); this->storeToySimpleValueWithKey(value, key, container); return key; } void ToyValueStorage::storeToySimpleValueWithKey(const ToySimpleValue& value, const std::string& key, const std::string& container) { if(_storedToySimpleValues[container].find(key) == _storedToySimpleValues[container].end()){ ToySimpleValue* copy = new ToySimpleValue(value); _storedToySimpleValues[container].insert({key, copy}); } } ToySimpleValue* ToyValueStorage::getStoredToySimpleValue(const std::string& key, const std::string& container) { auto& containerMap = _storedToySimpleValues[container]; if(containerMap.find(key) != containerMap.end()){ return containerMap[key]; } return nullptr; } void ToyValueStorage::removeStoredToySimpleValue(const std::string& key, const std::string& container) { //return; auto& containerMap = _storedToySimpleValues[container]; if(containerMap.find(key) != containerMap.end()){ delete containerMap[key]; containerMap.erase(key); } } void ToyValueStorage::removeAllStoredToySimpleValues(const std::string& container) { //return; if(container != ""){ auto& containerMap = _storedToySimpleValues[container]; for(auto& pair : containerMap){ delete pair.second; } _storedToySimpleValues.erase(container); } else { for(auto& containerMap : _storedToySimpleValues){ for(auto& pair : containerMap.second){ delete pair.second; } } _storedToySimpleValues.clear(); } } void ToyValueStorage::clearStoredData(const std::string& container) { this->removeAllStoredValues(container); this->removeAllStoredFunctions(container); this->removeAllStoredToySimpleValues(container); if(container != ""){ _lastStoredKeyNumbers.erase(container); _storedToySimpleValues.erase(container); _storedValues.erase(container); _storedFunctions.erase(container); } else { _lastStoredKeyNumbers.clear(); } } std::string ToyValueStorage::generateNextKey(const std::string& container) { if(_lastStoredKeyNumbers.find(container) == _lastStoredKeyNumbers.end()){ _lastStoredKeyNumbers[container] = 0; } else { _lastStoredKeyNumbers[container] = (_lastStoredKeyNumbers[container]+1)%_modulus; } return "value" + std::to_string(_lastStoredKeyNumbers[container]); };