log4cpp 1.1
Loading...
Searching...
No Matches
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 <map>
12#include <string>
13#include <sstream>
14#include <stdexcept>
15#include "Portability.hh"
16
17namespace log4cpp
18{
19 class FactoryParams;
20 namespace details
21 {
23 {
24 public:
25 base_validator_data(const char* tag, const FactoryParams* params) : tag_(tag), params_(params){}
26
27 protected:
28 const char* tag_;
30
31 template<typename T>
32 void assign(const std::string& param_value, T& value) const
33 {
34 assign_impl(param_value, value);
35 }
36
37 template<typename T>
38 void assign_impl(const std::string& param_value, T& value) const
39 {
40 std::stringstream s;
41 s << param_value;
42 s >> value;
43 }
44
45 void assign_impl(const std::string& param_value, std::string& value) const
46 {
47 value = param_value;
48 }
49
50 void throw_error(const char* param_name) const
51 {
52 std::stringstream s;
53 s << "Property '" << param_name << "' required to configure " << tag_;
54 throw std::runtime_error(s.str());
55 }
56 };
57
58 class parameter_validator;
59 }
60
62 {
63 typedef std::map<std::string, std::string> storage_t;
64
65 storage_t storage_;
66
67 public:
68 typedef storage_t::const_iterator const_iterator;
69
70 const std::string& operator[](const std::string& v) const;
71 std::string& operator[](const std::string& v) { return storage_[v]; }
72 details::parameter_validator get_for(const char* tag) const;
73 const_iterator find(const std::string& t) const;
74 const_iterator begin() const { return storage_.begin(); }
75 const_iterator end() const { return storage_.end(); }
76 };
77
78 namespace details
79 {
80 class optional_params_validator;
82 {
83 public:
84 required_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
85
86#if defined(_MSC_VER) && _MSC_VER < 1300
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#else
90 template<typename T>
91 optional_params_validator optional(const char* param, T& value) const;
92#endif
93
94 template<typename T>
95 const required_params_validator& operator()(const char* param, T& value) const
96 {
98 if (i != params_->end())
99 assign(i->second, value);
100 else
101 throw_error(param);
102
103 return *this;
104 }
105
106 };
107
109 {
110 public:
111 optional_params_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
112
113 template<typename T>
114 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
115
116 template<typename T>
117 const optional_params_validator& operator()(const char* param, T& value) const
118 {
120 if (i != params_->end())
121 assign(i->second, value);
122
123 return *this;
124
125 }
126 };
127
129 {
130 public:
131 parameter_validator(const char* tag, const FactoryParams* params) : base_validator_data(tag, params) {}
132
133 template<typename T>
134 required_params_validator required(const char* param, T& value) const { required_params_validator v(tag_, params_); v(param, value); return v; }
135
136 template<typename T>
137 optional_params_validator optional(const char* param, T& value) const { optional_params_validator v(tag_, params_); v(param, value); return v; }
138 };
139
140#if !(defined(_MSC_VER) && _MSC_VER < 1300)
141 template<typename T>
142 optional_params_validator
143 required_params_validator::optional(const char* param, T& value) const
144 {
146 v(param, value);
147 return v;
148 }
149#endif
150 }
151
153 {
154 return details::parameter_validator(tag, this);
155 }
156}
157
158#endif // h_3e645482_ae6a_43e5_8f81_abbc4200212d
#define LOG4CPP_EXPORT
Definition: Export.hh:26
Definition: FactoryParams.hh:62
const_iterator find(const std::string &t) const
Definition: FactoryParams.cpp:21
const_iterator begin() const
Definition: FactoryParams.hh:74
details::parameter_validator get_for(const char *tag) const
Definition: FactoryParams.hh:152
std::string & operator[](const std::string &v)
Definition: FactoryParams.hh:71
storage_t::const_iterator const_iterator
Definition: FactoryParams.hh:68
const_iterator end() const
Definition: FactoryParams.hh:75
Definition: FactoryParams.hh:23
void assign(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:32
const FactoryParams * params_
Definition: FactoryParams.hh:29
base_validator_data(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:25
const char * tag_
Definition: FactoryParams.hh:28
void assign_impl(const std::string &param_value, std::string &value) const
Definition: FactoryParams.hh:45
void assign_impl(const std::string &param_value, T &value) const
Definition: FactoryParams.hh:38
void throw_error(const char *param_name) const
Definition: FactoryParams.hh:50
Definition: FactoryParams.hh:109
const optional_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:117
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:114
optional_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:111
Definition: FactoryParams.hh:129
required_params_validator required(const char *param, T &value) const
Definition: FactoryParams.hh:134
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:137
parameter_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:131
Definition: FactoryParams.hh:82
required_params_validator(const char *tag, const FactoryParams *params)
Definition: FactoryParams.hh:84
optional_params_validator optional(const char *param, T &value) const
Definition: FactoryParams.hh:143
const required_params_validator & operator()(const char *param, T &value) const
Definition: FactoryParams.hh:95
The top level namespace for all 'Log for C++' types and classes.
Definition: AbortAppender.hh:16