From eff801f39a10f2404399dba4cb0fab69d2667ce4 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Mon, 19 May 2014 02:28:44 +0400 Subject: [PATCH] Extract "inherit node" function --- lib/CSpellHandler.cpp | 17 ++++++----------- lib/JsonNode.cpp | 7 +++++++ lib/JsonNode.h | 13 ++++++++++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/CSpellHandler.cpp b/lib/CSpellHandler.cpp index dc8f43861..257ecb367 100644 --- a/lib/CSpellHandler.cpp +++ b/lib/CSpellHandler.cpp @@ -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"); } diff --git a/lib/JsonNode.cpp b/lib/JsonNode.cpp index 1b4a85fd1..03f9ce6d7 100644 --- a/lib/JsonNode.cpp +++ b/lib/JsonNode.cpp @@ -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 files) { bool isValid; diff --git a/lib/JsonNode.h b/lib/JsonNode.h index 43f12a3f5..c1cc6fb5d 100644 --- a/lib/JsonNode.h +++ b/lib/JsonNode.h @@ -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 @@ -159,6 +159,13 @@ namespace JsonUtils * @note this function will preserve data stored in source by creating copy */ 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 @@ -167,7 +174,7 @@ namespace JsonUtils DLL_LINKAGE JsonNode assembleFromFiles(std::vector files); DLL_LINKAGE JsonNode assembleFromFiles(std::vector 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); /**