log4cpp  1.1.6
FactoryParams.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
3  * Copyright 2002, Bastiaan Bakker. All rights reserved.
4  *
5  * See the COPYING file for the terms of usage and distribution.
6  */
7 
8 #if !defined(h_3e645482_ae6a_43e5_8f81_abbc4200212d)
9 #define h_3e645482_ae6a_43e5_8f81_abbc4200212d
10 
11 #include "Portability.hh"
12 #include <map>
13 #include <sstream>
14 #include <stdexcept>
15 #include <string>
16 
17 namespace log4cpp {
18  class FactoryParams;
19  namespace details {
21  public:
22  base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params) {}
23 
24  protected:
25  const char* tag_;
27 
28  template <typename T> void assign(const std::string& param_value, T& value) const {
29  assign_impl(param_value, value);
30  }
31 
32  template <typename T> void assign_impl(const std::string& param_value, T& value) const {
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  value = param_value;
40  }
41 
42  void throw_error(const char* param_name) const {
43  std::stringstream s;
44  s << "Property '" << param_name << "' required to configure " << tag_;
45  throw std::runtime_error(s.str());
46  }
47  };
48 
49  class parameter_validator;
50  } // namespace details
51 
53  typedef std::map<std::string, std::string> storage_t;
54 
55  storage_t storage_;
56 
57  public:
58  typedef storage_t::const_iterator const_iterator;
59 
60  const std::string& operator[](const std::string& v) const;
61  std::string& operator[](const std::string& v) {
62  return storage_[v];
63  }
64  details::parameter_validator get_for(const char* tag) const;
65  const_iterator find(const std::string& t) const;
67  return storage_.begin();
68  }
69  const_iterator end() const {
70  return storage_.end();
71  }
72  };
73 
74  namespace details {
75  class optional_params_validator;
77  public:
78  required_params_validator(const char* tag, const FactoryParams* params)
79  : base_validator_data(tag, params) {}
80 
81 #if defined(_MSC_VER) && _MSC_VER < 1300
82  template <typename T> optional_params_validator optional(const char* param, T& value) const {
84  v(param, value);
85  return v;
86  }
87 #else
88  template <typename T> optional_params_validator optional(const char* param, T& value) const;
89 #endif
90 
91  template <typename T> const required_params_validator& operator()(const char* param, T& value) const {
93  if (i != params_->end())
94  assign(i->second, value);
95  else
96  throw_error(param);
97 
98  return *this;
99  }
100  };
101 
103  public:
104  optional_params_validator(const char* tag, const FactoryParams* params)
105  : base_validator_data(tag, params) {}
106 
107  template <typename T> required_params_validator required(const char* param, T& value) const {
109  v(param, value);
110  return v;
111  }
112 
113  template <typename T> const optional_params_validator& operator()(const char* param, T& value) const {
115  if (i != params_->end())
116  assign(i->second, value);
117 
118  return *this;
119  }
120  };
121 
123  public:
124  parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
125 
126  template <typename T> required_params_validator required(const char* param, T& value) const {
128  v(param, value);
129  return v;
130  }
131 
132  template <typename T> optional_params_validator optional(const char* param, T& value) const {
134  v(param, value);
135  return v;
136  }
137  };
138 
139 #if !(defined(_MSC_VER) && _MSC_VER < 1300)
140  template <typename T>
143  v(param, value);
144  return v;
145  }
146 #endif
147  } // namespace details
148 
149  inline details::parameter_validator FactoryParams::get_for(const char* tag) const {
150  return details::parameter_validator(tag, this);
151  }
152 } // namespace log4cpp
153 
154 #endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d
Definition: FactoryParams.hh:20
details::parameter_validator get_for(const char *tag) const
Definition: FactoryParams.hh:149
std::string & operator[](const std::string &v)
Definition: FactoryParams.hh:61
const optional_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:113
Definition: FactoryParams.hh:102
void assign(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:28
Definition: FactoryParams.hh:122
optional_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:104
void assign_impl(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:32
storage_t::const_iterator const_iterator
Definition: FactoryParams.hh:58
Definition: FactoryParams.hh:76
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:141
#define LOG4CPP_EXPORT
Definition: Export.hh:26
Definition: FactoryParams.hh:52
The top level namespace for all &#39;Log for C++&#39; types and classes.
Definition: AbortAppender.hh:16
const required_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:91
void assign_impl(const std::string &param_value, std::string &value) const
Definition: FactoryParams.hh:38
const_iterator end() const
Definition: FactoryParams.hh:69
const char * tag_
Definition: FactoryParams.hh:25
base_validator_data(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:22
void throw_error(const char *param_name) const
Definition: FactoryParams.hh:42
parameter_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:124
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:126
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:107
const FactoryParams * params_
Definition: FactoryParams.hh:26
required_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:78
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:132
const_iterator begin() const
Definition: FactoryParams.hh:66
const_iterator find(const std::string &t) const
Definition: FactoryParams.cpp:19