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 parameter_validator;
00052 }
00053
00054 class LOG4CPP_EXPORT FactoryParams
00055 {
00056 typedef std::map<std::string, std::string> storage_t;
00057
00058 storage_t storage_;
00059
00060 public:
00061 typedef storage_t::const_iterator const_iterator;
00062
00063 const std::string& operator[](const std::string& v) const;
00064 std::string& operator[](const std::string& v) { return storage_[v]; }
00065 details::parameter_validator get_for(const char* tag) const;
00066 const_iterator find(const std::string& t) const;
00067 const_iterator begin() const { return storage_.begin(); }
00068 const_iterator end() const { return storage_.end(); }
00069 };
00070
00071 namespace details
00072 {
00073 class optional_params_validator;
00074 class required_params_validator : public base_validator_data
00075 {
00076 public:
00077 required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00078
00079 #if defined(_MSC_VER) && _MSC_VER < 1300
00080 template<typename T>
00081 optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00082 #else
00083 template<typename T>
00084 optional_params_validator optional(const char* param, T& value) const;
00085 #endif
00086
00087 template<typename T>
00088 const required_params_validator& operator()(const char* param, T& value) const
00089 {
00090 FactoryParams::const_iterator i = params_->find(param);
00091 if (i != params_->end())
00092 assign(i->second, value);
00093 else
00094 throw_error(param);
00095
00096 return *this;
00097 }
00098
00099 };
00100
00101 class optional_params_validator : public base_validator_data
00102 {
00103 public:
00104 optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00105
00106 template<typename T>
00107 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00108
00109 template<typename T>
00110 const optional_params_validator& operator()(const char* param, T& value) const
00111 {
00112 FactoryParams::const_iterator i = params_->find(param);
00113 if (i != params_->end())
00114 assign(i->second, value);
00115
00116 return *this;
00117
00118 }
00119 };
00120
00121 class parameter_validator : public base_validator_data
00122 {
00123 public:
00124 parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
00125
00126 template<typename T>
00127 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
00128
00129 template<typename T>
00130 optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
00131 };
00132
00133 #if !(defined(_MSC_VER) && _MSC_VER < 1300)
00134 template<typename T>
00135 optional_params_validator
00136 required_params_validator::optional(const char* param, T& value) const
00137 {
00138 optional_params_validator v(tag_, params_);
00139 v(param, value);
00140 return v;
00141 }
00142 #endif
00143 }
00144
00145 inline details::parameter_validator FactoryParams::get_for(const char* tag) const
00146 {
00147 return details::parameter_validator(tag, this);
00148 }
00149 }
00150
00151 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d