// // AniValueStorage.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 13.06.2017. // // #include #include "AniValueStorage.h" const std::string AniValueStorage::DefaultContainer = "DefaultContainer"; std::string AniValueStorage::storeValue(const rapidjson::Value& value, const std::string& container) { auto key = this->generateNextKey(container); this->storeValueWithKey(value, key, container); return key; } void AniValueStorage::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, _AniValueStorage->GetAllocator()); _storedValues[container].insert({key, copy}); } } rapidjson::Value* AniValueStorage::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 AniValueStorage::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 AniValueStorage::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 AniValueStorage::storeFunction(std::function callback, const std::string& container) { auto key = this->generateNextKey(container); this->storeFunctionWithKey(callback, key, container); return key; } void AniValueStorage::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 AniValueStorage::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 AniValueStorage::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 AniValueStorage::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 AniValueStorage::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 AniValueStorage::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 AniValueStorage::storeAniSimpleValue(const AniSimpleValue& value, const std::string& container) { auto key = this->generateNextKey(container); this->storeAniSimpleValueWithKey(value, key, container); return key; } void AniValueStorage::storeAniSimpleValueWithKey(const AniSimpleValue& value, const std::string& key, const std::string& container) { if(_storedAniSimpleValues[container].find(key) == _storedAniSimpleValues[container].end()){ AniSimpleValue* copy = new AniSimpleValue(value); _storedAniSimpleValues[container].insert({key, copy}); } } AniSimpleValue* AniValueStorage::getStoredAniSimpleValue(const std::string& key, const std::string& container) { auto& containerMap = _storedAniSimpleValues[container]; if(containerMap.find(key) != containerMap.end()){ return containerMap[key]; } return nullptr; } void AniValueStorage::removeStoredAniSimpleValue(const std::string& key, const std::string& container) { //return; auto& containerMap = _storedAniSimpleValues[container]; if(containerMap.find(key) != containerMap.end()){ delete containerMap[key]; containerMap.erase(key); } } void AniValueStorage::removeAllStoredAniSimpleValues(const std::string& container) { //return; if(container != ""){ auto& containerMap = _storedAniSimpleValues[container]; for(auto& pair : containerMap){ delete pair.second; } _storedAniSimpleValues.erase(container); } else { for(auto& containerMap : _storedAniSimpleValues){ for(auto& pair : containerMap.second){ delete pair.second; } } _storedAniSimpleValues.clear(); } } void AniValueStorage::clearStoredData(const std::string& container) { this->removeAllStoredValues(container); this->removeAllStoredFunctions(container); this->removeAllStoredAniSimpleValues(container); if(container != ""){ _lastStoredKeyNumbers.erase(container); _storedAniSimpleValues.erase(container); _storedValues.erase(container); _storedFunctions.erase(container); } else { _lastStoredKeyNumbers.clear(); } } std::string AniValueStorage::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]); };