log4cpp  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FactoryParams.hh
Go to the documentation of this file.
1 #if !defined(h_3e645482_ae6a_43e5_8f81_abbc4200212d)
2 #define h_3e645482_ae6a_43e5_8f81_abbc4200212d
3 
4 #include <map>
5 #include <string>
6 #include <sstream>
7 #include <stdexcept>
8 #include "Portability.hh"
9 
10 namespace log4cpp
11 {
12  class FactoryParams;
13  namespace details
14  {
16  {
17  public:
18  base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params){}
19 
20  protected:
21  const char* tag_;
23 
24  template<typename T>
25  void assign(const std::string& param_value, T& value) const
26  {
27  assign_impl(param_value, value);
28  }
29 
30  template<typename T>
31  void assign_impl(const std::string& param_value, T& value) const
32  {
33  std::stringstream s;
34  s << param_value;
35  s >> value;
36  }
37 
38  void assign_impl(const std::string& param_value, std::string& value) const
39  {
40  value = param_value;
41  }
42 
43  void throw_error(const char* param_name) const
44  {
45  std::stringstream s;
46  s << "Property '" << param_name << "' required to configure " << tag_;
47  throw std::runtime_error(s.str());
48  }
49  };
50 
51  class optional_params_validator;
53  {
54  public:
55  required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
56 
57  template<typename T>
58  optional_params_validator optional(const char* param, T& value) const;
59 
60  template<typename T>
61  const required_params_validator& operator()(const char* param, T& value) const;
62  };
63 
65  {
66  public:
67  optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
68 
69  template<typename T>
70  required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
71 
72  template<typename T>
73  const optional_params_validator& operator()(const char* param, T& value) const;
74  };
75 
76  template<typename T>
77  optional_params_validator required_params_validator::optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
78 
80  {
81  public:
82  parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
83 
84  template<typename T>
85  required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
86 
87  template<typename T>
88  optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
89  };
90  }
91 
93  {
94  typedef std::map<std::string, std::string> storage_t;
95 
96  storage_t storage_;
97 
98  public:
99  typedef storage_t::const_iterator const_iterator;
100 
101  const std::string& operator[](const std::string& v) const;
102  std::string& operator[](const std::string& v) { return storage_[v]; }
103  details::parameter_validator get_for(const char* tag) const { return details::parameter_validator(tag, this); }
104  const_iterator find(const std::string& t) const;
105  const_iterator begin() const { return storage_.begin(); }
106  const_iterator end() const { return storage_.end(); }
107 
108  private:
109  /*typedef std::map<std::string, std::string> storage_t;
110 
111  storage_t storage_; */
112  };
113 
114  namespace details
115  {
116  template<typename T>
117  const required_params_validator& required_params_validator::operator()(const char* param, T& value) const
118  {
120  if (i != params_->end())
121  assign(i->second, value);
122  else
123  throw_error(param);
124 
125  return *this;
126  }
127 
128  template<typename T>
129  const optional_params_validator& optional_params_validator::operator()(const char* param, T& value) const
130  {
132  if (i != params_->end())
133  assign(i->second, value);
134 
135  return *this;
136  }
137  }
138 }
139 
140 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d