PSEnv/include/EnvObjectStore.h

Go to the documentation of this file.
00001 #ifndef PSENV_ENVOBJECTSTORE_H
00002 #define PSENV_ENVOBJECTSTORE_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: EnvObjectStore.h 12584 2016-09-08 17:56:13Z dubrovin@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class EnvObjectStore.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 #include <typeinfo>
00017 #include <boost/shared_ptr.hpp>
00018 #include <boost/enable_shared_from_this.hpp>
00019 #include <boost/utility.hpp>
00020 
00021 //----------------------
00022 // Base Class Headers --
00023 //----------------------
00024 
00025 //-------------------------------
00026 // Collaborating Class Headers --
00027 //-------------------------------
00028 #include "pdsdata/xtc/Src.hh"
00029 #include "PSEvt/DataProxy.h"
00030 #include "PSEvt/EventKey.h"
00031 #include "PSEvt/ProxyDictI.h"
00032 #include "PSEvt/Source.h"
00033 
00034 //------------------------------------
00035 // Collaborating Class Declarations --
00036 //------------------------------------
00037 
00038 //              ---------------------
00039 //              -- Class Interface --
00040 //              ---------------------
00041 
00042 namespace PSEnv {
00043 
00044 /**
00045  *  @ingroup PSEnv
00046  *  
00047  *  @brief Class to store environment data objects (such as configuration
00048  *  or calibration) corresponding to event data objects.
00049  *
00050  *  This class is very similar to PSEvt::Event class (and is implemented 
00051  *  on top of the same proxy dictionary classes) but it has more specialized 
00052  *  interface. In particular it does not support additional string keys as
00053  *  it is expected that there will be only one version of the configuration
00054  *  or calibrations objects.
00055  *
00056  *  This software was developed for the LCLS project.  If you use all or 
00057  *  part of it, please give an appropriate acknowledgment.
00058  *
00059  *  @see Env
00060  *
00061  *  @version \$Id: EnvObjectStore.h -1$
00062  *
00063  *  @author Andrei Salnikov
00064  */
00065 
00066 class EnvObjectStore : public boost::enable_shared_from_this<EnvObjectStore>, boost::noncopyable {
00067 public:
00068 
00069   /// Special class used for type-less return from get()
00070   struct GetResultProxy {
00071     
00072     /// Convert the result of get() call to smart pointer to object
00073     template<typename T>
00074     operator boost::shared_ptr<T>() {
00075       boost::shared_ptr<void> vptr = m_dict->get(&typeid(const T), m_source, m_key, m_foundSrc);
00076       return boost::static_pointer_cast<T>(vptr);
00077     }
00078     
00079     boost::shared_ptr<PSEvt::ProxyDictI> m_dict;  ///< Proxy dictionary containing the data
00080     PSEvt::Source m_source;    ///< Data source address
00081     std::string m_key;         ///< String key
00082     Pds::Src* m_foundSrc;      ///< Pointer to where to store the exact address of found object
00083   };
00084 
00085   /**
00086    *  @brief Standard constructor takes proxy dictionary object
00087    *  
00088    *  @param[in] dict Pointer to proxy dictionary
00089    */
00090   explicit EnvObjectStore(const boost::shared_ptr<PSEvt::ProxyDictI>& dict) : m_dict(dict) {}
00091 
00092   // Destructor
00093   ~EnvObjectStore() {}
00094 
00095   /**
00096    *  @brief Add one more proxy object to the store
00097    *
00098    *  @param[in] proxy - Proxy object for type T.
00099    *  @param[in] source - Source detector address.
00100    *  @param[in] key - ...
00101    */
00102   template <typename T>
00103   void putProxy(const boost::shared_ptr<PSEvt::Proxy<T> >& proxy, const Pds::Src& source,
00104                 const std::string& key=std::string())
00105   {
00106     PSEvt::EventKey evKey(&typeid(const T), source, key);
00107     if ( m_dict->exists(evKey) ) {
00108       m_dict->remove(evKey);
00109     }
00110     m_dict->put(boost::static_pointer_cast<PSEvt::ProxyI>(proxy), evKey);
00111   }
00112 
00113   /**
00114    *  @brief Add one more object to the store.
00115    *  
00116    *  If there is already an object with the same type and address it 
00117    *  will be replaced.
00118    *  
00119    *  @param[in] data - Object to store in the event.
00120    *  @param[in] source - Source detector address.
00121    *  @param[in] key - ...
00122    */
00123   template <typename T>
00124   void put(const boost::shared_ptr<T>& data, const Pds::Src& source,
00125            const std::string& key=std::string()) 
00126 
00127   {
00128     boost::shared_ptr<PSEvt::ProxyI> proxyPtr(new PSEvt::DataProxy<T>(data));
00129     PSEvt::EventKey evKey(&typeid(const T), source, key);
00130     if ( m_dict->exists(evKey) ) {
00131       m_dict->remove(evKey);
00132     }
00133     m_dict->put(proxyPtr, evKey);
00134   }
00135   
00136   /**
00137    *  @brief Get an object from store.
00138    *  
00139    *  @param[in] source - Source detector address.
00140    *  @param[in] key - ...
00141    *  @return Shared pointer (or object convertible to it) which can be zero when object is not found.
00142    */
00143   GetResultProxy get(const Pds::Src& source, const std::string& key=std::string()) 
00144   {
00145     GetResultProxy pxy = { m_dict, PSEvt::Source(source), key, NULL };
00146     return pxy;
00147   }
00148 
00149   /**
00150    *  @brief Get an object from store.
00151    *  
00152    *  @param[in] source - Source detector address.
00153    *  @param[out] foundSrc - If pointer is non-zero then pointed object will be assigned 
00154    *                       with the exact source address of the returned object.
00155    *  @param[in] key - ...
00156    *  @return Shared pointer (or object convertible to it) which can be zero when object is not found.
00157    */
00158   GetResultProxy get(const PSEvt::Source& source, Pds::Src* foundSrc=NULL, const std::string& key=std::string()) 
00159   {
00160     GetResultProxy pxy = { m_dict, source, key, foundSrc};
00161     return pxy;
00162   }
00163 
00164   /**
00165    *  @brief Get the list of keys for existing config objects
00166    *  
00167    *  @return list of the EventKey objects
00168    */
00169   std::list<PSEvt::EventKey> keys(const PSEvt::Source& source = PSEvt::Source()) const
00170   {
00171     std::list<PSEvt::EventKey> result;
00172     m_dict->keys(result, source);
00173     return result;
00174   }
00175 
00176   /**
00177    *  @brief Get access to proxy dictionary.
00178    *
00179    *  This method exposes underlying proxy dictionary object. It should
00180    *  not be used by ordinary clients but it could be useful for code
00181    *  which implements additional services based on event (such as
00182    *  Python wrappers).
00183    */
00184   const boost::shared_ptr<PSEvt::ProxyDictI>& proxyDict() const { return m_dict; }
00185 
00186 protected:
00187 
00188 private:
00189 
00190   // Data members
00191   boost::shared_ptr<PSEvt::ProxyDictI> m_dict;   ///< Proxy dictionary object 
00192 
00193 };
00194 
00195 } // namespace PSEnv
00196 
00197 #endif // PSENV_ENVOBJECTSTORE_H

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7