log4cpp 1.1
|
00001 /* 00002 * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved. 00003 * Copyright 2002, Bastiaan Bakker. All rights reserved. 00004 * 00005 * See the COPYING file for the terms of usage and distribution. 00006 */ 00007 00008 #if !defined(h_3e645482_ae6a_43e5_8f81_abbc4200212d) 00009 #define h_3e645482_ae6a_43e5_8f81_abbc4200212d 00010 00011 #include <map> 00012 #include <string> 00013 #include <sstream> 00014 #include <stdexcept> 00015 #include "Portability.hh" 00016 00017 namespace log4cpp 00018 { 00019 class FactoryParams; 00020 namespace details 00021 { 00022 class base_validator_data 00023 { 00024 public: 00025 base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params){} 00026 00027 protected: 00028 const char* tag_; 00029 const FactoryParams* params_; 00030 00031 template<typename T> 00032 void assign(const std::string& param_value, T& value) const 00033 { 00034 assign_impl(param_value, value); 00035 } 00036 00037 template<typename T> 00038 void assign_impl(const std::string& param_value, T& value) const 00039 { 00040 std::stringstream s; 00041 s << param_value; 00042 s >> value; 00043 } 00044 00045 void assign_impl(const std::string& param_value, std::string& value) const 00046 { 00047 value = param_value; 00048 } 00049 00050 void throw_error(const char* param_name) const 00051 { 00052 std::stringstream s; 00053 s << "Property '" << param_name << "' required to configure " << tag_; 00054 throw std::runtime_error(s.str()); 00055 } 00056 }; 00057 00058 class parameter_validator; 00059 } 00060 00061 class LOG4CPP_EXPORT FactoryParams 00062 { 00063 typedef std::map<std::string, std::string> storage_t; 00064 00065 storage_t storage_; 00066 00067 public: 00068 typedef storage_t::const_iterator const_iterator; 00069 00070 const std::string& operator[](const std::string& v) const; 00071 std::string& operator[](const std::string& v) { return storage_[v]; } 00072 details::parameter_validator get_for(const char* tag) const; 00073 const_iterator find(const std::string& t) const; 00074 const_iterator begin() const { return storage_.begin(); } 00075 const_iterator end() const { return storage_.end(); } 00076 }; 00077 00078 namespace details 00079 { 00080 class optional_params_validator; 00081 class required_params_validator : public base_validator_data 00082 { 00083 public: 00084 required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {} 00085 00086 #if defined(_MSC_VER) && _MSC_VER < 1300 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 #else 00090 template<typename T> 00091 optional_params_validator optional(const char* param, T& value) const; 00092 #endif 00093 00094 template<typename T> 00095 const required_params_validator& operator()(const char* param, T& value) const 00096 { 00097 FactoryParams::const_iterator i = params_->find(param); 00098 if (i != params_->end()) 00099 assign(i->second, value); 00100 else 00101 throw_error(param); 00102 00103 return *this; 00104 } 00105 00106 }; 00107 00108 class optional_params_validator : public base_validator_data 00109 { 00110 public: 00111 optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {} 00112 00113 template<typename T> 00114 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; } 00115 00116 template<typename T> 00117 const optional_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 00123 return *this; 00124 00125 } 00126 }; 00127 00128 class parameter_validator : public base_validator_data 00129 { 00130 public: 00131 parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {} 00132 00133 template<typename T> 00134 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; } 00135 00136 template<typename T> 00137 optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; } 00138 }; 00139 00140 #if !(defined(_MSC_VER) && _MSC_VER < 1300) 00141 template<typename T> 00142 optional_params_validator 00143 required_params_validator::optional(const char* param, T& value) const 00144 { 00145 optional_params_validator v(tag_, params_); 00146 v(param, value); 00147 return v; 00148 } 00149 #endif 00150 } 00151 00152 inline details::parameter_validator FactoryParams::get_for(const char* tag) const 00153 { 00154 return details::parameter_validator(tag, this); 00155 } 00156 } 00157 00158 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d