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 
00018 #include <mrpt/version.h>
00019 #if MRPT_VERSION<0x199
00020 #include <mrpt/utils/utils_defs.h>  // mrpt::format()
00021 using namespace mrpt::utils;
00022 #else
00023 #include <mrpt/core/format.h>
00024 using namespace mrpt;
00025 #endif
00026 
00027 #include <sstream>  // std::stringstream
00028 #include <map>
00029 #include <string>
00030 
00031 using namespace mvsim;
00032 
00033 TClassFactory_worldElements mvsim::classFactory_worldElements;
00034 
00035 // Explicit registration calls seem to be one (the unique?) way to assure
00036 // registration takes place:
00037 void register_all_world_elements()
00038 {
00039         static bool done = false;
00040         if (done)
00041                 return;
00042         else
00043                 done = true;
00044 
00045         REGISTER_WORLD_ELEMENT("ground_grid", GroundGrid)
00046         REGISTER_WORLD_ELEMENT("occupancy_grid", OccupancyGridMap)
00047         REGISTER_WORLD_ELEMENT("elevation_map", ElevationMap)
00048 }
00049 
00050 WorldElementBase* WorldElementBase::factory(
00051         World* parent, const rapidxml::xml_node<char>* root, const char* class_name)
00052 {
00053         register_all_world_elements();
00054 
00055         using namespace std;
00056         using namespace rapidxml;
00057 
00058         string sName;
00059         if (!root)
00060         {
00061                 sName = string(class_name);
00062         }
00063         else
00064         {
00065                 if (0 != strcmp(root->name(), "element"))
00066                         throw runtime_error(
00067                                 mrpt::format(
00068                                         "[WorldElementBase::factory] XML root element is '%s' "
00069                                         "('<element>' expected)",
00070                                         root->name()));
00071 
00072                 // Get class name:
00073                 const xml_attribute<>* attrib_class = root->first_attribute("class");
00074                 if (!attrib_class || !attrib_class->value())
00075                         throw runtime_error(
00076                                 "[WorldElementBase::factory] Missing mandatory attribute "
00077                                 "'class' in node <element>");
00078 
00079                 sName = string(attrib_class->value());
00080         }
00081         WorldElementBase* we =
00082                 classFactory_worldElements.create(sName, parent, root);
00083 
00084         if (!we)
00085                 throw runtime_error(
00086                         mrpt::format(
00087                                 "[WorldElementBase::factory] Unknown world element type '%s'",
00088                                 root->name()));
00089 
00090         return we;
00091 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35