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 "Base/GCException.h"
00005 #include <map>
00006 #include <string>
00007 #include <sstream>
00008 #include <stdexcept>
00009 #include "Portability.hh"
00010
00011 LOG4CPP_NS_BEGIN
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
00048 throw RUNTIME_EXCEPTION("%s", s.str().c_str());
00049 }
00050 };
00051
00052 class optional_params_validator;
00053 class required_params_validator : public base_validator_data
00054 {
00055 public:
00056 required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00057
00058 template<typename T>
00059 optional_params_validator optional(const char* param, T& value) const;
00060
00061 template<typename T>
00062 const required_params_validator& operator()(const char* param, T& value) const;
00063 };
00064
00065 class optional_params_validator : public base_validator_data
00066 {
00067 public:
00068 optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00069
00070 template<typename T>
00071 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00072
00073 template<typename T>
00074 const optional_params_validator& operator()(const char* param, T& value) const;
00075 };
00076
00077 template<typename T>
00078 optional_params_validator required_params_validator::optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00079
00080 class parameter_validator : public base_validator_data
00081 {
00082 public:
00083 parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00084
00085 template<typename T>
00086 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00087
00088 template<typename T>
00089 optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00090 };
00091 }
00092
00093 class LOG4CPP_EXPORT FactoryParams
00094 {
00095 typedef std::map<std::string, std::string> storage_t;
00096
00097 storage_t storage_;
00098
00099 public:
00100 typedef storage_t::const_iterator const_iterator;
00101
00102 const std::string& operator[](const std::string& v) const;
00103 std::string& operator[](const std::string& v) { return storage_[v]; }
00104 details::parameter_validator get_for(const char* tag) const { return details::parameter_validator(tag, this); }
00105 const_iterator find(const std::string& t) const;
00106 const_iterator begin() const { return storage_.begin(); }
00107 const_iterator end() const { return storage_.end(); }
00108
00109 private:
00110
00111
00112
00113 };
00114
00115 namespace details
00116 {
00117 template<typename T>
00118 const required_params_validator& required_params_validator::operator()(const char* param, T& value) const
00119 {
00120 FactoryParams::const_iterator i = params_->find(param);
00121 if (i != params_->end())
00122 assign(i->second, value);
00123 else
00124 throw_error(param);
00125
00126 return *this;
00127 }
00128
00129 template<typename T>
00130 const optional_params_validator& optional_params_validator::operator()(const char* param, T& value) const
00131 {
00132 FactoryParams::const_iterator i = params_->find(param);
00133 if (i != params_->end())
00134 assign(i->second, value);
00135
00136 return *this;
00137 }
00138 }
00139 LOG4CPP_NS_END
00140
00141 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d