log4cpp  1.1
 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 parameter_validator;
52  }
53 
55  {
56  typedef std::map<std::string, std::string> storage_t;
57 
58  storage_t storage_;
59 
60  public:
61  typedef storage_t::const_iterator const_iterator;
62 
63  const std::string& operator[](const std::string& v) const;
64  std::string& operator[](const std::string& v) { return storage_[v]; }
65  details::parameter_validator get_for(const char* tag) const;
66  const_iterator find(const std::string& t) const;
67  const_iterator begin() const { return storage_.begin(); }
68  const_iterator end() const { return storage_.end(); }
69  };
70 
71  namespace details
72  {
73  class optional_params_validator;
75  {
76  public:
77  required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
78 
79 #if defined(_MSC_VER) && _MSC_VER < 1300
80  template<typename T>
81  optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
82 #else
83  template<typename T>
84  optional_params_validator optional(const char* param, T& value) const;
85 #endif
86 
87  template<typename T>
88  const required_params_validator& operator()(const char* param, T& value) const
89  {
91  if (i != params_->end())
92  assign(i->second, value);
93  else
94  throw_error(param);
95 
96  return *this;
97  }
98 
99  };
100 
102  {
103  public:
104  optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
105 
106  template<typename T>
107  required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
108 
109  template<typename T>
110  const optional_params_validator& operator()(const char* param, T& value) const
111  {
113  if (i != params_->end())
114  assign(i->second, value);
115 
116  return *this;
117 
118  }
119  };
120 
122  {
123  public:
124  parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
125 
126  template<typename T>
127  required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
128 
129  template<typename T>
130  optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
131  };
132 
133 #if !(defined(_MSC_VER) && _MSC_VER < 1300)
134  template<typename T>
135  optional_params_validator
136  required_params_validator::optional(const char* param, T& value) const
137  {
139  v(param, value);
140  return v;
141  }
142 #endif
143  }
144 
146  {
147  return details::parameter_validator(tag, this);
148  }
149 }
150 
151 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:130
Definition: FactoryParams.hh:15
std::string & operator[](const std::string &v)
Definition: FactoryParams.hh:64
Definition: FactoryParams.hh:101
Definition: FactoryParams.hh:121
optional_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:104
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:107
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:127
storage_t::const_iterator const_iterator
Definition: FactoryParams.hh:61
Definition: FactoryParams.hh:74
const required_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:88
const optional_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:110
void assign_impl(const std::string &param_value, std::string &value) const
Definition: FactoryParams.hh:38
#define LOG4CPP_EXPORT
Definition: Export.hh:19
Definition: FactoryParams.hh:54
const_iterator begin() const
Definition: FactoryParams.hh:67
The top level namespace for all 'Log for C++' types and classes.
Definition: AbortAppender.hh:16
const_iterator end() const
Definition: FactoryParams.hh:68
const char * tag_
Definition: FactoryParams.hh:21
base_validator_data(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:18
parameter_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:124
void assign(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:25
void throw_error(const char *param_name) const
Definition: FactoryParams.hh:43
const FactoryParams * params_
Definition: FactoryParams.hh:22
details::parameter_validator get_for(const char *tag) const
Definition: FactoryParams.hh:145
const_iterator find(const std::string &t) const
Definition: FactoryParams.cpp:15
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:136
required_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:77
void assign_impl(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:31