Author: silene
Date: Sat Apr  4 19:50:56 2009
New Revision: 34485

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34485&view=rev
Log:
Added a config::add_child variant that does not create the child if it already 
exists.

Modified:
    trunk/src/config.cpp
    trunk/src/config.hpp
    trunk/src/multiplayer_connect.cpp
    trunk/src/preferences.cpp
    trunk/src/unit.cpp
    trunk/src/unit_types.cpp

Modified: trunk/src/config.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/config.cpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/config.cpp (original)
+++ trunk/src/config.cpp Sat Apr  4 19:50:56 2009
@@ -166,6 +166,15 @@
                return *i->second.front();
 
        return config();
+}
+
+config &config::child_or_add(const std::string &key)
+{
+       child_map::const_iterator i = children.find(key);
+       if (i != children.end() && !i->second.empty())
+               return *i->second.front();
+
+       return add_child(key);
 }
 
 config& config::add_child(const std::string& key)

Modified: trunk/src/config.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/config.hpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/config.hpp (original)
+++ trunk/src/config.hpp Sat Apr  4 19:50:56 2009
@@ -175,6 +175,12 @@
        t_string& operator[](const std::string& key);
        const t_string& operator[](const std::string& key) const;
 
+       /**
+        * Returns a reference to the first child with the given @a key.
+        * Creates the child if it does not yet exist.
+        */
+       config &child_or_add(const std::string &key);
+
        const t_string& get_attribute(const std::string& key) const;
        bool has_attribute(const std::string& key) const {return 
values.find(key) != values.end();}
        void remove_attribute(const std::string& key) {values.erase(key);}

Modified: trunk/src/multiplayer_connect.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/multiplayer_connect.cpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/multiplayer_connect.cpp (original)
+++ trunk/src/multiplayer_connect.cpp Sat Apr  4 19:50:56 2009
@@ -658,10 +658,9 @@
                        }
                        {
                                res["id"] = res["save_id"];
-                               config *ai = res.child("ai");
-                               if (!ai) ai = &res.add_child("ai");
+                               config &ai = res.child_or_add("ai");
                                if (ai_algorithm_ != "default")
-                                    (*ai)["ai_algorithm"] = ai_algorithm_;
+                                       ai["ai_algorithm"] = ai_algorithm_;
                        }
                        description = N_("Computer player");
                        break;

Modified: trunk/src/preferences.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/preferences.cpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/preferences.cpp (original)
+++ trunk/src/preferences.cpp Sat Apr  4 19:50:56 2009
@@ -585,12 +585,10 @@
        hotkey::save_hotkeys(prefs);
 }
 
-void add_alias(const std::string& alias, const std::string& command) {
-       config* alias_list = prefs.child("alias");
-       if (alias_list == NULL) {
-               alias_list = &(prefs.add_child("alias"));
-       }
-       (*alias_list)[alias] = command;
+void add_alias(const std::string &alias, const std::string &command)
+{
+       config &alias_list = prefs.child_or_add("alias");
+       alias_list[alias] = command;
 }
 
 

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Sat Apr  4 19:50:56 2009
@@ -2633,36 +2633,27 @@
                                                set_state(remove,"");
                                        }
                                } else if (apply_to == "movement_costs") {
-                                       config *mv = 
cfg_.child("movement_costs");
+                                       config &mv = 
cfg_.child_or_add("movement_costs");
                                        const config *ap = 
effect.child("movement_costs");
                                        const std::string &replace = 
effect["replace"];
-                                       if(!mv) {
-                                               mv = 
&cfg_.add_child("movement_costs");
-                                       }
                                        if (ap) {
-                                               mod_mdr_merge(*mv, *ap, 
!utils::string_bool(replace));
+                                               mod_mdr_merge(mv, *ap, 
!utils::string_bool(replace));
                                        }
                                        movement_costs_.clear();
                                } else if (apply_to == "defense") {
-                                       config *mv = cfg_.child("defense");
+                                       config &mv = 
cfg_.child_or_add("defense");
                                        const config *ap = 
effect.child("defense");
                                        const std::string &replace = 
effect["replace"];
-                                       if(!mv) {
-                                               mv = &cfg_.add_child("defense");
-                                       }
                                        if (ap) {
-                                               mod_mdr_merge(*mv, *ap, 
!utils::string_bool(replace));
+                                               mod_mdr_merge(mv, *ap, 
!utils::string_bool(replace));
                                        }
                                        defense_mods_.clear();
                                } else if (apply_to == "resistance") {
-                                       config *mv = cfg_.child("resistance");
+                                       config &mv = 
cfg_.child_or_add("resistance");
                                        const config *ap = 
effect.child("resistance");
                                        const std::string &replace = 
effect["replace"];
-                                       if(!mv) {
-                                               mv = 
&cfg_.add_child("resistance");
-                                       }
                                        if (ap) {
-                                               mod_mdr_merge(*mv, *ap, 
!utils::string_bool(replace));
+                                               mod_mdr_merge(mv, *ap, 
!utils::string_bool(replace));
                                        }
                                } else if (apply_to == "zoc") {
                                        const std::string &zoc_value = 
effect["value"];
@@ -2670,10 +2661,7 @@
                                                emit_zoc_ = 
utils::string_bool(zoc_value);
                                        }
                                } else if (apply_to == "new_ability") {
-                                       config *ab = cfg_.child("abilities");
-                                       if(!ab) {
-                                               ab = 
&cfg_.add_child("abilities");
-                                       }
+                                       config &ab = 
cfg_.child_or_add("abilities");
                                        const config *ab_effect = 
effect.child("abilities");
                                        if (ab_effect) {
                                                config to_append;
@@ -2682,7 +2670,7 @@
                                                                
to_append.add_child(ab.key, ab.cfg);
                                                        }
                                                }
-                                               ab->append(to_append);
+                                               ab.append(to_append);
                                        }
                                } else if (apply_to == "remove_ability") {
                                        const config *ab_effect = 
effect.child("abilities");

Modified: trunk/src/unit_types.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_types.cpp?rev=34485&r1=34484&r2=34485&view=diff
==============================================================================
--- trunk/src/unit_types.cpp (original)
+++ trunk/src/unit_types.cpp Sat Apr  4 19:50:56 2009
@@ -178,13 +178,9 @@
                if ( mode != "append") {
                        cfg_.clear_children("specials");
                }
-               config* new_specials = cfg_.child("specials");
-               if (new_specials == NULL) {
-                       cfg_.add_child("specials");
-                       new_specials = cfg_.child("specials");
-               }
+               config &new_specials = cfg_.child_or_add("specials");
                foreach (const config::any_child &value, 
set_specials->all_children_range()) {
-                       new_specials->add_child(value.key, value.cfg);
+                       new_specials.add_child(value.key, value.cfg);
                }
        }
 


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to