FieldArray.h

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #ifndef _FIELD_ARRAY_H_
00004 #define _FIELD_ARRAY_H_
00005 
00006 #include <fesa-core/Utilities/ParserElements.h>
00007 #include <fesa-core/DataStore/AbstractField.h>
00008 #include <fesa-core/DataStore/FieldValue.h>
00009 #include <string>
00010 
00011 namespace fesa
00012 {
00013 
00018 template<typename T>
00019 class FieldArray: public AbstractField {
00020   public:
00026     FieldArray(const std::string& fieldName, const FieldCategory fieldCategory,
00027                bool multiplexed, bool multiMultiplexed, bool persistent,
00028                DataIntegrity bufferType, DataStore* pDataStore, int32_t size);
00029 
00033     ~FieldArray();
00034 
00040     uint32_t getMaxSize();
00041 
00042   protected:
00043 
00049     uint32_t getFieldValueSize();
00050 
00055     void initialize(FieldElement& fieldElement);
00056 
00061     void setFieldValueAddress(char* pFV, bool initFieldsFlag);
00062 
00068     void checkMaxDimension(uint32_t size);
00069 
00074     FieldValue<T[]>* getFieldValue(int32_t slot);
00075 
00079     uint32_t maxSize_;
00080 
00085     FieldValue<T[]>* fieldValue_;
00086 
00092     void copyValue(uint32_t slot, std::string& str);
00093 
00094     /*
00095      * \return the value of the pending buffer as a string
00096      */
00097     void getValueToStore(int32_t slot, std::string& str);
00098 
00099 };
00100 
00101 template<typename T>
00102 FieldArray<T>::FieldArray(const std::string& fieldName,
00103                           const FieldCategory fieldCategory, bool multiplexed,
00104                           bool multiMultiplexed, bool persistent, DataIntegrity bufferType,
00105                           DataStore* pDataStore, int32_t size) :
00106     AbstractField(fieldName, fieldCategory, multiplexed, multiMultiplexed,
00107                   persistent, bufferType, pDataStore), maxSize_(size), fieldValue_(
00108                       NULL) {
00109 
00110 }
00111 
00112 template<typename T>
00113 FieldArray<T>::~FieldArray() {
00114 }
00115 
00116 template<typename T>
00117 inline FieldValue<T[]>* FieldArray<T>::getFieldValue(int32_t slot) {
00118     char* address = (char*) this->fieldValue_;
00119     address += valueSize_ * slot;
00120     return (FieldValue<T[]>*) address;
00121 }
00122 
00123 template<typename T>
00124 void FieldArray<T>::setFieldValueAddress(char* pFV, bool initFieldsFlag) {
00125     int32_t depth = 1;
00126 
00127     if (this->multiplexingManager_ != NULL) {
00128 
00129         depth = this->multiplexingManager_->getDepth();
00130     }
00131 
00132     this->fieldValue_ = (FieldValue<T[]>*) pFV;
00133 
00134     if (initFieldsFlag) {
00135 
00136         for (int32_t i = 0; i < depth; i++) {
00137             FieldValue<T[]>* temp = new (pFV) FieldValue<T[]> (buffer_,
00138                                                                maxSize_);
00139             if (!temp) {
00140                 throw FesaException(__FILE__, __LINE__,
00141                                     FesaErrorSettingFieldValueAddress.c_str(),
00142                                     name_.c_str());
00143             }
00144             pFV += this->valueSize_;
00145         }
00146     }
00147 
00148 }
00149 
00150 template<typename T>
00151 uint32_t FieldArray<T>::getFieldValueSize() {
00152     if (valueSize_ == 0) {
00153         valueSize_ = sizeof(FieldValue<T[]> ) + sizeof(T) * maxSize_ * buffer_;
00154     }
00155     return valueSize_;
00156 
00157 }
00158 
00159 template<typename T>
00160 void FieldArray<T>::initialize(FieldElement& fieldElement) {
00161     if (fieldElement.dimension1_ > 0) {
00162 
00163         this->maxSize_ = fieldElement.dimension1_;
00164     }
00165 
00166 }
00167 
00168 template<typename T>
00169 void FieldArray<T>::checkMaxDimension(uint32_t size) {
00170     if (size > this->maxSize_) {
00171         throw FesaException(__FILE__, __LINE__,
00172                             FesaErrorFieldMaxDimensionOutOfBound.c_str(),
00173                             StringUtilities::toString(size).c_str(), this->name_.c_str(),
00174                             StringUtilities::toString(maxSize_).c_str());
00175     }
00176 }
00177 
00178 template<typename T>
00179 void FieldArray<T>::copyValue(uint32_t slot, std::string& str) {
00180     std::vector<std::string> tokens;
00181 
00182     // Copy the string into a temp string because the original one cannot be modified because
00183     // it is used to populate the other slots if the filed is multiplexed.
00184     std::string tmpStr(str);
00185     StringUtilities::removeBrackets(tmpStr);
00186     StringUtilities::getElements(tmpStr, tokens);
00187 
00188     this->checkMaxDimension(tokens.size());
00189 
00190     // get the min size between the number of tokens and the array
00191     uint32_t minSize = tokens.size() < this->maxSize_ ? tokens.size()
00192         : this->maxSize_;
00193 
00194     FieldValue<T[]>* pFV = this->getFieldValue(slot);
00195 
00196     pFV->setActiveCurrentSize(minSize);
00197     pFV->setPendingCurrentSize(minSize);
00198 
00199     T* lastValue = new T();
00200     for (uint32_t j = 0; j < minSize; j++) {
00201         Converter<T>::stringToValue(tokens[j],
00202                                     &(pFV->activeBuffer((char*) pFV)[j]));
00203         pFV->pendingBuffer((char*) pFV)[j] = pFV->activeBuffer((char*) pFV)[j];
00204     }
00205     // fill the rest of the array with the last value
00206     *lastValue = pFV->activeBuffer((char*) pFV)[minSize-1];
00207     for (uint32_t j = minSize; j < this->maxSize_; j++) {
00208         pFV->activeBuffer((char*) pFV)[j] = *lastValue;
00209         pFV->pendingBuffer((char*) pFV)[j] = *lastValue;
00210     }
00211 
00212     delete lastValue;
00213 
00214 }
00215 
00216 template<typename T>
00217 void FieldArray<T>::getValueToStore(int32_t slot, std::string& value) {
00218     value = "{";
00219     std::string str;
00220 
00221     FieldValue<T[]>* pFV = this->getFieldValue(slot);
00222     T* pBufVal;
00223     uint32_t size;
00224     if(pFV->isToBeSync()) {
00225         pBufVal = pFV->pendingBuffer((char*) pFV);
00226         size = pFV->getPendingCurrentSize();
00227     }
00228     else {
00229         pBufVal = pFV->activeBuffer((char*) pFV);
00230         size = pFV->getActiveCurrentSize();
00231     }
00232     for (uint32_t j = 0; j < size; j++) {
00233         Converter<T>::valueToString(pBufVal[j], str);
00234         if (j != size - 1) {
00235             value = value + str + ",";
00236         } else {
00237             value = value + str;
00238         }
00239     }
00240     value += "}";
00241 }
00242 
00243 template<typename T>
00244 uint32_t FieldArray<T>::getMaxSize() {
00245     return this->maxSize_;
00246 }
00247 
00248 } // fesa
00249 
00250 #endif // _FIELD_ARRAY_H_

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1