pvAccessCPP  7.1.6
configuration.h
1 /**
2  * Copyright - See the COPYRIGHT that is included with this distribution.
3  * pvAccessCPP is distributed subject to a Software License Agreement found
4  * in file LICENSE that is included with this distribution.
5  */
6 
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 
42 /**
43  * Configuration
44  */
45 class epicsShareClass Configuration
46 {
48 public:
50 
51  Configuration() {}
52  virtual ~Configuration() =0;
53  /**
54  * Get the environment variable specified by name or return default value
55  * if it does not exist.
56  *
57  * @param name name of the environment variable to return.
58  * @param defualtValue default value to return if environment variable does not exists.
59  *
60  * @return environment variable value as bool or default value if it does not exist.
61  */
62  bool getPropertyAsBoolean(const std::string &name, const bool defaultValue) const;
63  /**
64  * Get the environment variable specified by name or return default value
65  * if it does not exist.
66  *
67  * @param name name of the environment variable to return.
68  * @param defualtValue default value to return if environment variable does not exists.
69  *
70  * @return environment variable value as int32 or default value if it does not exist.
71  */
73  /**
74  * Get the environment variable specified by name or return default value
75  * if it does not exist.
76  *
77  * @param name name of the environment variable to return.
78  * @param defualtValue default value to return if environment variable does not exists.
79  *
80  * @return environment variable value as float or default value if it does not exist.
81  */
82  float getPropertyAsFloat(const std::string &name, const float defaultValue) const;
83  /**
84  * Get the environment variable specified by name or return default value
85  * if it does not exist.
86  *
87  * @param name name of the environment variable to return.
88  * @param defualtValue default value to return if environment variable does not exists.
89  *
90  * @return environment variable value as double or default value if it does not exist.
91  */
92  double getPropertyAsDouble(const std::string &name, const double defaultValue) const;
93  /**
94  * Get the environment variable specified by name or return default value
95  * if it does not exist.
96  *
97  * @param name name of the environment variable to return.
98  * @param defualtValue default value to return if environment variable does not exists.
99  *
100  * @return environment variable value as std::string or default value if it does not exist.
101  */
103  /**
104  * Fetch and parse as a socket address and port number (address family set accordingly).
105  * At present only numeric addresses are parsed (eg. "127.0.0.1:4242").
106  *
107  * The storage pointed to be addr should be initialized with a default value, or zeroed.
108  *
109  * @param name name of the environment variable to return.
110  * @pram addr pointer to the address struct to be filled in
111  * @return true if addr now contains an address, false otherwise
112  */
113  bool getPropertyAsAddress(const std::string& name, osiSockAddr* addr) const;
114 
115  bool hasProperty(const std::string &name) const;
116 
117  typedef std::set<std::string> keys_t;
118  /** Return a (partial) list of available key names.
119  * Does not include key names from the ConfigurationEnviron
120  */
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 
134 //! Lookup configuration strings from an in memory store
135 class epicsShareClass ConfigurationMap: public Configuration
136 {
138 public:
139  typedef std::map<std::string, std::string> properties_t;
141  ConfigurationMap() {}
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 
149 //! Lookup configuration strings from the process environment
150 class epicsShareClass ConfigurationEnviron: public Configuration
151 {
153 public:
155  virtual ~ConfigurationEnviron() {}
156 private:
157  virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
158 };
159 
160 typedef ConfigurationEnviron SystemConfigurationImpl;
161 
162 //! Lookup configuration strings from a heap of sub-Configurations.
163 //! Most recently push'd is checked first.
164 class epicsShareClass ConfigurationStack : public Configuration
165 {
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) {
176  }
177  inline confs_t::value_type pop_back() {
178  if(confs.empty())
179  throw std::runtime_error("Stack empty");
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 {
195  template<typename V>
196  ConfigurationBuilder& add(const std::string& name, const V& val)
197  {
199  strm<<val;
200  return _add(name, strm.str());
201  }
203 private:
204  ConfigurationBuilder& _add(const std::string& name, const std::string& val);
208 };
209 
210 /**
211  * Configuration provider.
212  */
213 class epicsShareClass ConfigurationProvider
214 {
216 public:
219  virtual ~ConfigurationProvider() {}
220  /**
221  * Return configuration specified by name.
222  *
223  * @param name name of the configuration to return.
224  *
225  * @return configuration specified by name or NULL if it does not exists.
226  */
228  /**
229  * Register configuration.
230  *
231  * @param name name of the configuration to register.
232  * @param configuration configuration to register.
233  */
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() {}
242  /**
243  * Destructor. Note: Registered configurations will be deleted!!
244  */
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;
250  std::map<std::string,Configuration::shared_pointer> _configs;
251 };
252 
253 /**
254  * Configuration factory.
255  */
256 class epicsShareClass ConfigurationFactory
257 {
259 public:
261 
262  /**
263  * Lazily creates configuration provider.
264  *
265  * @param name name of the configuration to register.
266  * @param configuration configuration to register.
267  *
268  * @return configuration provider
269  */
272  {
274  }
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.
Lookup configuration strings from a heap of sub-Configurations.
virtual void authNZMessage(epics::pvData::PVStructure::shared_pointer const &data)=0
Pass data to the active security plug-in session.
Lookup configuration strings from an in memory store.