FieldArray2D.h

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #ifndef _FIELD_ARRAY_2D_H_
00004 #define _FIELD_ARRAY_2D_H_
00005 
00006 #include <fesa-core/DataStore/AbstractField.h>
00007 
00008 #include <string>
00009 
00010 namespace fesa
00011 {
00012 
00017 template<typename T>
00018 class FieldArray2D : public AbstractField {
00019   public:
00024     FieldArray2D(const std::string& fieldName,
00025                  const FieldCategory fieldCategory, bool multiplexed,
00026                  bool multiMultiplexed, bool persistent, DataIntegrity bufferType,
00027                  DataStore* pDataStore, int32_t size1, int32_t size2);
00028 
00032     ~FieldArray2D();
00033 
00039     void getMaxSize(uint32_t& size1, uint32_t& size2);
00040 
00041   protected:
00042 
00047     void initialize(FieldElement& fieldElement);
00048 
00054     uint32_t getFieldValueSize();
00055 
00060     void setFieldValueAddress(char* pFV, bool initFieldsFlag);
00061 
00069     void checkMaxDimensions(uint32_t size1, uint32_t size2);
00070 
00075     FieldValue<T[][1]>* getFieldValue(int32_t slot);
00076 
00082     void copyValue(uint32_t slot, std::string& str);
00083 
00084     /*
00085      * \return the value of the pending buffer as a string
00086      */
00087     void getValueToStore(int32_t slot, std::string& value);
00088 
00092     uint32_t maxSize1_;
00093 
00097     uint32_t maxSize2_;
00098 
00102     FieldValue<T[][1]>* fieldValue_;
00103 
00104 };
00105 
00106 template<typename T>
00107 FieldArray2D<T>::FieldArray2D(const std::string& fieldName,
00108                               const FieldCategory fieldCategory, bool multiplexed,
00109                               bool multiMultiplexed, bool persistent, DataIntegrity bufferType,
00110                               DataStore* pDataStore, int32_t size1, int32_t size2) :
00111     AbstractField(fieldName, fieldCategory, multiplexed, multiMultiplexed,
00112                   persistent, bufferType, pDataStore), maxSize1_(size1), maxSize2_(
00113                       size2) {
00114 
00115 }
00116 
00117 template<typename T>
00118 FieldArray2D<T>::~FieldArray2D() {
00119 }
00120 
00121 template<typename T>
00122 inline FieldValue<T[][1]>* FieldArray2D<T>::getFieldValue(int32_t slot) {
00123 
00124     char* address = (char*) this->fieldValue_;
00125     address += valueSize_ * slot;
00126     return (FieldValue<T[][1]>*) address;
00127 
00128 }
00129 
00130 template<typename T>
00131 void FieldArray2D<T>::setFieldValueAddress(char* pFV, bool initFieldsFlag) {
00132     int32_t depth = 1;
00133 
00134     if (this->multiplexingManager_ != NULL) {
00135         depth = this->multiplexingManager_->getDepth();
00136     }
00137 
00138     this->fieldValue_ = (FieldValue<T[][1]>*) pFV;
00139 
00140     if (initFieldsFlag) {
00141         for (int32_t i = 0; i < depth; i++) {
00142             FieldValue<T[][1]>* temp = new (pFV) FieldValue<T[][1]> (buffer_,
00143                                                                      maxSize1_, maxSize2_);
00144             if (!temp) {
00145                 throw FesaException(__FILE__, __LINE__,
00146                                     FesaErrorSettingFieldValueAddress.c_str(),
00147                                     name_.c_str());
00148             }
00149             pFV += this->valueSize_;
00150         }
00151     }
00152 
00153 }
00154 
00155 template<typename T>
00156 void FieldArray2D<T>::initialize(FieldElement& fieldElement) {
00157 
00158     if (fieldElement.dimension1_ > 0) {
00159         this->maxSize1_ = fieldElement.dimension1_;
00160     }
00161     if (fieldElement.dimension2_ > 0) {
00162         this->maxSize2_ = fieldElement.dimension2_;
00163     }
00164 
00165 }
00166 
00167 template<typename T>
00168 uint32_t FieldArray2D<T>::getFieldValueSize() {
00169 
00170     if (valueSize_ == 0) {
00171         valueSize_ = sizeof(FieldValue<T[][1]> ) + sizeof(T) * maxSize1_
00172             * maxSize2_ * buffer_;
00173     }
00174     return valueSize_;
00175 }
00176 
00177 template<typename T>
00178 void FieldArray2D<T>::checkMaxDimensions(uint32_t size1,
00179                                          uint32_t size2) {
00180 
00181     if (size1 > maxSize1_ || size2 > maxSize2_) {
00182         throw FesaException(__FILE__, __LINE__,
00183                             FesaErrorFieldMaxDimensionsOutOfBound.c_str(),
00184                             StringUtilities::toString(size1).c_str(),
00185                             StringUtilities::toString(size2).c_str(),
00186                             this->name_.c_str(),
00187                             StringUtilities::toString(maxSize1_).c_str(),
00188                             StringUtilities::toString(maxSize2_).c_str());
00189     }
00190 }
00191 
00192 template<typename T>
00193 void FieldArray2D<T>::copyValue(uint32_t slot, std::string& str) {
00194 
00195     std::vector<std::string> rows;
00196 
00197     // Copy the string into a temp string because the original one cannot be modified because
00198     // it is used to populate the other slots if the filed is multiplexed.
00199     std::string tmpStr(str);
00200     StringUtilities::removeBrackets(tmpStr);
00201     StringUtilities::getElements(tmpStr, rows);
00202 
00203     checkMaxDimensions(rows.size(), 0);
00204 
00205     // get the min size between the number of rows and the first dimension
00206     uint32_t minRowSize = rows.size() < this->maxSize1_ ? rows.size()
00207         : this->maxSize1_;
00208 
00209     FieldValue<T[][1]>* pFV = this->getFieldValue(slot);
00210 
00211     T (*activeVal)[this->maxSize2_];
00212 
00213     activeVal = (T(*)[this->maxSize2_]) pFV->activeBuffer((char*) pFV);
00214 
00215     T (*pendingVal)[this->maxSize2_];
00216 
00217     pendingVal = (T(*)[this->maxSize2_]) pFV->pendingBuffer((char*) pFV);
00218 
00219     for (uint32_t j = 0; j < minRowSize; ++j) {
00220         std::vector<std::string> columns;
00221         // remove any blanks before open bracket and after close bracket
00222         std::string& rowEl = rows.at(j);
00223 
00224         StringUtilities::removeBrackets(rowEl);
00225         StringUtilities::getElements(rowEl, columns);
00226 
00227         checkMaxDimensions(rows.size(), columns.size());
00228 
00229         // get the min size between the number of rows and the first dimension
00230         uint32_t minColumnSize =
00231             (columns.size() < this->maxSize2_) ? columns.size()
00232             : this->maxSize2_;
00233 
00234         pFV->setPendingCurrentSize(minRowSize, minColumnSize);
00235         pFV->setActiveCurrentSize(minRowSize, minColumnSize);
00236 
00237         for (uint32_t jj = 0; jj < minColumnSize; ++jj) {
00238 
00239             Converter<T>::stringToValue(columns.at(jj), &activeVal[j][jj]);
00240             pendingVal[j][jj] = activeVal[j][jj];
00241 
00242         }
00243     }
00244 
00245 }
00246 
00247 template<typename T>
00248 void FieldArray2D<T>::getValueToStore(int32_t slot, std::string& value) {
00249     value = "{";
00250     std::string str;
00251 
00252     FieldValue<T[][1]>* pFV = this->getFieldValue(slot);
00253     T** pBufVal;
00254     uint32_t currentSize1=0, currentSize2=0;
00255     if(pFV->isToBeSync()) {
00256         pBufVal = pFV->pendingBuffer((char*) pFV);
00257         pFV->getPendingCurrentSize(currentSize1, currentSize2);
00258     }
00259     else {
00260         pBufVal = pFV->activeBuffer((char*) pFV);
00261         pFV->getActiveCurrentSize(currentSize1, currentSize2);
00262     }
00263     for (uint32_t j = 0; j < currentSize1; j++) {
00264         value += "{";
00265         for (uint32_t jj = 0; jj < currentSize2; ++jj) {
00266             T (*pVal)[currentSize2];
00267             pVal = (T(*)[currentSize2]) pBufVal;
00268             Converter<T>::valueToString(pVal[j][jj], str);
00269             if ((jj != currentSize2 - 1)) {
00270                 value = value + str + ",";
00271             } else {
00272                 value = value + str;
00273             }
00274         }
00275         value += "}";
00276         if ((j != currentSize1 - 1))
00277             value += ",";
00278     }
00279     value += "}";
00280 }
00281 
00282 template<typename T>
00283 void FieldArray2D<T>::getMaxSize(uint32_t& size1, uint32_t& size2) {
00284     size1 = this->maxSize1_;
00285     size2 = this->maxSize2_;
00286 
00287 }
00288 
00289 } // fesa
00290 
00291 #endif // _FIELD_ARRAY_2D_H_

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1