RawOptionsContainer.hpp
Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011  Antonio Franchi and Paolo Stegagno
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: martin.riedel@tuebingen.mpg.de
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00027 #ifndef RAWOPTIONSCONTAINER_HPP__
00028 #define RAWOPTIONSCONTAINER_HPP__
00029 
00030 #include <telekyb_defines/telekyb_defines.hpp>
00031 
00032 #include <telekyb_base/Tools/XmlRpcHelper.hpp>
00033 
00034 // STL
00035 #include <map>
00036 #include <string>
00037 #include <iostream>
00038 #include <sstream>
00039 
00040 // YAML
00041 #include <yaml-cpp/yaml.h>
00042 // Helper
00043 #include <telekyb_base/Tools/YamlHelper.hpp>
00044 
00045 // ros
00046 #include <ros/ros.h>
00047 
00048 namespace TELEKYB_NAMESPACE
00049 {
00050 
00051 typedef std::map<std::string, std::string> StringMap;
00052 
00053 class RawOptionsContainer {
00054 protected:
00055         static StringMap rawOptionsMap;
00056         static bool addUpdateDashOption(std::string dashkey, const std::string& value, bool overwrite, bool showDashError = false);
00057 
00058 public:
00059         // Checking
00060         static bool hasOption(const std::string& key);
00061         // Manipulation
00062         static bool removeOption(const std::string& key);
00063         static bool getOption(const std::string& key, std::string& value);
00064         // Option must exist
00065         static bool updateOption(const std::string& key, const std::string& value);
00066         // Option must NOT exist
00067         static bool addOption(const std::string& key, const std::string& value);
00068         // Value gets set whether option exists or not
00069         static void addUpdateOption(const std::string& key, const std::string& value);
00070 
00071         // parse CommandLine Arguments.
00072         static bool parseCommandLine(int argc, char* const argv[], bool overwrite = false);
00073         static bool parseCommandLine(const std::vector<std::string>& commandLineArgs, bool overwrite = false);
00074 
00075         // parse File
00076         static bool parseFile(const std::string& fileName, bool overwrite = false);
00077 
00078         // Template Function for Object
00079         template < class _T > //, class StringConversion_>
00080         static bool getOptionValue(const std::string& name, _T& value) {
00081                 bool success = false;
00082                 std::string stringValue;
00083                 if (getOption(name, stringValue)) {
00084                         success = true;
00085                 } else if (ros::param::has("~" + name)) {
00086                         //ROS_INFO_STREAM("Found Option " << name << " in private Nodehandle ParameterServer");
00087                         XmlRpc::XmlRpcValue xmlValue;
00088                         ros::param::get("~" + name, xmlValue);
00089                         success = XmlRpcHelper::xmlRpcValuetoString(xmlValue, stringValue);
00090                 } else {
00091                         // success = false
00092                 }
00093 
00094                 if (success) {
00095                         // remove from all containers!
00096                         //removeOption(name);
00097                         //ros::param::del("~" + name);
00098 
00099                         if (! YamlHelper::parseStringToValue(stringValue, value) ) {
00100                                 ROS_ERROR_STREAM("Option " << name << ". Unable to parse String!");
00101                                 success = false;
00102                         }
00103                         //std::cout << "Parsed Value: " << value << std::endl;
00104                 }
00105 //              else {
00106 //                      ROS_DEBUG_STREAM("Option " << name << " not specified!");
00107 //              }
00108 
00109                 return success;
00110         }
00111 
00112 //      template < class _T >
00113 //      static bool getOptionValue(const std::string& name, _T& value) {
00114 //              return getOptionValue< _T, StringConversion<_T> >(name, value);
00115 //      }
00116 
00117         // be careful with this.
00118         static void clearOptions();
00119 
00120         static void print();
00121         static std::string toString();
00122 
00123         // Get Reference to StringMap
00124         static StringMap& getMap();
00125 
00126 };
00127 
00128 } // namespace
00129 
00130 #endif /* OPTIONSCONTAINER_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_base
Author(s): Dr. Antonio Franchi and Martin Riedel
autogenerated on Mon Nov 11 2013 11:12:34