00001
00002
00003 #include <fesa-core/DataStore/AbstractField.h>
00004
00005 #include <fesa-core/DataStore/DataStore.h>
00006 #include <fesa-core/DataStore/FieldValue.h>
00007 #include <fesa-core/DataStore/FieldSynchroManager.h>
00008 #include <fesa-core/Synchronization/MultiplexingContext.h>
00009 #include <fesa-core/Synchronization/AbstractMultiplexingManager.h>
00010 #include <fesa-core/Synchronization/ExtendedMultiplexingManager.h>
00011 #include <fesa-core/Synchronization/CycleDescriptor.h>
00012 #include <fesa-core/Utilities/ParserElements.h>
00013 #include <fesa-core/Exception/FesaException.h>
00014
00015
00016 namespace fesa
00017 {
00018
00019 AbstractField::AbstractField(const std::string& fieldName,
00020 const FieldCategory fieldCategory, bool multiplexed,
00021 bool multiMultiplexed, bool persistent, DataIntegrity bufferType,
00022 DataStore* pDataStore) :
00023 name_(fieldName), fieldCategory_(fieldCategory), multiplexed_(multiplexed),
00024 multiplexingManager_(0), pDataStore_(pDataStore), isMultiMux_(
00025 multiMultiplexed), isPersistent_(persistent), isShared_(true),
00026 valueSize_(0), buffer_(bufferType) {
00027
00028 pDataStore_->registerField(this);
00029 }
00030
00031 std::string AbstractField::getAsString(MultiplexingContext* context) {
00032
00033 if (this->multiplexingManager_ && (context == NULL || context->getType() == MultiplexingContext::NoneCtxt) )
00034 throw FesaException(__FILE__, __LINE__, FesaErrorNoneContextforMuxedField.c_str(),
00035 this->name_.c_str());
00036
00037 uint32_t slot = (this->multiplexingManager_ == NULL) ? 0
00038 : this->multiplexingManager_->getSlot(*context);
00039 std::string str("");
00040 getValueToStore(slot, str);
00041 return str;
00042 }
00043
00044 void AbstractField::setMultiplexingManager(
00045 AbstractMultiplexingManager* muxManager) {
00046 multiplexingManager_ = muxManager;
00047 }
00048
00049 AbstractMultiplexingManager* AbstractField::getMultiplexingManager() {
00050 return multiplexingManager_;
00051 }
00052
00053 uint32_t AbstractField::getMuxDepth() {
00054 if (multiplexingManager_) {
00055 return multiplexingManager_->getDepth();
00056 }
00057 return 1;
00058 }
00059
00060 void AbstractField::setMultiplexed(bool multiplexed) {
00061 multiplexed_ = multiplexed;
00062 }
00063
00064 bool AbstractField::isMultiplexed() {
00065 return multiplexed_;
00066
00067 }
00068
00069 const std::string& AbstractField::getName() {
00070 return name_;
00071 }
00072
00073 bool AbstractField::isPersistent() {
00074 return isPersistent_;
00075 }
00076
00077 void AbstractField::setMultiMuxFlag(bool flag) {
00078 isMultiMux_ = flag;
00079 }
00080
00081 bool AbstractField::isMultiMux() {
00082 return isMultiMux_;
00083 }
00084
00085 AbstractField::~AbstractField() {
00086
00087 }
00088
00089 void AbstractField::initialize(FieldElement& fieldElement) {
00090
00091 }
00092
00093 FieldCategory AbstractField::getType() {
00094 return fieldCategory_;
00095 }
00096
00097
00098
00099
00100
00101 void AbstractField::setDataIntegrity(DataIntegrity dataIntegrity) {
00102 buffer_ = dataIntegrity;
00103 }
00104
00105 void AbstractField::commit(uint32_t slot) {
00106 this->getFieldValue(slot)->commit();
00107 }
00108
00109 void AbstractField::registerModifiedField(FieldValueBase* pFV,
00110 MultiplexingContext* context) {
00111 FieldSynchroManager* pFieldSynchroManager =
00112 FieldSynchroManager::getInstance();
00113
00114 pFieldSynchroManager->registerModifiedField(context, this->pDataStore_, isShared_, pFV);
00115 pFV->setPendingChanged(true);
00116 }
00117
00118 void AbstractField::resetToBeSync(uint32_t slot)
00119 {
00120 if (this->getFieldValue(slot)->isToBeSync())
00121 {
00122 this->getFieldValue(slot)->setToBeSync(false);
00123 }
00124 }
00125
00126 void AbstractField::restore(FieldElement& fieldElement) {
00127
00128 std::string str;
00129 int32_t slot;
00130
00131 try {
00132
00133 if (fieldElement.fromPersistency_) {
00134
00135 for (std::map<int32_t, std::string>::iterator iter =
00136 fieldElement.fieldValuesCol_.begin(); iter
00137 != fieldElement.fieldValuesCol_.end(); iter++) {
00138
00139 slot = 0;
00140 if (this->multiplexingManager_ != NULL) {
00141 slot = this->multiplexingManager_->requireSlot(iter->first);
00142 }
00143 this->copyValue(slot, iter->second);
00144
00145
00146 ExtendedMultiplexingManager
00147 * emm =
00148 dynamic_cast<ExtendedMultiplexingManager*> (this->multiplexingManager_);
00149
00150
00151
00152 std::map<int32_t, std::map<std::string, std::string> >::iterator
00153 extraValues = fieldElement.fieldExtraValuesCol_.find(
00154 iter->first);
00155
00156 for (std::map<std::string, std::string>::iterator itCycleId =
00157 (*extraValues).second.begin(); itCycleId
00158 != (*extraValues).second.end(); ++itCycleId) {
00159
00160 if (emm != 0) {
00161 slot = emm->requireSlot(iter->first, itCycleId->first);
00162 } else {
00163 throw FesaException(
00164 __FILE__,
00165 __LINE__,
00166 FesaErrorFieldMissingExtendedMultiplexingManager.c_str(),
00167 this->name_.c_str());
00168 }
00169 this->copyValue(slot, itCycleId->second);
00170
00171 }
00172
00173 }
00174 }
00175
00176 else {
00177
00178 std::map<int32_t, std::string>::iterator iter =
00179 fieldElement.fieldValuesCol_.begin();
00180
00181 int32_t depth = 1;
00182
00183 if (this->multiplexingManager_ != NULL) {
00184 depth = this->multiplexingManager_->getDepth();
00185 }
00186
00187
00188 if (iter != fieldElement.fieldValuesCol_.end()) {
00189 for (int32_t i = 0; i < depth; i++) {
00190 this->copyValue(i, iter->second);
00191 }
00192 }
00193
00194 }
00195
00196 } catch (FesaException& ex) {
00197
00198 throw FesaException(ex.getFileName(), ex.getLineNumber(),
00199 FesaErrorRestoringField.c_str(), this->name_.c_str());
00200
00201 } catch (...) {
00202 throw FesaException(__FILE__, __LINE__,
00203 FesaErrorRestoringField.c_str(), this->name_.c_str());
00204 }
00205
00206 }
00207
00208 void AbstractField::store(FieldElement& fieldElement) {
00209
00210 std::string str;
00211
00212 fieldElement.fieldName_ = this->getName();
00213 fieldElement.fieldValuesCol_.clear();
00214 fieldElement.cyclesNamesCol_.clear();
00215
00216 std::map<std::string, std::string> map;
00217 std::vector<int32_t> cyclesCol_;
00218
00219 std::string cycleName;
00220 int32_t cycleId;
00221
00222 if (this->multiplexingManager_ != NULL) {
00223 cyclesCol_
00224 = this->multiplexingManager_->getTimingDescriptor()->getCycleIdsCol();
00225
00226 } else {
00227
00228 cyclesCol_.push_back(-1);
00229 }
00230
00231 try {
00232
00233
00234 for (std::vector<int32_t>::const_iterator itr = cyclesCol_.begin(); itr
00235 != cyclesCol_.end(); ++itr) {
00236
00237 cycleName = "NONE";
00238 cycleId = -1;
00239 map.clear();
00240 int32_t slot = 0;
00241
00242 cycleId = (*itr);
00243 if (this->multiplexingManager_ != NULL) {
00244 slot = this->multiplexingManager_->getLastSlot(cycleId);
00245 cycleName
00246 = this->multiplexingManager_->getTimingDescriptor()->getCycleSelectorName(
00247 cycleId);
00248 }
00249
00250 this->getValueToStore(slot, str);
00251
00252 fieldElement.fieldValuesCol_.insert(make_pair(cycleId, str));
00253 fieldElement.cyclesNamesCol_.insert(make_pair(cycleId, cycleName));
00254
00255 ExtendedMultiplexingManager
00256 * emm =
00257 dynamic_cast<ExtendedMultiplexingManager*> (this->multiplexingManager_);
00258 if (emm != 0) {
00259 std::vector<std::string> extraCriterionCol =
00260 emm->getExtraCriterionCol();
00261
00262 for (uint32_t ext = 0; ext < extraCriterionCol.size(); ext++) {
00263
00264 int32_t slot =
00265 emm->getLastSlot(cycleId, extraCriterionCol[ext]);
00266 getValueToStore(slot, str);
00267 map.insert(make_pair(extraCriterionCol[ext], str));
00268
00269 }
00270 }
00271 fieldElement.fieldExtraValuesCol_.insert(make_pair(cycleId, map));
00272 }
00273 }
00274
00275 catch (FesaException& ex) {
00276 throw FesaException(ex.getFileName(), ex.getLineNumber(),
00277 FesaErrorStoringField.c_str(), this->name_.c_str());
00278 } catch (...) {
00279 throw FesaException(__FILE__, __LINE__, FesaErrorStoringField.c_str(),
00280 this->name_.c_str());
00281 }
00282
00283 }
00284
00285 }