Field.h

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #ifndef FIELD_H_
00004 #define FIELD_H_
00005 
00006 #include <fesa-core/DataStore/AbstractField.h>
00007 #include <fesa-core/DataStore/FieldValue.h>
00008 #include <fesa-core/DataStore/Converter.h>
00009 #include <fesa-core/Synchronization/AbstractMultiplexingManager.h>
00010 #include <fesa-core/Exception/FesaException.h>
00011 
00012 #include <string>
00013 #include <cstring>
00014 
00015 namespace fesa
00016 {
00017 
00022 template<typename T>
00023 class Field : public AbstractField {
00024   public:
00030     Field(const std::string& fieldName, const FieldCategory fieldCategory,
00031           bool multiplexed, bool multiMultiplexed, bool persistent,
00032           DataIntegrity bufferType, DataStore* pDataStore);
00033 
00037     ~Field();
00038 
00039   protected:
00040 
00046     uint32_t getFieldValueSize();
00047 
00052     FieldValue<T>* getFieldValue(int32_t slot);
00053 
00058     void setFieldValueAddress(char* pFV, bool initFieldsFlag);
00059 
00063     FieldValue<T>* fieldValue_;
00064 
00070     void copyValue(uint32_t slot, std::string& val);
00071 
00072     /*
00073      * \return the value of the pending buffer as a string
00074      */
00075     void getValueToStore(int32_t slot, std::string& str);
00076 
00077 };
00078 
00079 template<typename T>
00080 Field<T>::Field(const std::string& fieldName,
00081                 const FieldCategory fieldCategory, bool multiplexed,
00082                 bool multiMultiplexed, bool persistent, DataIntegrity bufferType,
00083                 DataStore* pDataStore) :
00084     AbstractField(fieldName, fieldCategory, multiplexed, multiMultiplexed,
00085                   persistent, bufferType, pDataStore) {
00086 
00087 }
00088 
00089 template<typename T>
00090 Field<T>::~Field() {
00091 
00092 }
00093 
00094 template<typename T>
00095 inline FieldValue<T>* Field<T>::getFieldValue(int32_t slot) {
00096 
00097     char* address = (char*) this->fieldValue_;
00098     address += valueSize_ * slot;
00099     return (FieldValue<T>*) address;
00100 }
00101 
00102 template<typename T>
00103 uint32_t Field<T>::getFieldValueSize() {
00104     if (valueSize_ == 0) {
00105         valueSize_ = static_cast<uint32_t>(sizeof(FieldValue<T> )) + static_cast<uint32_t>(sizeof(T)) * buffer_;
00106     }
00107     return valueSize_;
00108 }
00109 
00110 template<typename T>
00111 void Field<T>::setFieldValueAddress(char* pFV, bool initFieldsFlag) {
00112     int32_t depth = 1;
00113 
00114     if (this->multiplexingManager_ != NULL) {
00115         depth = this->multiplexingManager_->getDepth();
00116     }
00117 
00118     this->fieldValue_ = (FieldValue<T>*) pFV;
00119 
00120     if (initFieldsFlag) {
00121         for (int32_t i = 0; i < depth; i++) {
00122             FieldValue<T>* temp = new (pFV) FieldValue<T> (buffer_);
00123             if (!temp) {
00124                 throw FesaException(__FILE__, __LINE__,
00125                                     FesaErrorSettingFieldValueAddress.c_str(),
00126                                     name_.c_str());
00127             }
00128             pFV += this->valueSize_;
00129         }
00130     }
00131 
00132 }
00133 
00134 template<typename T>
00135 void Field<T>::copyValue(uint32_t slot, std::string& str) {
00136     T* pVal = new T;
00137     Converter<T>::stringToValue(str, pVal);
00138 
00139     FieldValue<T>* pFV = this->getFieldValue(slot);
00140 
00141     std::memcpy(&(pFV->activeBuffer((char*) pFV)), pVal, sizeof(T));
00142     std::memcpy(&(pFV->pendingBuffer((char*) pFV)), pVal, sizeof(T));
00143 
00144     delete pVal;
00145 }
00146 
00147 template<typename T>
00148 void Field<T>::getValueToStore(int32_t slot, std::string& str)
00149 
00150 {
00151     FieldValue<T>* pFV = this->getFieldValue(slot);
00152 
00153     if (pFV->hasPendingChanged() || pFV->isToBeSync()) {
00154         Converter<T>::valueToString(pFV->pendingBuffer((char*) pFV), str);
00155     } else {
00156         Converter<T>::valueToString(pFV->activeBuffer((char*) pFV), str);
00157     }
00158 }
00159 
00160 } // fesa
00161 
00162 #endif // FIELD_H_

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1