libmoost
/home/mhx/git/github/libmoost/include/moost/configurable/binder.h
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_CONFIGURABLE_BINDER_H__
00029 #define MOOST_CONFIGURABLE_BINDER_H__
00030 
00031 #include <vector>
00032 #include <string>
00033 #include <istream>
00034 #include <ostream>
00035 #include <map>
00036 #include <stdexcept>
00037 
00038 #include <boost/noncopyable.hpp>
00039 
00040 #include "configurable.h"
00041 #include "binding.hpp"
00042 
00043 namespace moost { namespace configurable {
00044 
00050 // TODO: is there a clever way to do copy ctor?
00051 class binder : public configurable, boost::noncopyable
00052 {
00053 private:
00054 
00055   typedef std::map<std::string, persistable * > route_map;
00056 
00057   route_map m_routes;
00058 
00059   std::vector< persistable * > m_bindings;
00060 
00061 protected:
00062 
00064   void child(const std::string & key, persistable & value);
00065 
00067   template<typename T>
00068   void bind(const std::string & key, T & value);
00069 
00071   template<typename T>
00072   void bind(const std::string & key, T & value, const T & default_value);
00073 
00074 public:
00075 
00077   binder() {}
00078 
00080   virtual ~binder();
00081 
00083   void read(std::istream & source);
00084 
00086   void write(std::ostream & dest, int indent = 0) const;
00087 
00089   void set(const std::string & key, const std::string & value);
00090 
00092   void get(const std::string & key, std::string & value) const;
00093 
00095   void list(std::vector< std::pair< std::string, std::string > > & items);
00096 
00098   void set_default();
00099 };
00100 
00101 template<typename T>
00102 void binder::bind(const std::string & key, T & value)
00103 {
00104   m_bindings.push_back(new binding<T>(value));
00105   m_routes[key] = m_bindings.back();
00106 }
00107 
00108 template<typename T>
00109 void binder::bind(const std::string & key, T & value, const T & default_value)
00110 {
00111   m_bindings.push_back(new binding<T>(value, default_value));
00112   m_routes[key] = m_bindings.back();
00113 }
00114 
00115 }}
00116 
00117 #endif // MOOST_CONFIGURABLE_BINDER_H__