PSEnv/include/EpicsStoreImpl.h

Go to the documentation of this file.
00001 #ifndef PSENV_EPICSSTOREIMPL_H
00002 #define PSENV_EPICSSTOREIMPL_H
00003 
00004 //--------------------------------------------------------------------------
00005 // File and Version Information:
00006 //      $Id: EpicsStoreImpl.h 8426 2014-06-17 01:39:57Z davidsch@SLAC.STANFORD.EDU $
00007 //
00008 // Description:
00009 //      Class EpicsStoreImpl.
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------
00014 // C/C++ Headers --
00015 //-----------------
00016 #include <string>
00017 #include <vector>
00018 #include <map>
00019 #include <boost/shared_ptr.hpp>
00020 #include <boost/utility.hpp>
00021 #include <tr1/tuple>
00022 
00023 //----------------------
00024 // Base Class Headers --
00025 //----------------------
00026 
00027 //-------------------------------
00028 // Collaborating Class Headers --
00029 //-------------------------------
00030 #include "pdsdata/xtc/Src.hh"
00031 #include "psddl_psana/epics.ddl.h"
00032 #include "psddl_psana/EpicsLib.h"
00033 #include "PSEnv/Exceptions.h"
00034 #include "PSTime/Time.h"
00035 
00036 //------------------------------------
00037 // Collaborating Class Declarations --
00038 //------------------------------------
00039 
00040 //              ---------------------
00041 //              -- Class Interface --
00042 //              ---------------------
00043 
00044 namespace PSEnv {
00045 
00046 /**
00047  *  @ingroup PSEnv
00048  *  
00049  *  @brief Class that provides implementation for the EPICS PV store.
00050  *  
00051  *  This is a part of the EpicsStore implementation detail, should not
00052  *  be of much interest to end users. Hides all complexities from
00053  *  EpicsStore class declaration.
00054  *
00055  *  This software was developed for the LCLS project.  If you use all or 
00056  *  part of it, please give an appropriate acknowledgment.
00057  *
00058  *  @version \$Id: EpicsStoreImpl.h 8426 2014-06-17 01:39:57Z davidsch@SLAC.STANFORD.EDU $
00059  *
00060  *  @author Andrei Salnikov
00061  */
00062 
00063 class EpicsStoreImpl  {
00064 public:
00065 
00066   // Default constructor
00067   EpicsStoreImpl () ;
00068 
00069   // Destructor
00070   ~EpicsStoreImpl () ;
00071 
00072   /// Store EPICS PV
00073   void store(const boost::shared_ptr<Psana::Epics::EpicsPvHeader>& pv, const Pds::Src& src, const std::string *pvName=NULL, long eventTag = -1);
00074   
00075   /// Store alias name for EPICS PV.
00076   void storeAlias(const Pds::Src& src, int pvId, const std::string& alias);
00077 
00078   /// Get the full list of PV names and aliases.
00079   void names(std::vector<std::string>& names) const;
00080 
00081   /// Get the list of PV names.
00082   void pvNames(std::vector<std::string>& names) const;
00083 
00084   /// Get the list of PV aliases.
00085   void aliases(std::vector<std::string>& names) const;
00086 
00087   /// Get alias name for specified PV name.
00088   std::string alias(const std::string& pv) const;
00089 
00090   /// Get PV name for specified alias name.
00091   std::string pvName(const std::string& alias) const;
00092 
00093   /// Get CTRL object for given EPICS PV name
00094   template <typename T>
00095   boost::shared_ptr<T> getCtrl(const std::string& name) const {
00096     boost::shared_ptr<Psana::Epics::EpicsPvCtrlHeader> ptr = getCtrlImpl(name);
00097     return boost::dynamic_pointer_cast<T>(ptr);
00098   }
00099   
00100   /// Get TIME object for given EPICS PV name
00101   template <typename T>
00102   boost::shared_ptr<T> getTime(const std::string& name) const {
00103     boost::shared_ptr<Psana::Epics::EpicsPvTimeHeader> ptr = getTimeImpl(name);
00104     return boost::dynamic_pointer_cast<T>(ptr);
00105   }
00106 
00107   /// Type for storing TIME EPICs pv and eventTag used when storing
00108   struct TimeHeaderAndEventTag {
00109     boost::shared_ptr<Psana::Epics::EpicsPvTimeHeader> pv;
00110     long eventTag;
00111   TimeHeaderAndEventTag() : pv(), eventTag(-1) {};
00112     TimeHeaderAndEventTag(boost::shared_ptr<Psana::Epics::EpicsPvTimeHeader> _pv, 
00113                           long _eventTag) : pv(_pv), eventTag(_eventTag) {};
00114   }; 
00115 
00116   /// Get TimeHeader and EventTag for a given EPICS PV name. If name not found,
00117   /// return default TimeHeaderAndEventTag wher pv is NULL.
00118   TimeHeaderAndEventTag getTimeAndEventTag(const std::string& name) const;
00119 
00120   /// Get base class object for given EPICS PV name
00121   boost::shared_ptr<Psana::Epics::EpicsPvHeader> getAny(const std::string& name) const ;
00122 
00123   /**
00124    *   @brief Get status info for the EPICS PV.
00125    *   
00126    *   @param[in] name      PV name
00127    *   @param[out] status   EPICS status value
00128    *   @param[out] severity EPICS severity value
00129    *   @param[out] time     Time of the last change, can be (0) if time is unknown
00130    *   
00131    *   @throw ExceptionEpicsName  if the name of the PV is not known
00132    */
00133   void getStatus(const std::string& name, int& status, int& severity, PSTime::Time& time) const ;
00134 
00135   /**
00136    *  @brief Get the value of the EPICS PV, convert to requested type.
00137    *  
00138    *   @param[in] name     PV name
00139    *   @param[in] idx      value index (for array PVs use non-zero index)
00140    *   @return the value of the PV
00141    *   
00142    *   @throw ExceptionEpicsName  if the name of the PV is not known
00143    *   @throw ExceptionEpicsConversion  if the PV value cannot be converted to requested type
00144    */
00145   template <typename T>
00146   T getValue(const std::string& name, unsigned idx=0) const
00147   {
00148     boost::shared_ptr<Psana::Epics::EpicsPvHeader> pv = getAny(name);
00149     if (not pv.get()) throw ExceptionEpicsName(ERR_LOC, name);
00150     try {
00151       return Psana::EpicsLib::getEpicsValue<T>(*pv, idx);
00152     } catch (const std::exception& ex) {
00153       throw ExceptionEpicsConversion(ERR_LOC, name, typeid(T), ex.what());
00154     }
00155   }
00156 
00157 protected:
00158 
00159   /// Implementation of the getCtrl which returns generic pointer
00160   boost::shared_ptr<Psana::Epics::EpicsPvCtrlHeader> getCtrlImpl(const std::string& name) const;
00161 
00162   /// Implementation of the getTime which returns generic pointer
00163   boost::shared_ptr<Psana::Epics::EpicsPvTimeHeader> getTimeImpl(const std::string& name) const;
00164   
00165 private:
00166 
00167   // PV id is: src.log(), src.phy(), epics.pvId
00168   typedef std::tr1::tuple<uint32_t, uint32_t, int> PvId;
00169 
00170   /// Type for mapping from PV id to PV or alias name
00171   typedef std::map<PvId, std::string> ID2Name;
00172   
00173   /// Type for mapping from PV or alias name to PV id
00174   typedef std::map<std::string, PvId> Name2ID;
00175 
00176   /// Type for mapping from PV name to EpicsPvCtrl* objects
00177   typedef std::map<std::string, boost::shared_ptr<Psana::Epics::EpicsPvCtrlHeader> > CrtlMap;
00178 
00179   /// Type for mapping from PV name to EpicsPvTime* objects
00180   typedef std::map<std::string, TimeHeaderAndEventTag > TimeMap;
00181   
00182   // Data members
00183   ID2Name m_id2name;  ///< Mapping from PV ID to its name.
00184   Name2ID m_name2id;  ///< Mapping from PV name to its ID.
00185   ID2Name m_id2alias;  ///< Mapping from PV ID to alias name.
00186   Name2ID m_alias2id;  ///< Mapping from alias name to its ID.
00187   CrtlMap m_ctrlMap;  ///< Mapping from PV name to EPICS object for CTRL objects
00188   TimeMap m_timeMap;  ///< Mapping from PV name to EPICS object for TIME objects
00189 
00190 };
00191 
00192 } // namespace PSEnv
00193 
00194 #endif // PSENV_EPICSSTOREIMPL_H

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7