FactoryParams.hh
Go to the documentation of this file.
00001 #if !defined(h_3e645482_ae6a_43e5_8f81_abbc4200212d)
00002 #define h_3e645482_ae6a_43e5_8f81_abbc4200212d
00003 
00004 #include <map>
00005 #include <string>
00006 #include <sstream>
00007 #include <stdexcept>
00008 #include "Portability.hh"
00009 
00010 namespace log4cpp
00011 {
00012    class FactoryParams;
00013    namespace details
00014    {
00015       class base_validator_data
00016       {
00017          public:
00018             base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params){}
00019 
00020          protected:
00021             const char* tag_;
00022             const FactoryParams* params_;
00023 
00024             template<typename T>
00025             void assign(const std::string& param_value, T& value) const
00026             {
00027                assign_impl(param_value, value);
00028             }
00029 
00030             template<typename T>
00031             void assign_impl(const std::string& param_value, T& value) const
00032             {
00033                std::stringstream s;
00034                s << param_value;
00035                s >> value;
00036             }
00037 
00038             void assign_impl(const std::string& param_value, std::string& value) const
00039             {
00040                value = param_value;
00041             }
00042 
00043             void throw_error(const char* param_name) const
00044             {
00045                std::stringstream s;
00046                s << "Property '" << param_name << "' required to configure " << tag_;
00047                throw std::runtime_error(s.str());
00048             }
00049       };
00050 
00051       class optional_params_validator;
00052       class required_params_validator : public base_validator_data
00053       {
00054          public:
00055             required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00056 
00057             template<typename T>
00058             optional_params_validator optional(const char* param, T& value) const;
00059             
00060             template<typename T>
00061             const required_params_validator& operator()(const char* param, T& value) const;
00062       };
00063       
00064       class optional_params_validator : public base_validator_data
00065       {
00066          public:
00067             optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00068 
00069             template<typename T>
00070             required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00071 
00072             template<typename T>
00073             const optional_params_validator& operator()(const char* param, T& value) const;
00074       };
00075 
00076       template<typename T>
00077       optional_params_validator required_params_validator::optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00078 
00079       class parameter_validator : public base_validator_data
00080       {
00081          public:
00082             parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00083 
00084             template<typename T>
00085             required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00086 
00087             template<typename T>
00088             optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00089       };
00090    }
00091 
00092    class LOG4CPP_EXPORT FactoryParams
00093    {
00094          typedef std::map<std::string, std::string> storage_t;
00095                  
00096                                  storage_t storage_;
00097       
00098       public:
00099          typedef storage_t::const_iterator const_iterator;
00100 
00101          const std::string& operator[](const std::string& v) const;
00102          std::string& operator[](const std::string& v) { return storage_[v]; }
00103          details::parameter_validator get_for(const char* tag) const { return details::parameter_validator(tag, this); }
00104          const_iterator find(const std::string& t) const;
00105          const_iterator begin() const { return storage_.begin(); }
00106          const_iterator end() const { return storage_.end(); }
00107 
00108       private:
00109          /*typedef std::map<std::string, std::string> storage_t;
00110 
00111          storage_t storage_; */
00112    };
00113 
00114    namespace details
00115    {
00116       template<typename T>
00117       const required_params_validator& required_params_validator::operator()(const char* param, T& value) const
00118       {
00119          FactoryParams::const_iterator i = params_->find(param);
00120          if (i != params_->end())
00121             assign(i->second, value);
00122          else
00123             throw_error(param);
00124 
00125          return *this;
00126       }
00127       
00128       template<typename T>
00129       const optional_params_validator& optional_params_validator::operator()(const char* param, T& value) const
00130       {
00131          FactoryParams::const_iterator i = params_->find(param);
00132          if (i != params_->end())
00133             assign(i->second, value);
00134 
00135          return *this;
00136       }
00137    }
00138 }
00139 
00140 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung.
autogenerated on Thu Jan 2 2014 11:35:02