Parameter.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 PARAMETER_h
00012 #define PARAMETER_h
00013 #include "VParameterNode.hh"
00014 #include "ParameterIOimpl.hh"
00015 #include "Message.hh"
00016 #include "phrase.hh"
00017 
00018 #include <stdexcept>
00019 #include <string>
00020 #include <cstdlib>
00021 #include <typeinfo>
00022 #include <ctime>
00023 #include <cstdio>
00024 
00025 
00026 
00034 class ParameterList;
00035 
00036 template<class T> class Parameter : public VParameterNode{
00037 public:
00039   Parameter(T& t, const std::string& key="", const std::string& helptext = "") :
00040     VParameterNode(key, helptext), _val(t) {_node_type = PARAMETER;}
00042   Parameter(const Parameter& right) : 
00043     VParameterNode(right._default_key, right._helptext), _val(right._val) {}
00045   virtual ~Parameter() {};
00047   Parameter& operator=(const Parameter& right){ _val=right._val; return *this;}
00049   const T& GetValue() const { return _val; }
00051   const T* GetPointer() const { return &_val; }
00053   virtual int PrintHelp(const std::string& myname="") const;
00054     
00056   virtual Parameter<T>* Clone(const void* from, void* to) const;
00057   
00059   typedef T param_type;
00060 protected:
00062   virtual std::istream& ReadFrom( std::istream& in , bool single=false);
00064   virtual std::ostream& WriteTo( std::ostream& out , bool showhelp=false, 
00065                                  int indent=0) const;
00066   
00067   //implementation of read/write is done with impls so we can overload
00068   /*std::istream& read_impl(std::istream& in, T& t){ return in>>t; }
00069   std::ostream& write_impl(std::ostream& out, T& t, 
00070                            bool showhelp=false, int indent=0){ return out<<t; }
00071   */
00072   //specific impl overload
00073   
00074 private:
00075   T& _val; 
00076 };
00077 
00078 template<class T> 
00079 inline std::istream& Parameter<T>::ReadFrom(std::istream& in, bool)
00080 {
00081   if( !ParameterIOimpl::read(in, _val) && !in.eof()){
00082     Message e(EXCEPTION);
00083     e<<"Error trying to read parameter with default key "<<_default_key<<"!\n";
00084     throw std::invalid_argument(e.str());
00085   }
00086   return in;
00087 }
00088 
00089 template<class T> 
00090 inline std::ostream& Parameter<T>::WriteTo(std::ostream& out, bool showhelp, int indent) const
00091 {
00092   return ParameterIOimpl::write(out, _val, showhelp, indent);
00093 }
00094 
00095 template<class T>
00096 inline int Parameter<T>::PrintHelp(const std::string& myname) const
00097 {
00098   VParameterNode::PrintHelp(myname);
00099   std::cout<<"Parameter type: "<<typeid(_val).name()<<"\n"
00100            <<"Current Value:  ";
00101   ParameterIOimpl::write(std::cout, _val, true);
00102   std::cout<<std::endl;
00103   std::string dummy;
00104   std::cout<<"\nHit <enter> to continue.";
00105   std::getline(std::cin, dummy);
00106   std::getline(std::cin, dummy);
00107   
00108   return 0;
00109 }
00110 
00111 template<class T> inline 
00112 Parameter<T>* Parameter<T>::Clone(const void* from, void* to) const
00113 {
00114   unsigned diff = (const char*)(GetPointer()) - (const char*)from;
00115   return new Parameter<T>((T&)(*( (char*)to+diff )), _default_key, _helptext); 
00116 }
00117 
00118 
00119 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1