Author: silene
Date: Sat Apr  4 19:50:51 2009
New Revision: 34484

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34484&view=rev
Log:
Added a config::child variant for safe initialization.

Modified:
    trunk/src/ai_dfool.cpp
    trunk/src/config.cpp
    trunk/src/config.hpp
    trunk/src/mapgen.cpp
    trunk/src/mapgen_dialog.cpp

Modified: trunk/src/ai_dfool.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_dfool.cpp?rev=34484&r1=34483&r2=34484&view=diff
==============================================================================
--- trunk/src/ai_dfool.cpp (original)
+++ trunk/src/ai_dfool.cpp Sat Apr  4 19:50:51 2009
@@ -306,7 +306,7 @@
   {
     const config::child_list mem_list=cfg.get_children("unit_memory");
     for(config::child_list::const_iterator mem = mem_list.begin(); mem != 
mem_list.end(); ++mem) {
-      config unit_cfg = *((*mem)->child("unit"));
+      config unit_cfg = (*mem)->child_or_empty("unit");
 
       unit u(unit_cfg);
 

Modified: trunk/src/config.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/config.cpp?rev=34484&r1=34483&r2=34484&view=diff
==============================================================================
--- trunk/src/config.cpp (original)
+++ trunk/src/config.cpp Sat Apr  4 19:50:51 2009
@@ -157,6 +157,15 @@
        } else {
                return NULL;
        }
+}
+
+config config::child_or_empty(const std::string& key) const
+{
+       child_map::const_iterator i = children.find(key);
+       if (i != children.end() && !i->second.empty())
+               return *i->second.front();
+
+       return config();
 }
 
 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=34484&r1=34483&r2=34484&view=diff
==============================================================================
--- trunk/src/config.hpp (original)
+++ trunk/src/config.hpp Sat Apr  4 19:50:51 2009
@@ -162,6 +162,11 @@
        const child_list& get_children(const std::string& key) const;
        const child_map& all_children() const;
 
+       /**
+        * Copies the first child with the given @a key, or an empty config if 
there is none.
+        */
+       config child_or_empty(const std::string &key) const;
+
        config* child(const std::string& key);
        const config* child(const std::string& key) const;
        config& add_child(const std::string& key);

Modified: trunk/src/mapgen.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/mapgen.cpp?rev=34484&r1=34483&r2=34484&view=diff
==============================================================================
--- trunk/src/mapgen.cpp (original)
+++ trunk/src/mapgen.cpp Sat Apr  4 19:50:51 2009
@@ -765,11 +765,7 @@
        LOG_NG << "done generating height map...\n";
        LOG_NG << (SDL_GetTicks() - ticks) << "\n"; ticks = SDL_GetTicks();
 
-       const config* const names_info = cfg.child("naming");
-       config naming;
-       if(names_info != NULL) {
-               naming = *names_info;
-       }
+       config naming = cfg.child_or_empty("naming");
 
        // Make a dummy race for generating names
        unit_race name_generator(naming);
@@ -1282,12 +1278,9 @@
                }
        }
 
-       if(nvillages > 0) {
-               const config* const naming = cfg.child("village_naming");
-               config naming_cfg;
-               if(naming != NULL) {
-                       naming_cfg = *naming;
-               }
+       if (nvillages > 0)
+       {
+               config naming_cfg = cfg.child_or_empty("village_naming");
 
                const unit_race village_names_generator(naming_cfg);
 

Modified: trunk/src/mapgen_dialog.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/mapgen_dialog.cpp?rev=34484&r1=34483&r2=34484&view=diff
==============================================================================
--- trunk/src/mapgen_dialog.cpp (original)
+++ trunk/src/mapgen_dialog.cpp Sat Apr  4 19:50:51 2009
@@ -405,12 +405,8 @@
 config default_map_generator::create_scenario(const std::vector<std::string>& 
args)
 {
        DBG_NG << "creating scenario...\n";
-       config res;
-
-       const config* const scenario = cfg_.child("scenario");
-       if(scenario != NULL) {
-               res = *scenario;
-       }
+
+       config res = cfg_.child_or_empty("scenario");
 
        DBG_NG << "got scenario data...\n";
 


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

Reply via email to