/* * File: PvData.h * Author: lpiccoli * * Created on July 28, 2010, 9:55 AM */ #ifndef _PVDATA_H #define _PVDATA_H #include #include #include #include #include #include #include #include "Defs.h" FF_NAMESPACE_START class LoopConfigurationTest; template class PvData; /** * Definition of templates for all used data types */ /** PvData definition for CHAR PV */ typedef PvData PvDataChar; /** PvData definition for UCHAR PV */ typedef PvData PvDataUChar; /** PvData definition for INT PV */ typedef PvData PvDataInt; /** PvData definition for UINT PV */ typedef PvData PvDataUInt; /** PvData definition for LONG PV */ typedef PvData PvDataLong; /** PvData definition for ULONG PV */ typedef PvData PvDataULong; /** PvData definition for DOUBLE PV */ typedef PvData PvDataDouble; /** PvData definition for SHORT PV */ typedef PvData PvDataShort; /** PvData definition for USHORT PV */ typedef PvData PvDataUShort; /** PvData definition for STRING PV */ typedef PvData PvDataString; /** PvData definition for BOOL PV */ typedef PvData PvDataBool; /** PvData definition for DOUBLE waveform PV */ typedef PvData > PvDataDoubleWaveform; /** PvData definition for USHORT waveform PV */ typedef PvData > PvDataUShortWaveform; /** PvData definition for STRING waveform PV */ typedef PvData > PvDataStringWaveform; /** PvData definition for CHAR waveform PV */ typedef PvData PvDataCharWaveform; /** * Definition of template maps for the different types of PvData. * See comment on PvData::_pvMap bellow. */ /** Map of PvDataChar */ typedef std::map*> PvMapChar; /** Map of PvDataUChar */ typedef std::map*> PvMapUChar; /** Map of PvDataInt */ typedef std::map*> PvMapInt; /** Map of PvDataUInt */ typedef std::map*> PvMapUInt; /** Map of PvDataLong */ typedef std::map*> PvMapLong; /** Map of PvDataULong */ typedef std::map*> PvMapULong; /** Map of PvDataDouble */ typedef std::map*> PvMapDouble; /** Map of PvDataShort */ typedef std::map*> PvMapShort; /** Map of PvDataUShort */ typedef std::map*> PvMapUShort; /** Map of PvDataString */ typedef std::map*> PvMapString; /** Map of PvDataBool */ typedef std::map*> PvMapBool; /** Map of PvDataDoubleWaveform */ typedef std::map*> PvMapDoubleWaveform; /** Map of PvDataUShortWaveform */ typedef std::map*> PvMapUShortWaveform; /** Map of PvDataStringWaveform */ typedef std::map*> PvMapStringWaveform; /** Map of PvDataCharWaveform */ typedef std::map*> PvMapCharWaveform; /** * This is a template class used for connecting object attributes to PV Device * Support. Any attribute (not a DeviceView) that is accessible through a PV * should be of this template class. The parameter to the template should be * the attribute type (long, int, double, etc). * * As instances are created they are added to the static PvData::_pvMap. When * the Device Support is created the specified INST_IO string is used to search * for the correct device in the _pvMap. * * @author L.Piccoli */ template class PvData { public: /** * Create a PvData with the given PvName and add it to the _pvMap * singleton * * @param pvName name of the PV associated with this data * @author L.Piccoli */ PvData(std::string pvName) : _externalValue(NULL), _pvName(pvName), _scanlist(NULL), _record(NULL) { insert(); }; PvData() : _externalValue(NULL), _pvName("ERROR"), _scanlist(NULL), _record(NULL) { }; PvData(std::string pvName, Type value) : _value(value), _externalValue(NULL), _pvName(pvName), _scanlist(NULL), _record(NULL) { insert(); } /** * Empty constructor, used only to instantiate classes in * order to initialize the _pvMap static variable. * * This constructor must *not* be used for other purposes besides * initializing the maps. * * @author L.Piccoli */ PvData(bool clear, int a) : _externalValue(NULL), _pvName("INVALID") { if (clear) { _pvMap.clear(); } } PvData(const PvData& orig) { _value = orig._value; _externalValue = orig._externalValue; }; virtual ~PvData() { }; PvData & operator=(const PvData &) { return *this; }; PvData & operator=(const Type &value) { _value = value; return *this; } bool operator==(const PvData &other) { return _value == other._value; } bool operator==(const Type &value) { return value == _value; } bool operator!=(const PvData &other) { return !(*this == other); } bool operator!=(const Type &value) { return !(*this == value); } bool operator<(const Type &value) { return _value < value; } static std::map *> *> &getPvMap() { return _pvMap; } Type *getValueAddress() { if (_externalValue == NULL) { return &_value; } else { std::cerr << ">>> RETURN EXTERNAL ADDRESS" << std::endl; return _externalValue; } } Type getValue() { if (_externalValue == NULL) { return _value; } else { std::cerr << ">>> RETURN EXTERNAL VALUE" << std::endl; return *_externalValue; } } /** * This allows values to be read from/written to an area external to the * PvData class. * * For example, suppose Class A declares a PvDataInt, but * the actual data resides in an external package. The address of the value * from the external package is passed to this method, such that the * read/write operations now use the specified address. * * @param externalValue pointer to the value to be used by this PvData * @author L.Piccoli */ void setExternalValue(Type *externalValue) { _externalValue = externalValue; } std::string getPvName() { return _pvName; } /** * This method is called by the Device Support code to update the internal * value based on the data received from Channel Access. It is used * by output devices such as Ao, Bo, Stringout and Waveformout. * * If the Waveformout is used, this method must be overriden by a subclass * implementation. The process of copying from the void * parameter to the * internal representation depends on the element of the waveform (typically * represented as std::vector<...>). * * @param val pointer to the new data to be assigned. The val pointer is * the precord->val field. * @param size number of elements pointed by the parameter val (defaul is 1) * @return 0 on success, -1 on failure * @author L.Piccoli */ virtual int write(Type *val, int size = 1) { _value = *val; if (_externalValue != NULL) { *_externalValue = *val; } return 0; } /** * This method is called by the Device Support code to update the PV record * value based on the internal data. It is used by input devices such as Ai, * Bi, Stringin and Waveformin. * * If the Waveformin is used, this method must be overriden by a subclass * implementation. The process of copying from the waveform (typically * represented as std::vector<...>) to the void * parameter depends on the * type of the waveform elements. * * @param val pointer to the data that will be updated. The val pointer is * the precord->val field. * @param size number of elements pointed by the parameter val (defaul is 1) * @return 0 on success, -1 on failure * @author L.Piccoli */ int read(Type *val, int size = 1) { if (_externalValue == NULL) { *val = _value; } else { *val = *_externalValue; } return 0; } IOSCANPVT getScanList() { return _scanlist; } void initScanList() { scanIoInit(&_scanlist); } void scanIoRequest() { if (_scanlist != NULL) { ::scanIoRequest(_scanlist); } } void setRecord(void *precord) { _record = reinterpret_cast(precord); } void setAlarm(bool set = true) { if (_record != NULL) { if (set) { _record -> nsev = MAJOR_ALARM; _record -> nsta = HIHI_ALARM; } else { _record -> nsev = NO_ALARM; _record -> nsta = NO_ALARM; } } } friend class LoopConfigurationTest; // For unit tests protected: /** Actual value */ Type _value; /** External value */ Type *_externalValue; /** Name of the PV associated with this PvData */ std::string _pvName; private: /** Device support scanlist, used for a single PvData instance */ IOSCANPVT _scanlist; /** * Points to the PV record. Even though an aiRecord is used here, most * fields at the start of the struct are the same for all type of records. * Remember that the value for the record is provided by the _value * attribute, which is accessed through device support. */ aiRecord *_record; /** * Insert a new PvData in the proper Map * * @author L.Piccoli */ void insert() { // First check if there is a PV with the same name, and then append it // to the end of the vector std::vector *> *vector; typename std::map *> *>::iterator it; it = _pvMap.find(_pvName); if (it == _pvMap.end()) { vector = new std::vector *>(); _pvMap.insert(std::pair *> *>(_pvName, vector)); } else { vector = it->second; } vector->push_back(this); } /** * Unique map of PvName -> vector of PvData for the specified Type. This map * is only used to lookup PvData instances based on a PvName when the Device * Support code is created. * * The object file containing this code must be loaded and initialized * before the iocInit() call is performed. During iocInit the device support * code is called, which will search for the PvData instances in this _pvMap. * * For devices used as output (e.g. Ao, Bo, Stringout, Waveformout), the * pointer to std::vector *> > is saved to the precord->dvpt * field. * * If devices are used as input (e.g. Ai, Bi, Stringin, Waveformin) the * pointer to the PvData is saved to precord-dpvt. * * The assignment to precord->dpvt is performed in the device support code * (see file devFfConfig.cc) */ static std::map *> *> _pvMap; }; FF_NAMESPACE_END #endif /* _PVDATA_H */