浏览代码

Extract "inherit node" function

AlexVinS 11 年之前
父节点
当前提交
eff801f39a
共有 3 个文件被更改,包括 23 次插入14 次删除
  1. 6 11
      lib/CSpellHandler.cpp
  2. 7 0
      lib/JsonNode.cpp
  3. 10 3
      lib/JsonNode.h

+ 6 - 11
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");
 
 }
 

+ 7 - 0
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<std::string> files)
 {
 	bool isValid;

+ 10 - 3
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<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);
 
 	/**