psana/include/Configurable.h

Go to the documentation of this file.
00001 #ifndef PSANA_CONFIGURABLE_H
00002 #define PSANA_CONFIGURABLE_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: Configurable.h 10071 2015-05-12 23:28:27Z davidsch@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class Configurable.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 #include <string>
00017 #include <vector>
00018 
00019 //----------------------
00020 // Base Class Headers --
00021 //----------------------
00022 
00023 //-------------------------------
00024 // Collaborating Class Headers --
00025 //-------------------------------
00026 #include "ConfigSvc/ConfigSvc.h"
00027 #include "PSEvt/Source.h"
00028 #include "psana/Context.h"
00029 
00030 //------------------------------------
00031 // Collaborating Class Declarations --
00032 //------------------------------------
00033 
00034 //              ---------------------
00035 //              -- Class Interface --
00036 //              ---------------------
00037 
00038 namespace psana {
00039 
00040 /**
00041  *  @ingroup psana
00042  *  
00043  *  @brief Class that provides a simplified interface to a framework's 
00044  *  configuration service. 
00045  *  
00046  *  This object can be used either as a base class or a member of other 
00047  *  classes. It does not define any virtual methods (even destructor)
00048  *  so make sure that classes which inherit it do it properly.
00049  *
00050  *  This software was developed for the LCLS project.  If you use all or 
00051  *  part of it, please give an appropriate acknowledgment.
00052  *
00053  *  @see ConfigSvc::ConfigSvc
00054  *
00055  *  @version \$Id: Configurable.h 10071 2015-05-12 23:28:27Z davidsch@SLAC.STANFORD.EDU $
00056  *
00057  *  @author Andrei Salnikov
00058  */
00059 
00060 class Configurable  {
00061 public:
00062 
00063   /**
00064    *  @brief Constructor takes a name.
00065    *  
00066    *  It accept names in the format "Package.ClassName" or "Package.ClassName:InstanceName".
00067    *  The configuration will be read from a section corresponding to this name if present
00068    *  of from a section without instance name.
00069    *  
00070    *  @param[in] name Name of this configurable.
00071    */
00072   Configurable (const std::string& name) ;
00073 
00074   // Destructor
00075   ~Configurable();
00076 
00077   /// returns instance of configuration service
00078   ConfigSvc::ConfigSvc configSvc() const
00079   {
00080     return ConfigSvc::ConfigSvc(m_context);
00081   }
00082 
00083   /**
00084    *  @brief Get the value of a single parameter, this method can be used for numeric types only.
00085    *  
00086    *  @param[in] param  Name of the parameter  
00087    *  @return Parameter value
00088    *  
00089    *  @throw ConfigSvc::ExceptionMissing thrown if parameter or section is not found
00090    */
00091   ConfigSvc::ConfigSvc::Result config(const std::string& param) const
00092   {
00093     ConfigSvc::ConfigSvc cfg(m_context);
00094     try {
00095       return cfg.get(name(), param);
00096     } catch (const ConfigSvc::ExceptionMissing& ex) {
00097       return cfg.get(className(), param);
00098     }
00099   }
00100 
00101   /**
00102    *  @brief Get the value of a single parameter as a string.
00103    *  
00104    *  @param[in] param  Name of the parameter  
00105    *  @return Parameter value
00106    *  
00107    *  @throw ConfigSvc::ExceptionMissing thrown if parameter or section is not found
00108    */
00109   std::string configStr(const std::string& param) const
00110   {
00111     ConfigSvc::ConfigSvc cfg(m_context);
00112     try {
00113       return cfg.getStr(name(), param);
00114     } catch (const ConfigSvc::ExceptionMissing& ex) {
00115       return cfg.getStr(className(), param);
00116     }
00117   }
00118 
00119   /**
00120    *  @brief Get the value of a single parameter as a Source object.
00121    *
00122    *  @param[in] param  Name of the parameter
00123    *  @return Parameter value
00124    *
00125    *  @throw ConfigSvc::ExceptionMissing thrown if parameter or section is not found
00126    */
00127   PSEvt::Source configSrc(const std::string& param) const
00128   {
00129     return PSEvt::Source(configStr(param));
00130   }
00131 
00132   /**
00133    *  @brief Get the value of a single parameter, this method can be used for numeric types only.
00134    *  
00135    *  Returns default value if parameter is not found. 
00136    *  
00137    *  @param[in] param  Name of the parameter  
00138    *  @param[in] def    Default value to return if parameter is not there  
00139    *  @return Parameter value or default value
00140    */
00141   template <typename T>
00142   ConfigSvc::ConfigSvc::ResultDef<T> config(const std::string& param, const T& def) const
00143   {
00144     ConfigSvc::ConfigSvc cfg(m_context);
00145     ConfigSvc::ConfigSvc::ResultDef<T> res(cfg.get(name(), param, def));
00146     if (not res.isDefault()) return res;
00147     return cfg.get<T>(className(), param, def);
00148   }
00149 
00150   /**
00151    *  @brief Get the value of a single parameter as a string.
00152    *  
00153    *  Returns default value if parameter is not found. 
00154    *  
00155    *  @param[in] param  Name of the parameter  
00156    *  @param[in] def    Default value to return if parameter is not there
00157    *  @return Parameter value or default value
00158    */
00159   std::string configStr(const std::string& param, const std::string& def) const
00160   {
00161     ConfigSvc::ConfigSvc cfg(m_context);
00162     try {
00163       return cfg.getStr(name(), param);
00164     } catch (const ConfigSvc::ExceptionMissing& ex) {
00165       return cfg.getStr(className(), param, def);
00166     }    
00167   }
00168 
00169   /**
00170    *  @brief Get the value of a single parameter as a Source object.
00171    *
00172    *  Returns default value if parameter is not found.
00173    *
00174    *  @param[in] param  Name of the parameter
00175    *  @param[in] def    Default value to return if parameter is not there
00176    *  @return Parameter value or default value
00177    */
00178   PSEvt::Source configSrc(const std::string& param, const std::string& def) const
00179   {
00180     return PSEvt::Source(configStr(param, def));
00181   }
00182 
00183   /**
00184    *  @brief Get the value of a parameter as a sequence.
00185    *  
00186    *  @param[in] param  Name of the parameter  
00187    *  @return The object that is convertible to sequence type such as std::list<<std::string>
00188    *          or std::vector<int>
00189    *  
00190    *  @throw ConfigSvc::ExceptionMissing thrown if parameter or section is not found
00191    */
00192   ConfigSvc::ConfigSvc::ResultList configList(const std::string& param) const
00193   {
00194     ConfigSvc::ConfigSvc cfg(m_context);
00195     try {
00196       return cfg.getList(name(), param);
00197     } catch (const ConfigSvc::ExceptionMissing& ex) {
00198       return cfg.getList(className(), param);
00199     }
00200   }
00201   
00202   /**
00203    *  @brief Get the value of a parameter as a list.
00204    *  
00205    *  Returns default list if parameter is not found. 
00206    *  
00207    *  @param[in] param  Name of the parameter
00208    *  @param[in] def    Default value to return if parameter is not there
00209    *  @return The object that is convertible to a list sequence type such as std::list<<std::string>
00210    */
00211   template <typename T>
00212   std::list<T> configList(const std::string& param, const std::list<T>& def) const
00213   {
00214     ConfigSvc::ConfigSvc cfg(m_context);
00215     try {
00216       return cfg.getList(name(), param);
00217     } catch (const ConfigSvc::ExceptionMissing& ex) {
00218       return cfg.getList(className(), param, def);
00219     }
00220   }
00221 
00222   /**
00223    *  @brief Get the value of a parameter as a vector.
00224    *  
00225    *  Returns default vector if parameter is not found. 
00226    *  
00227    *  @param[in] param  Name of the parameter
00228    *  @param[in] def    Default value to return if parameter is not there
00229    *  @return The object that is convertible to a std::vector<T> for some type T
00230    */
00231   template <typename T>
00232   std::vector<T> configList(const std::string& param, const std::vector<T>& def) const
00233   {
00234     ConfigSvc::ConfigSvc cfg(m_context);
00235     try {
00236       return cfg.getList(name(), param);
00237     } catch (const ConfigSvc::ExceptionMissing& ex) {
00238       return cfg.getList(className(), param, def);
00239     }
00240   }
00241 
00242   /// Get the full name of the object including class and instance name.
00243   const std::string& name() const { return m_name; }
00244   
00245   /// Get the class name of the object.
00246   const std::string& className() const { return m_className; }
00247 
00248 protected:
00249 
00250 private:
00251 
00252   // Data members
00253   std::string m_name;
00254   std::string m_className;
00255   Context::context_t m_context;                       ///< context (id) of this framework instance
00256 
00257 };
00258 
00259 } // namespace psana
00260 
00261 #endif // PSANA_CONFIGURABLE_H

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7