WorldElementBase.cpp
Go to the documentation of this file.
00001 /*+-------------------------------------------------------------------------+
00002   |                       MultiVehicle simulator (libmvsim)                 |
00003   |                                                                         |
00004   | Copyright (C) 2014  Jose Luis Blanco Claraco (University of Almeria)    |
00005   | Copyright (C) 2017  Borys Tymchenko (Odessa Polytechnic University)     |
00006   | Distributed under GNU General Public License version 3                  |
00007   |   See <http://www.gnu.org/licenses/>                                    |
00008   +-------------------------------------------------------------------------+ */
00009 
00010 #include <mvsim/WorldElements/OccupancyGridMap.h>
00011 #include <mvsim/WorldElements/GroundGrid.h>
00012 #include <mvsim/WorldElements/ElevationMap.h>
00013 
00014 #include <rapidxml.hpp>
00015 #include <rapidxml_utils.hpp>
00016 #include <rapidxml_print.hpp>
00017 #include <mrpt/utils/utils_defs.h>  // mrpt::format()
00018 
00019 #include <sstream>  // std::stringstream
00020 #include <map>
00021 #include <string>
00022 
00023 using namespace mvsim;
00024 
00025 TClassFactory_worldElements mvsim::classFactory_worldElements;
00026 
00027 // Explicit registration calls seem to be one (the unique?) way to assure
00028 // registration takes place:
00029 void register_all_world_elements()
00030 {
00031         static bool done = false;
00032         if (done)
00033                 return;
00034         else
00035                 done = true;
00036 
00037         REGISTER_WORLD_ELEMENT("ground_grid", GroundGrid)
00038         REGISTER_WORLD_ELEMENT("occupancy_grid", OccupancyGridMap)
00039         REGISTER_WORLD_ELEMENT("elevation_map", ElevationMap)
00040 }
00041 
00042 WorldElementBase* WorldElementBase::factory(
00043         World* parent, const rapidxml::xml_node<char>* root, const char* class_name)
00044 {
00045         register_all_world_elements();
00046 
00047         using namespace std;
00048         using namespace rapidxml;
00049 
00050         string sName;
00051         if (!root)
00052         {
00053                 sName = string(class_name);
00054         }
00055         else
00056         {
00057                 if (0 != strcmp(root->name(), "element"))
00058                         throw runtime_error(
00059                                 mrpt::format(
00060                                         "[WorldElementBase::factory] XML root element is '%s' "
00061                                         "('<element>' expected)",
00062                                         root->name()));
00063 
00064                 // Get class name:
00065                 const xml_attribute<>* attrib_class = root->first_attribute("class");
00066                 if (!attrib_class || !attrib_class->value())
00067                         throw runtime_error(
00068                                 "[WorldElementBase::factory] Missing mandatory attribute "
00069                                 "'class' in node <element>");
00070 
00071                 sName = string(attrib_class->value());
00072         }
00073         WorldElementBase* we =
00074                 classFactory_worldElements.create(sName, parent, root);
00075 
00076         if (!we)
00077                 throw runtime_error(
00078                         mrpt::format(
00079                                 "[WorldElementBase::factory] Unknown world element type '%s'",
00080                                 root->name()));
00081 
00082         return we;
00083 }


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:48