HValueStorage.cpp 7.19 KB
//
//  HValueStorage.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 13.06.2017.
//
//

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

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

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

void HValueStorage::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, _HValueStorage->GetAllocator());
        _storedValues[container].insert({key, copy});
    }
}

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

void HValueStorage::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()> HValueStorage::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 HValueStorage::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 HValueStorage::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 HValueStorage::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 HValueStorage::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 HValueStorage::storeHSimpleValue(const HSimpleValue& value, const std::string& container)
{
    auto key = this->generateNextKey(container);
    this->storeHSimpleValueWithKey(value, key, container);
    return key;
}

void HValueStorage::storeHSimpleValueWithKey(const HSimpleValue& value, const std::string& key, const std::string& container)
{
    if(_storedHSimpleValues[container].find(key) == _storedHSimpleValues[container].end()){
        HSimpleValue* copy = new HSimpleValue(value);
        _storedHSimpleValues[container].insert({key, copy});
    }
}

HSimpleValue* HValueStorage::getStoredHSimpleValue(const std::string& key, const std::string& container)
{
    auto& containerMap = _storedHSimpleValues[container];
    if(containerMap.find(key) != containerMap.end()){
        return containerMap[key];
    }
    return nullptr;
}

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

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

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

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