1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-21 12:06:49 +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& levels = object["levels"];
JsonNode& base = levels["base"]; JsonNode& base = levels["base"];
auto inheritNode = [&](JsonNode & dest){ auto inheritNode = [&](const std::string & name){
//merging destination node into copy of base node implements inheritance semantic JsonUtils::inherit(levels[name],base);
//detail configuration is priority
//this allows more simplification
JsonNode inheritedNode(base);
JsonUtils::merge(inheritedNode,dest);
dest.swap(inheritedNode);
}; };
inheritNode(levels["none"]); inheritNode("none");
inheritNode(levels["basic"]); inheritNode("basic");
inheritNode(levels["advanced"]); inheritNode("advanced");
inheritNode(levels["expert"]); inheritNode("expert");
} }

View File

@ -735,6 +735,13 @@ void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
merge(dest, 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) JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
{ {
bool isValid; bool isValid;

View File

@ -141,7 +141,7 @@ namespace JsonUtils
DLL_LINKAGE void resolveIdentifier (const JsonNode &node, si32 &var); 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 * struct : recursively calls this function
* arrays : each entry will be merged recursively * arrays : each entry will be merged recursively
* values : value in source will replace value in dest * values : value in source will replace value in dest
@ -151,7 +151,7 @@ namespace JsonUtils
DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source); 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 * struct : recursively calls this function
* arrays : each entry will be merged recursively * arrays : each entry will be merged recursively
* values : value in source will replace value in dest * values : value in source will replace value in dest
@ -160,6 +160,13 @@ namespace JsonUtils
*/ */
DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source); 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 * @brief generate one Json structure from multiple files
* @param files - list of filenames with parts of json structure * @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);
DLL_LINKAGE JsonNode assembleFromFiles(std::vector<std::string> files, bool & isValid); 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); DLL_LINKAGE JsonNode assembleFromFiles(std::string filename);
/** /**