1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Extract "inherit node" function

This commit is contained in:
AlexVinS 2014-05-19 02:28:44 +04:00
parent e90fae9638
commit eff801f39a
3 changed files with 23 additions and 14 deletions

View File

@ -871,19 +871,14 @@ void CSpellHandler::beforeValidate(JsonNode & object)
JsonNode& levels = object["levels"];
JsonNode& base = levels["base"];
auto inheritNode = [&](JsonNode & dest){
//merging destination node into copy of base node implements inheritance semantic
//detail configuration is priority
//this allows more simplification
JsonNode inheritedNode(base);
JsonUtils::merge(inheritedNode,dest);
dest.swap(inheritedNode);
auto inheritNode = [&](const std::string & name){
JsonUtils::inherit(levels[name],base);
};
inheritNode(levels["none"]);
inheritNode(levels["basic"]);
inheritNode(levels["advanced"]);
inheritNode(levels["expert"]);
inheritNode("none");
inheritNode("basic");
inheritNode("advanced");
inheritNode("expert");
}

View File

@ -735,6 +735,13 @@ void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
merge(dest, source);
}
void JsonUtils::inherit(JsonNode & descendant, const JsonNode & base)
{
JsonNode inheritedNode(base);
merge(inheritedNode,descendant);
descendant.swap(inheritedNode);
}
JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
{
bool isValid;

View File

@ -141,7 +141,7 @@ namespace JsonUtils
DLL_LINKAGE void resolveIdentifier (const JsonNode &node, si32 &var);
/**
* @brief recursivly merges source into dest, replacing identical fields
* @brief recursively merges source into dest, replacing identical fields
* struct : recursively calls this function
* arrays : each entry will be merged recursively
* values : value in source will replace value in dest
@ -151,7 +151,7 @@ namespace JsonUtils
DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source);
/**
* @brief recursivly merges source into dest, replacing identical fields
* @brief recursively merges source into dest, replacing identical fields
* struct : recursively calls this function
* arrays : each entry will be merged recursively
* values : value in source will replace value in dest
@ -160,6 +160,13 @@ namespace JsonUtils
*/
DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source);
/** @brief recursively merges descendant into copy of base node
* Result emulates inheritance semantic
*
*
*/
DLL_LINKAGE void inherit(JsonNode & descendant, const JsonNode & base);
/**
* @brief generate one Json structure from multiple files
* @param files - list of filenames with parts of json structure
@ -167,7 +174,7 @@ namespace JsonUtils
DLL_LINKAGE JsonNode assembleFromFiles(std::vector<std::string> files);
DLL_LINKAGE JsonNode assembleFromFiles(std::vector<std::string> files, bool & isValid);
/// This version loads all files with same name (overriden by mods)
/// This version loads all files with same name (overridden by mods)
DLL_LINKAGE JsonNode assembleFromFiles(std::string filename);
/**