pvAccessCPP  7.1.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
configuration.h
1 
7 #ifndef CONFIGURATION_H
8 #define CONFIGURATION_H
9 
10 #include <string.h>
11 #include <iostream>
12 #include <sstream>
13 #include <fstream>
14 #include <map>
15 #include <set>
16 
17 #ifdef epicsExportSharedSymbols
18 # define configurationEpicsExportSharedSymbols
19 # undef epicsExportSharedSymbols
20 #endif
21 
22 #include <pv/pvType.h>
23 #include <pv/noDefaultMethods.h>
24 #include <pv/lock.h>
25 #include <pv/sharedPtr.h>
26 
27 #include <envDefs.h>
28 #ifdef configurationEpicsExportSharedSymbols
29 # define epicsExportSharedSymbols
30 # undef configurationEpicsExportSharedSymbols
31 #endif
32 
33 #include <shareLib.h>
34 
35 union osiSockAddr; // defined in osiSock;
36 
37 namespace epics {
38 namespace pvAccess {
39 
40 class ConfigurationStack;
41 
45 class epicsShareClass Configuration
46 {
47  EPICS_NOT_COPYABLE(Configuration)
48 public:
49  POINTER_DEFINITIONS(Configuration);
50 
51  Configuration() {}
52  virtual ~Configuration() =0;
62  bool getPropertyAsBoolean(const std::string &name, const bool defaultValue) const;
72  epics::pvData::int32 getPropertyAsInteger(const std::string &name, const epics::pvData::int32 defaultValue) const;
82  float getPropertyAsFloat(const std::string &name, const float defaultValue) const;
92  double getPropertyAsDouble(const std::string &name, const double defaultValue) const;
102  std::string getPropertyAsString(const std::string &name, const std::string &defaultValue) const;
113  bool getPropertyAsAddress(const std::string& name, osiSockAddr* addr) const;
114 
115  bool hasProperty(const std::string &name) const;
116 
121  keys_t keys() const
122  {
123  keys_t ret;
124  addKeys(ret);
125  return ret;
126  }
127 
128 protected:
129  friend class ConfigurationStack;
130  virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const = 0;
131  virtual void addKeys(keys_t&) const {}
132 };
133 
135 class epicsShareClass ConfigurationMap: public Configuration
136 {
137  EPICS_NOT_COPYABLE(ConfigurationMap)
138 public:
140  properties_t properties;
141  ConfigurationMap() {}
142  ConfigurationMap(const properties_t& p) :properties(p) {}
143  virtual ~ConfigurationMap() {}
144 private:
145  virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
146  virtual void addKeys(keys_t&) const;
147 };
148 
150 class epicsShareClass ConfigurationEnviron: public Configuration
151 {
152  EPICS_NOT_COPYABLE(ConfigurationEnviron)
153 public:
155  virtual ~ConfigurationEnviron() {}
156 private:
157  virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
158 };
159 
161 
164 class epicsShareClass ConfigurationStack : public Configuration
165 {
166  EPICS_NOT_COPYABLE(ConfigurationStack)
168  confs_t confs;
169  virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
170  virtual void addKeys(keys_t&) const;
171 public:
172  ConfigurationStack() {}
173  virtual ~ConfigurationStack() {}
174  inline void push_back(const confs_t::value_type& conf) {
175  confs.push_back(conf);
176  }
177  inline confs_t::value_type pop_back() {
178  if(confs.empty())
179  throw std::runtime_error("Stack empty");
180  confs_t::value_type ret(confs.back());
181  confs.pop_back();
182  return ret;
183  }
184  inline size_t size() const {
185  return confs.size();
186  }
187 };
188 
189 struct epicsShareClass ConfigurationBuilder
190 {
191  ConfigurationBuilder();
192  ConfigurationBuilder& push_env();
193  ConfigurationBuilder& push_map();
194  ConfigurationBuilder& push_config(const Configuration::shared_pointer&);
195  template<typename V>
196  ConfigurationBuilder& add(const std::string& name, const V& val)
197  {
198  std::ostringstream strm;
199  strm<<val;
200  return _add(name, strm.str());
201  }
202  Configuration::shared_pointer build();
203 private:
204  ConfigurationBuilder& _add(const std::string& name, const std::string& val);
205  ConfigurationMap::properties_t mymap;
206  std::tr1::shared_ptr<ConfigurationStack> stack;
207  friend ConfigurationBuilder& operator<<(ConfigurationBuilder&, const std::string& s);
208 };
209 
213 class epicsShareClass ConfigurationProvider
214 {
215  EPICS_NOT_COPYABLE(ConfigurationProvider)
216 public:
217  POINTER_DEFINITIONS(ConfigurationProvider);
219  virtual ~ConfigurationProvider() {}
227  virtual Configuration::shared_pointer getConfiguration(const std::string &name) = 0;
234  virtual void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration) = 0;
235 };
236 
237 class ConfigurationProviderImpl: public ConfigurationProvider
238 {
239  EPICS_NOT_COPYABLE(ConfigurationProviderImpl)
240 public:
241  ConfigurationProviderImpl() {}
245  virtual ~ConfigurationProviderImpl() {}
246  Configuration::shared_pointer getConfiguration(const std::string &name);
247  void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration);
248 private:
249  epics::pvData::Mutex _mutex;
251 };
252 
256 class epicsShareClass ConfigurationFactory
257 {
258  EPICS_NOT_COPYABLE(ConfigurationFactory)
259 public:
260  POINTER_DEFINITIONS(ConfigurationFactory);
261 
270  static ConfigurationProvider::shared_pointer getProvider();
271  static void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration)
272  {
273  getProvider()->registerConfiguration(name, configuration);
274  }
275  static Configuration::shared_pointer getConfiguration(const std::string& name)
276  {
277  return getProvider()->getConfiguration(name);
278  }
279 
280 private:
282 };
283 
284 }
285 }
286 
287 #endif /* CONFIGURATION_H */
Lookup configuration strings from the process environment.
basic_ostream< _CharT, _Traits > & operator<<(basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Alloc > &__str)
size_t size() const
keys_t keys() const
Return a (partial) list of available key names.
Lookup configuration strings from a heap of sub-Configurations.
Lookup configuration strings from an in memory store.
__string_type str() const