FieldValue.h
Go to the documentation of this file.00001
00002
00003 #ifndef FIELD_VALUE_H_
00004 #define FIELD_VALUE_H_
00005
00006 #include <fesa-core/Core/FesaDefs.h>
00007
00008 namespace fesa
00009 {
00010
00015 class FieldValueBase
00016 {
00017 public:
00022 void commit(bool force);
00023
00029 bool isToBeSync();
00030
00036 void setToBeSync(bool flag);
00037
00043 void setPendingChanged(bool flag);
00044
00049 bool hasPendingChanged();
00050
00051 protected:
00055 enum Phase
00056 {
00057 P1 = 0, P2 = 1
00058 } phase_;
00059
00063 bool pendingChanged_;
00064
00068 bool toBeSync_;
00069
00070 FieldValueBase()
00071 {
00072 }
00073
00074 };
00075
00076 inline void FieldValueBase::commit(bool force = false)
00077 {
00078 if (toBeSync_ || force)
00079 {
00080 phase_ = (phase_ == P1) ? P2 : P1;
00081 toBeSync_ = false;
00082 pendingChanged_ = false;
00083 }
00084 }
00085
00086 inline bool FieldValueBase::isToBeSync()
00087 {
00088 return toBeSync_;
00089 }
00090
00091 inline void FieldValueBase::setToBeSync(bool flag)
00092 {
00093 toBeSync_ = flag;
00094 pendingChanged_ = false;
00095 }
00096
00097 inline void FieldValueBase::setPendingChanged(bool flag)
00098 {
00099
00100 pendingChanged_ = flag;
00101
00102 }
00103
00104 inline bool FieldValueBase::hasPendingChanged()
00105 {
00106 return pendingChanged_;
00107 }
00108
00113 template<typename T>
00114 class FieldValue : public FieldValueBase
00115 {
00116 public:
00117
00118 FieldValue(DataIntegrity dataIntegrity);
00119
00124 T & pendingBuffer(char* pFV);
00125
00130 T & activeBuffer(char *FV);
00131
00132 private:
00136 uint32_t offset_[2];
00137
00138 };
00139
00140 template<typename T>
00141 FieldValue<T>::FieldValue(DataIntegrity dataIntegrity)
00142 {
00143
00144 phase_ = P1;
00145 toBeSync_ = false;
00146 pendingChanged_ = false;
00147 offset_[0] = sizeof(FieldValue<T> );
00148 if (dataIntegrity == DoubleBuffered)
00149 {
00150 offset_[1] = sizeof(FieldValue<T> ) + sizeof(T);
00151 }
00152 else
00153 {
00154 offset_[1] = offset_[0];
00155 }
00156
00157 }
00158
00159 template<typename T>
00160 inline T & FieldValue<T>::pendingBuffer(char* pFV)
00161 {
00162 return (this->phase_ == this->P1) ? (T&) (*(pFV + offset_[0])) : (T&) (*(pFV + offset_[1]));
00163 }
00164
00165 template<typename T>
00166 inline T & FieldValue<T>::activeBuffer(char* pFV)
00167 {
00168 return (this->phase_ == this->P1) ? (T&) (*(pFV + offset_[1])) : (T&) (*(pFV + offset_[0]));
00169 }
00170
00171
00172
00177 template<typename T>
00178 class FieldValue<T[]> : public FieldValueBase
00179 {
00180 public:
00184 FieldValue(DataIntegrity dataIntegrity, uint32_t size);
00185
00189 T* pendingBuffer(char* pFV);
00190
00194 T* activeBuffer(char* pFV);
00195
00199 void setPendingCurrentSize(uint32_t currentSize);
00200
00204 void setActiveCurrentSize(uint32_t currentSize);
00205
00209 uint32_t getPendingCurrentSize();
00210
00214 uint32_t getActiveCurrentSize();
00215
00216 private:
00217
00218 uint32_t offset_[2];
00219
00223 uint32_t currentSize_[2];
00224
00225 bool isSingleBuffer_;
00226 };
00227
00228 template<typename T>
00229 FieldValue<T[]>::FieldValue(DataIntegrity dataIntegrity, uint32_t size)
00230 {
00231
00232 phase_ = P1;
00233 toBeSync_ = false;
00234 pendingChanged_ = false;
00235 currentSize_[0] = size;
00236 currentSize_[1] = size;
00237
00238 offset_[0] = sizeof(FieldValue<T[]> );
00239
00240 if (dataIntegrity == DoubleBuffered)
00241 {
00242 offset_[1] = sizeof(FieldValue<T[]> ) + sizeof(T) * size;
00243 isSingleBuffer_ = false;
00244 }
00245 else
00246 {
00247 offset_[1] = offset_[0];
00248 isSingleBuffer_ = true;
00249 }
00250 }
00251
00252 template<typename T>
00253 inline T* FieldValue<T[]>::pendingBuffer(char* pFV)
00254 {
00255 return (this->phase_ == this->P1) ? (T*) (pFV + offset_[0]) : (T*) (pFV + offset_[1]);
00256 }
00257
00258 template<typename T>
00259 inline T* FieldValue<T[]>::activeBuffer(char* pFV)
00260 {
00261 return (this->phase_ == this->P1) ? (T*) (pFV + offset_[1]) : (T*) (pFV + offset_[0]);
00262 }
00263
00264 template<typename T>
00265 inline uint32_t FieldValue<T[]>::getPendingCurrentSize()
00266 {
00267
00268 if (isSingleBuffer_)
00269 return currentSize_[0];
00270 else
00271 return (this->phase_ == this->P1) ? currentSize_[0] : currentSize_[1];
00272 }
00273
00274 template<typename T>
00275 inline uint32_t FieldValue<T[]>::getActiveCurrentSize()
00276 {
00277
00278 if (isSingleBuffer_)
00279 return currentSize_[0];
00280 else
00281 return (this->phase_ == this->P1) ? currentSize_[1] : currentSize_[0];
00282 }
00283
00284 template<typename T>
00285 inline void FieldValue<T[]>::setPendingCurrentSize(uint32_t currentSize)
00286 {
00287
00288 if (isSingleBuffer_ || this->phase_ == this->P1)
00289 {
00290 currentSize_[0] = currentSize;
00291 }
00292 else
00293 {
00294 currentSize_[1] = currentSize;
00295 }
00296 }
00297
00298 template<typename T>
00299 inline void FieldValue<T[]>::setActiveCurrentSize(uint32_t currentSize)
00300 {
00301
00302 if (isSingleBuffer_ || this->phase_ == this->P2)
00303 {
00304 currentSize_[0] = currentSize;
00305 }
00306 else
00307 {
00308 currentSize_[1] = currentSize;
00309 }
00310 }
00311
00312
00313
00318 template<typename T>
00319 class FieldValue<T[][1]> : public FieldValueBase
00320 {
00321 public:
00322
00323 FieldValue(DataIntegrity dataIntegrity, uint32_t size1, uint32_t size2);
00324
00328 T** pendingBuffer(char *pFV);
00329
00333 T** activeBuffer(char *pFV);
00334
00338 void getPendingCurrentSize(uint32_t& size1, uint32_t& size2);
00339
00343 void getActiveCurrentSize(uint32_t& size1, uint32_t& size2);
00344
00348 void setPendingCurrentSize(uint32_t size1, uint32_t size2);
00349
00353 void setActiveCurrentSize(uint32_t size1, uint32_t size2);
00354
00355 private:
00356
00357 uint32_t offset_[2];
00358
00362 uint32_t currentSize1_[2];
00363
00367 uint32_t currentSize2_[2];
00368
00369 bool isSingleBuffer_;
00370 };
00371
00372 template<typename T>
00373 FieldValue<T[][1]>::FieldValue(DataIntegrity dataIntegrity, uint32_t size1, uint32_t size2)
00374 {
00375
00376 phase_ = P1;
00377 toBeSync_ = false;
00378 pendingChanged_ = false;
00379
00380 currentSize1_[0] = size1;
00381 currentSize1_[1] = size1;
00382 currentSize2_[0] = size2;
00383 currentSize2_[1] = size2;
00384
00385 offset_[0] = sizeof(FieldValue<T[][1]> );
00386
00387 if (dataIntegrity == DoubleBuffered)
00388 {
00389 offset_[1] = sizeof(FieldValue<T[][1]> ) + sizeof(T) * size1 * size2;
00390 isSingleBuffer_ = false;
00391 }
00392 else
00393 {
00394 offset_[1] = offset_[0];
00395 isSingleBuffer_ = true;
00396 }
00397
00398 }
00399
00400 template<typename T>
00401 inline T** FieldValue<T[][1]>::pendingBuffer(char* pFV)
00402 {
00403 return (this->phase_ == this->P1) ? (T**) (pFV + offset_[0]) : (T**) (pFV + offset_[1]);
00404 }
00405
00406 template<typename T>
00407 inline T** FieldValue<T[][1]>::activeBuffer(char* pFV)
00408 {
00409 return (this->phase_ == this->P1) ? (T**) (pFV + offset_[1]) : (T**) (pFV + offset_[0]);
00410 }
00411
00412 template<typename T>
00413 inline void FieldValue<T[][1]>::getPendingCurrentSize(uint32_t& size1, uint32_t& size2)
00414 {
00415
00416 if (isSingleBuffer_ || this->phase_ == this->P1)
00417 {
00418 size1 = currentSize1_[0];
00419 size2 = currentSize2_[0];
00420 }
00421 else
00422 {
00423 size1 = currentSize1_[1];
00424 size2 = currentSize2_[1];
00425 }
00426 }
00427
00428 template<typename T>
00429 inline void FieldValue<T[][1]>::getActiveCurrentSize(uint32_t& size1, uint32_t& size2)
00430 {
00431
00432 if (isSingleBuffer_ || this->phase_ == this->P2)
00433 {
00434 size1 = currentSize1_[0];
00435 size2 = currentSize2_[0];
00436 }
00437 else
00438 {
00439 size1 = currentSize1_[1];
00440 size2 = currentSize2_[1];
00441 }
00442 }
00443
00444 template<typename T>
00445 void FieldValue<T[][1]>::setPendingCurrentSize(uint32_t size1, uint32_t size2)
00446 {
00447
00448 if (isSingleBuffer_ || this->phase_ == this->P1)
00449 {
00450 currentSize1_[0] = size1;
00451 currentSize2_[0] = size2;
00452 }
00453 else
00454 {
00455 currentSize1_[1] = size1;
00456 currentSize2_[1] = size2;
00457 }
00458 }
00459
00460 template<typename T>
00461 void FieldValue<T[][1]>::setActiveCurrentSize(uint32_t size1, uint32_t size2)
00462 {
00463
00464 if (isSingleBuffer_ || this->phase_ == this->P2)
00465 {
00466 currentSize1_[0] = size1;
00467 currentSize2_[0] = size2;
00468 }
00469 else
00470 {
00471 currentSize1_[1] = size1;
00472 currentSize2_[1] = size2;
00473 }
00474 }
00475
00476
00477
00482 template<>
00483 class FieldValue<char[]> : public FieldValueBase
00484 {
00485 public:
00490 FieldValue(DataIntegrity dataIntegrity, uint32_t size);
00491
00495 char* pendingBuffer(char* pFV);
00496
00500 char* activeBuffer(char *pFV);
00501
00502 private:
00503
00504 uint32_t offset_[2];
00505 };
00506
00507
00508
00509
00510
00515 template<>
00516 class FieldValue<char *[]> : public FieldValueBase
00517 {
00518 public:
00519
00525 FieldValue(DataIntegrity dataIntegrity, uint32_t size1);
00526
00530 uint32_t pendingBuffer();
00531
00535 uint32_t activeBuffer();
00536
00540 void setPendingCurrentSize(uint32_t size);
00541
00545 void setActiveCurrentSize(uint32_t size);
00546
00550 uint32_t getPendingCurrentSize();
00551
00555 uint32_t getActiveCurrentSize();
00556
00557 private:
00558
00562 uint32_t offset_[2];
00563
00567 uint32_t currentSize_[2];
00568
00569 bool isSingleBuffer_;
00570 };
00571
00572
00573 }
00574
00575 #endif // FIELD_VALUE_H_