ValueStorage.cpp 7.14 KB
//
//  ValueStorage.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 13.06.2017.
//
//

#include <stdio.h>
#include "ValueStorage.h"

const std::string ValueStorage::DefaultContainer = "DefaultContainer";

std::string ValueStorage::storeValue(const rapidjson::Value& value, const std::string& container)
{
    auto key = this->generateNextKey(container);
    this->storeValueWithKey(value, key, container);
    return key;
}

void ValueStorage::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* ValueStorage::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 ValueStorage::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 ValueStorage::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 ValueStorage::storeFunction(std::function<void()> callback, const std::string& container)
{
    auto key = this->generateNextKey(container);
    this->storeFunctionWithKey(callback, key, container);
    return key;
}

void ValueStorage::storeFunctionWithKey(std::function<void()> 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<void()> ValueStorage::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 ValueStorage::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 ValueStorage::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 ValueStorage::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 ValueStorage::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 ValueStorage::storeSimpleValue(const SimpleValue& value, const std::string& container)
{
    auto key = this->generateNextKey(container);
    this->storeSimpleValueWithKey(value, key, container);
    return key;
}

void ValueStorage::storeSimpleValueWithKey(const SimpleValue& value, const std::string& key, const std::string& container)
{
    if(_storedSimpleValues[container].find(key) == _storedSimpleValues[container].end()){
        SimpleValue* copy = new SimpleValue(value);
        _storedSimpleValues[container].insert({key, copy});
    }
}

SimpleValue* ValueStorage::getStoredSimpleValue(const std::string& key, const std::string& container)
{
    auto& containerMap = _storedSimpleValues[container];
    if(containerMap.find(key) != containerMap.end()){
        return containerMap[key];
    }
    return nullptr;
}

void ValueStorage::removeStoredSimpleValue(const std::string& key, const std::string& container)
{
//return;
    auto& containerMap = _storedSimpleValues[container];
    if(containerMap.find(key) != containerMap.end()){
        delete containerMap[key];
        containerMap.erase(key);
    }
}

void ValueStorage::removeAllStoredSimpleValues(const std::string& container)
{
//return;
    if(container != ""){
    
        auto& containerMap = _storedSimpleValues[container];
        for(auto& pair : containerMap){
                delete pair.second;
            }
        _storedSimpleValues.erase(container);
    
    } else {
        for(auto& containerMap : _storedSimpleValues){
            for(auto& pair : containerMap.second){
                delete pair.second;
            }
        }
        _storedSimpleValues.clear();
    }
}

void ValueStorage::clearStoredData(const std::string& container)
{
    this->removeAllStoredValues(container);
    this->removeAllStoredFunctions(container);
    this->removeAllStoredSimpleValues(container);
    
    if(container != ""){
        _lastStoredKeyNumbers.erase(container);
        _storedSimpleValues.erase(container);
        _storedValues.erase(container);
        _storedFunctions.erase(container);
    }
    else {
        _lastStoredKeyNumbers.clear();
    }
}

std::string ValueStorage::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]);
};