pvAccessCPP
utils/pv/configuration.h
00001 
00007 #ifndef CONFIGURATION_H
00008 #define CONFIGURATION_H
00009 
00010 #include <string.h>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <fstream>
00014 #include <map>
00015 #include <set>
00016 
00017 #ifdef epicsExportSharedSymbols
00018 #   define configurationEpicsExportSharedSymbols
00019 #   undef epicsExportSharedSymbols
00020 #endif
00021 
00022 #include <pv/pvType.h>
00023 #include <pv/noDefaultMethods.h>
00024 #include <pv/lock.h>
00025 #include <pv/sharedPtr.h>
00026 
00027 #include <envDefs.h>
00028 #ifdef configurationEpicsExportSharedSymbols
00029 #   define epicsExportSharedSymbols
00030 #   undef configurationEpicsExportSharedSymbols
00031 #endif
00032 
00033 #include <shareLib.h>
00034 
00035 union osiSockAddr; // defined in osiSock;
00036 
00037 namespace epics {
00038 namespace pvAccess {
00039 
00040 class ConfigurationStack;
00041 
00045 class epicsShareClass Configuration : private epics::pvData::NoDefaultMethods
00046 {
00047 public:
00048     POINTER_DEFINITIONS(Configuration);
00049 
00053     virtual ~Configuration() {};
00063     bool getPropertyAsBoolean(const std::string &name, const bool defaultValue) const;
00073     epics::pvData::int32 getPropertyAsInteger(const std::string &name, const epics::pvData::int32 defaultValue) const;
00083     float getPropertyAsFloat(const std::string &name, const float defaultValue) const;
00093     double getPropertyAsDouble(const std::string &name, const double defaultValue) const;
00103     std::string getPropertyAsString(const std::string &name, const std::string &defaultValue) const;
00114     bool getPropertyAsAddress(const std::string& name, osiSockAddr* addr) const;
00115 
00116     bool hasProperty(const std::string &name) const;
00117 
00118     typedef std::set<std::string> keys_t;
00122     keys_t keys() const
00123     {
00124         keys_t ret;
00125         addKeys(ret);
00126         return ret;
00127     }
00128 
00129 protected:
00130     friend class ConfigurationStack;
00131     virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const = 0;
00132     virtual void addKeys(keys_t&) const {}
00133 };
00134 
00136 class epicsShareClass ConfigurationMap: public Configuration
00137 {
00138 public:
00139     typedef std::map<std::string, std::string> properties_t;
00140     properties_t properties;
00141     ConfigurationMap() {}
00142     ConfigurationMap(const properties_t& p) :properties(p) {}
00143 private:
00144     virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
00145     virtual void addKeys(keys_t&) const;
00146 };
00147 
00149 class epicsShareClass ConfigurationEnviron: public Configuration
00150 {
00151 private:
00152     virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
00153 };
00154 
00155 typedef ConfigurationEnviron SystemConfigurationImpl;
00156 
00159 class epicsShareClass ConfigurationStack : public Configuration
00160 {
00161     typedef std::vector<std::tr1::shared_ptr<Configuration> > confs_t;
00162     confs_t confs;
00163     virtual bool tryGetPropertyAsString(const std::string& name, std::string* val) const;
00164     virtual void addKeys(keys_t&) const;
00165 public:
00166     inline void push_back(const confs_t::value_type& conf) {
00167         confs.push_back(conf);
00168     }
00169     inline confs_t::value_type pop_back() {
00170         if(confs.empty())
00171             throw std::runtime_error("Stack empty");
00172         confs_t::value_type ret(confs.back());
00173         confs.pop_back();
00174         return ret;
00175     }
00176     inline size_t size() const {
00177         return confs.size();
00178     }
00179 };
00180 
00181 struct epicsShareClass ConfigurationBuilder
00182 {
00183     ConfigurationBuilder();
00184     ConfigurationBuilder& push_env();
00185     ConfigurationBuilder& push_map();
00186     ConfigurationBuilder& push_config(const Configuration::shared_pointer&);
00187     template<typename V>
00188     ConfigurationBuilder& add(const std::string& name, const V& val)
00189     {
00190         std::ostringstream strm;
00191         strm<<val;
00192         return _add(name, strm.str());
00193     }
00194     Configuration::shared_pointer build();
00195 private:
00196     ConfigurationBuilder& _add(const std::string& name, const std::string& val);
00197     ConfigurationMap::properties_t mymap;
00198     std::tr1::shared_ptr<ConfigurationStack> stack;
00199     friend ConfigurationBuilder& operator<<(ConfigurationBuilder&, const std::string& s);
00200 };
00201 
00205 class epicsShareClass ConfigurationProvider : private epics::pvData::NoDefaultMethods
00206 {
00207 public:
00208     POINTER_DEFINITIONS(ConfigurationProvider);
00212     virtual ~ConfigurationProvider() {};
00220     virtual Configuration::shared_pointer getConfiguration(const std::string &name) = 0;
00227     virtual void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration) = 0;
00228 };
00229 
00230 class ConfigurationProviderImpl: public ConfigurationProvider
00231 {
00232 public:
00233     ConfigurationProviderImpl() {}
00237     ~ConfigurationProviderImpl() {}
00238     Configuration::shared_pointer getConfiguration(const std::string &name);
00239     void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration);
00240 private:
00241     epics::pvData::Mutex _mutex;
00242     std::map<std::string,Configuration::shared_pointer> _configs;
00243 };
00244 
00248 class epicsShareClass ConfigurationFactory : private epics::pvData::NoDefaultMethods
00249 {
00250 public:
00251     POINTER_DEFINITIONS(ConfigurationFactory);
00252 
00261     static ConfigurationProvider::shared_pointer getProvider();
00262     static void registerConfiguration(const std::string &name, Configuration::shared_pointer const & configuration)
00263     {
00264         getProvider()->registerConfiguration(name, configuration);
00265     }
00266     static Configuration::shared_pointer getConfiguration(const std::string& name)
00267     {
00268         return getProvider()->getConfiguration(name);
00269     }
00270 
00271 private:
00272     ConfigurationFactory() {};
00273 };
00274 
00275 }
00276 }
00277 
00278 #endif  /* CONFIGURATION_H */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator