ParameterList.hh

Go to the documentation of this file.
00001 //Copyright 2013 Ben Loer
00002 //This file is part of the ConfigHandler library
00003 //It is released under the terms of the GNU General Public License v3
00004 
00011 #ifndef PARAMETERLIST_h
00012 #define PARAMETERLIST_h
00013 
00014 #include "VParameterNode.hh"
00015 #include "Parameter.hh"
00016 #include "ConfigFunctor.hh"
00017 #include <string>
00018 #include <sstream>
00019 #include <vector>
00020 #include <map>
00021 #include <iostream>
00022 #include "Message.hh"
00023 #include <boost/type_traits.hpp>
00024 #include <boost/shared_ptr.hpp>
00025 
00026 
00041 class ParameterList : public VParameterNode{
00042 public:
00044   ParameterList(const std::string& key="", const std::string& helptext="") : 
00045     VParameterNode(key, helptext) { _node_type = PARAMETER_LIST; }
00047   virtual ~ParameterList();
00049   virtual ParameterList* Clone(const void* from, void* to) const
00050   {return (ParameterList*)((char*)to + ((const char*)this-(const char*)from));}
00051 protected:
00053   ParameterList(const ParameterList& right);
00055   ParameterList& operator=(const ParameterList& right);
00057   void CopyPlistRelative(const ParameterList& right);
00058 public:
00060   virtual void InitializeParameterList() { std::cerr<<"InitializeParameterList\n";}
00061 
00063   virtual std::istream& ReadFrom( std::istream& in, bool single=false);
00065   virtual std::ostream& WriteTo( std::ostream& out , bool showhelp=false, 
00066                                  int indent=0) const;
00068   virtual int PrintHelp(const std::string& myname="") const;
00070   template<class T> int RegisterParameter(const std::string& key, T& par,
00071                                           const std::string& helptext="");
00073   template<class R, class W> 
00074   int RegisterFunction(const std::string& key,
00075                        const R& read, 
00076                        const W& write,
00077                        const std::string& helptext="");
00079   template<class R> int RegisterReadFunction(const std::string& key, 
00080                                              const R& read,
00081                                              const std::string& helptext="");
00083   template<class W> int RegisterWriteFunction(const std::string& key, 
00084                                               const W& write,
00085                                               const std::string& helptext="");
00087   VParameterNode* const GetParameter(const std::string& key);
00088 protected:
00090   typedef std::map<std::string, VParameterNode*> ParMap;
00091   ParMap _parameters;   
00092   
00094   std::vector<boost::shared_ptr<VParameterNode> > _deleter; 
00095   
00097   template<class T> 
00098   int RegisterParameterImp(const std::string& key, T& par, 
00099                            const std::string& helptext,
00100                            const boost::false_type&);
00102   template<class T>
00103   int RegisterParameterImp(const std::string& key, T& par, 
00104                            const std::string& helptext, 
00105                            const boost::true_type&);
00106 };
00107 
00108 template<class T>
00109 inline int ParameterList::RegisterParameter(const std::string& key,
00110                                             T& par,
00111                                             const std::string& helptext)
00112 {
00113   if( _parameters.find(key) != _parameters.end() ){
00114     Message(ERROR)<<"Parameter with key \""<<key
00115              <<"\" already exists in this list!"<<std::endl;
00116     return -1;
00117   }
00118   
00119   return RegisterParameterImp(key,par,helptext,
00120                               boost::is_base_of<VParameterNode,T>());
00121 }
00122 
00123 //template specialization
00124 template<class T>
00125 inline int ParameterList::RegisterParameterImp(const std::string& key, 
00126                                                T& par,
00127                                                const std::string& helptext,
00128                                                const boost::true_type&)
00129 {
00130   _parameters.insert(std::make_pair(key,&par));
00131   return 0;
00132 }
00133 
00134 template<class T> 
00135 inline int ParameterList::RegisterParameterImp(const std::string& key,
00136                                                T& par,
00137                                                const std::string& helptext,
00138                                                const boost::false_type&)
00139 {
00140   boost::shared_ptr<VParameterNode> ptr(new Parameter<T>(par, key, helptext) );
00141   _deleter.push_back(ptr);
00142   _parameters.insert(std::make_pair(key,ptr.get()));
00143   return 0;
00144 }
00145 
00146 template<class R, class W>
00147 inline int ParameterList::RegisterFunction(const std::string& key,
00148                                            const R& reader, const W& writer,
00149                                            const std::string& helptext)
00150 {
00151   if( _parameters.find(key) != _parameters.end() ){
00152     Message(ERROR)<<"Parameter with key \""<<key
00153              <<"\" already exists in this list!"<<std::endl;
00154     return -1;
00155   }
00156   
00157   boost::shared_ptr<VParameterNode> 
00158     ptr(new ConfigFunctor<R,W>(reader, writer, key, helptext));
00159   _deleter.push_back(ptr);
00160   _parameters.insert(std::make_pair(key, ptr.get()));
00161   return 0;
00162 }
00163 
00164 template<class R>
00165 inline int ParameterList::RegisterReadFunction(const std::string& key,
00166                                                const R& reader,
00167                                                const std::string& helptext)
00168 {
00169   int r = RegisterFunction(key, reader, ConfigFunctorDummyWrite(), helptext); 
00170   if(r==0)
00171     _parameters[key]->haswrite = false;
00172   return r;
00173 }
00174 
00175 template<class W>
00176 inline int ParameterList::RegisterWriteFunction(const std::string& key,
00177                                                 const W& writer,
00178                                                 const std::string& helptext)
00179 {
00180   int r = RegisterFunction(key, ConfigFunctorDummyRead(), writer, helptext);
00181   if(r == 0)
00182     _parameters[key]->hasread = false;
00183   return r;
00184 }
00185 
00186 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1