00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "freParameter.h"
00023
00024 #include "freConvert.h"
00025
00026 namespace FREE
00027 {
00031
00032
00033 Parameter::LayerCountType
00034 Parameter::
00035 AddParameterLayer(bool bCopyValue, const std::string& sNewValue)
00036 {
00037 ParameterLayer::Pointer smpLayer = ParameterLayer::New(ParameterSize(),sNewValue);
00038
00039 if ((bCopyValue)&(m_Layers.size()>0))
00040 {
00041 ParameterLayer::Pointer smpLastLayer = m_Layers.back();
00042 *(smpLayer.GetPointer()) = *(smpLastLayer.GetPointer());
00043 }
00044
00045 m_Layers.push_back(smpLayer);
00046 return m_Layers.size()-1;
00047 };
00048
00049 void
00050 Parameter::
00051 DeleteParameterLayer(const Parameter::LayerCountType iLayer)
00052 {
00053 if (iLayer<m_Layers.size())
00054 {
00055 m_Layers.erase(m_Layers.begin()+iLayer);
00056 }
00057 };
00058
00059 void
00060 Parameter::
00061 Reset()
00062 {
00063 XMLStreamObject::Reset();
00064 while (m_Layers.size()>0)
00065 {
00066 DeleteParameterLayer(0);
00067 }
00068 };
00069
00070 void
00071 Parameter::
00072 Resize(const Parameter::ValueCountType iNewSize, const std::string& sNewValue)
00073 {
00074 m_iLayerSize = iNewSize;
00075 for(LayersType::const_iterator pos = m_Layers.begin(); pos != m_Layers.end(); pos++)
00076 {
00077 (*pos)->Resize(iNewSize,sNewValue);
00078 };
00079 };
00080
00081 void
00082 Parameter::
00083 Resize(const ValueCountType iNewSize, const LayerCountType iNewLayerCount, const std::string& sNewValue)
00084 {
00085 Reset();
00086 Resize(iNewSize);
00087 for (LayerCountType i = 0; i<iNewLayerCount; i++) AddParameterLayer(false,sNewValue);
00088 };
00089
00090
00091 Parameter::ValueCountType
00092 Parameter::
00093 ParameterSize() const
00094 {
00095 return m_iLayerSize;
00096 };
00097
00098 Parameter::LayerCountType
00099 Parameter::
00100 LayerCount() const
00101 {
00102 return m_Layers.size();
00103 };
00104
00105 ParameterLayer*
00106 Parameter::
00107 GetParameterLayer(const Parameter::LayerCountType iLayer) const
00108 {
00109 if (iLayer>=LayerCount()) return NULL;
00110 return m_Layers[iLayer];
00111 };
00112
00113 bool
00114 Parameter::
00115 GetParameterValue(bool& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00116 {
00117 if (iLayer>=LayerCount()) return false;
00118 return m_Layers[iLayer]->GetValue(value,iItemPos);
00119 };
00120
00121 bool
00122 Parameter::
00123 GetParameterValue(double& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00124 {
00125 if (iLayer>=LayerCount()) return false;
00126 return m_Layers[iLayer]->GetValue(value,iItemPos);
00127 };
00128
00129 bool
00130 Parameter::
00131 GetParameterValue(int& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00132 {
00133 if (iLayer>=LayerCount()) return false;
00134 return m_Layers[iLayer]->GetValue(value,iItemPos);
00135 };
00136
00137
00138 bool
00139 Parameter::
00140 GetParameterValue(long& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00141 {
00142 if (iLayer>=LayerCount()) return false;
00143 return m_Layers[iLayer]->GetValue(value,iItemPos);
00144 };
00145
00146 bool
00147 Parameter::
00148 GetParameterValue(unsigned long& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00149 {
00150 if (iLayer>=LayerCount()) return false;
00151 return m_Layers[iLayer]->GetValue(value,iItemPos);
00152 };
00153
00154 bool
00155 Parameter::
00156 GetParameterValue(std::string& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer) const
00157 {
00158 if (iLayer>=LayerCount()) return false;
00159 return m_Layers[iLayer]->GetValue(value,iItemPos);
00160 };
00161
00162 bool
00163 Parameter::
00164 SetParameterValue(const bool value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00165 {
00166 if (iLayer>=LayerCount()) return false;
00167 return m_Layers[iLayer]->SetValue(value,iItemPos);
00168 };
00169
00170 bool
00171 Parameter::
00172 SetParameterValue(const double value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00173 {
00174 if (iLayer>=LayerCount()) return false;
00175 return m_Layers[iLayer]->SetValue(value,iItemPos);
00176 };
00177
00178 bool
00179 Parameter::
00180 SetParameterValue(const int value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00181 {
00182 if (iLayer>=LayerCount()) return false;
00183 return m_Layers[iLayer]->SetValue(value,iItemPos);
00184 };
00185
00186 bool
00187 Parameter::
00188 SetParameterValue(const long value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00189 {
00190 if (iLayer>=LayerCount()) return false;
00191 return m_Layers[iLayer]->SetValue(value,iItemPos);
00192 };
00193
00194 bool
00195 Parameter::
00196 SetParameterValue(const unsigned long value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00197 {
00198 if (iLayer>=LayerCount()) return false;
00199 return m_Layers[iLayer]->SetValue(value,iItemPos);
00200 };
00201
00202 bool
00203 Parameter::
00204 SetParameterValue(const std::string& value, const Parameter::ValueCountType iItemPos, const Parameter::LayerCountType iLayer)
00205 {
00206 if (iLayer>=LayerCount()) return false;
00207 return m_Layers[iLayer]->SetValue(value,iItemPos);
00208 };
00209
00210 bool
00211 Parameter::
00212 SetParameterLayer(const Parameter::LayerCountType iLayer, const ParameterLayer& rSourceLayer)
00213 {
00214 if (iLayer>=LayerCount()) return false;
00215 if (m_Layers[iLayer]->Size()!=rSourceLayer.Size()) return false;
00216
00217 *(m_Layers[iLayer]) = rSourceLayer;
00218
00219 return true;
00220 };
00221
00222 Parameter&
00223 Parameter::
00224 operator = (const Parameter& rParameter)
00225 {
00226 if (this == &rParameter) return *this;
00227
00228 Resize(rParameter.ParameterSize(),0);
00229
00230 for (LayerCountType i=0; i < rParameter.LayerCount(); i++)
00231 {
00232 *(m_Layers[AddParameterLayer(false)].GetPointer()) = *(rParameter.GetParameterLayer(i));
00233 };
00234
00235 m_sParameterName = rParameter.GetParameterName();
00236 m_sTemplateID = rParameter.GetTemplateID();
00237
00238 return *this;
00239 };
00240
00241 Parameter::
00242 Parameter(const std::string& sParameterName, const Parameter::ValueCountType iLayerSize, const Parameter::LayerCountType iLayerCount, const std::string& sDefaultValue):XMLStreamObject(FREE::cXML_Parameter)
00243 {
00244 Reset();
00245 m_sParameterName = sParameterName;
00246 m_sTemplateID = "";
00247 Resize(iLayerSize);
00248 for (LayerCountType i=0; i<iLayerCount; i++)
00249 {
00250 AddParameterLayer(false,sDefaultValue);
00251 }
00252 };
00253
00254 Parameter::
00255 Parameter():XMLStreamObject(FREE::cXML_Parameter)
00256 {
00257 m_sParameterName = "Param";
00258 m_sTemplateID = "";
00259 Reset();
00260 Resize(1,1,"");
00261 };
00262
00263 Parameter::Pointer
00264 Parameter::
00265 New(const std::string& sParameterName, const ValueCountType iLayerSize, const LayerCountType iLayerCount, const std::string& sDefaultValue)
00266 {
00267 Pointer smartPtr;
00268 Parameter *rawPtr = ::itk::ObjectFactory<Parameter>::Create();
00269 if(rawPtr == NULL)
00270 {
00271 rawPtr = new Parameter;
00272 }
00273 smartPtr = rawPtr;
00274 rawPtr->UnRegister();
00275
00276 rawPtr->SetParameterName(sParameterName);
00277 rawPtr->Resize(iLayerSize,iLayerCount,sDefaultValue);
00278
00279 return smartPtr;
00280 };
00281
00282 Parameter::
00283 ~Parameter()
00284 {
00285 Reset();
00286 };
00287
00288 void
00289 Parameter::
00290 SubElementLoadProcessing(const std::string& rsXMLSubTag, const std::string& rsXMLSubElement, const std::string& rsXMLSubData)
00291 {
00292 if (rsXMLSubTag==cXML_ParameterLayer)
00293 {
00294 ParameterLayer::Pointer smpNewLayer = ParameterLayer::New();
00295
00296 try
00297 {
00298 smpNewLayer->LoadFromString(rsXMLSubElement);
00299 }
00300 catchAllNPassMacro ("Unknown error occured while loading parameter layer.");
00301
00302
00303 if (LayerCount()==0) m_iLayerSize = smpNewLayer->Size();
00304 else if (smpNewLayer->Size()!=m_iLayerSize) throwExceptionMacro("Inconsistent layer size within one parameter. Layers of an one parameter must have same count of values. Please check setup file.");
00305
00306
00307 m_Layers.push_back(smpNewLayer);
00308 }
00309 else if (rsXMLSubTag!=cXML_Text) throwExceptionMacro("Unknown xml tag, unable to load value. Incorrect Tag: " << rsXMLSubTag);
00310 };
00311
00312
00313 std::string
00314 Parameter::
00315 SaveData(const unsigned int& iDepth, bool& bHasSubElements) const
00316 {
00317 std::string sResult;
00318
00319
00320 for(LayersType::const_iterator pos = m_Layers.begin(); pos != m_Layers.end(); pos++)
00321 {
00322 AddSubElement(sResult,(*pos),iDepth);
00323 };
00324 return sResult;
00325 };
00326
00327 void
00328 Parameter::
00329 AttributesLoadProcessing(const AttributesType& rAttributes)
00330 {
00331 for (AttributesType::const_iterator pos = rAttributes.begin(); pos!= rAttributes.end(); pos++)
00332 {
00333 if (pos->first==cXML_ParameterName) m_sParameterName = pos->second;
00334 else if (pos->first==cXML_TemplateID) m_sTemplateID = pos->second;
00335 else if (pos->first==cXML_ParameterSize) m_iLayerSize = Convert::ToInt(pos->second);
00336 };
00337 };
00338
00339 void
00340 Parameter::
00341 SaveAttributes(AttributesSaveListType& rAttributes) const
00342 {
00343 rAttributes.push_back(AttributesType::value_type(cXML_ParameterName,m_sParameterName));
00344 if (IsTemplateBased()) rAttributes.push_back(AttributesType::value_type(cXML_TemplateID,m_sTemplateID));
00345 if ((m_iLayerSize!=1)&&(m_Layers.size()==0)) rAttributes.push_back(AttributesType::value_type(cXML_ParameterSize,Convert::ToStr(m_iLayerSize)));
00346 };
00347
00348 namespace Convert
00349 {
00350 Parameter::ParameterValueType ToParameterValueType(const std::string& sValue)
00351 {
00352 if (sValue==csPVTBool) return Parameter::PVTBool;
00353 if (sValue==csPVTDouble) return Parameter::PVTDouble;
00354 if (sValue==csPVTInteger) return Parameter::PVTInteger;
00355 if (sValue==csPVTLong) return Parameter::PVTLong;
00356 if (sValue==csPVTULong) return Parameter::PVTULong;
00357 if (sValue==csPVTURI) return Parameter::PVTURI;
00358 if (sValue==csPVTIDPath) return Parameter::PVTIDPath;
00359
00360 return Parameter::PVTString;
00361 };
00362
00363 std::string ToStr(const Parameter::ParameterValueType& pt)
00364 {
00365 switch (pt)
00366 {
00367 case Parameter::PVTBool: return csPVTBool;
00368 case Parameter::PVTDouble: return csPVTDouble;
00369 case Parameter::PVTInteger: return csPVTInteger;
00370 case Parameter::PVTLong: return csPVTLong;
00371 case Parameter::PVTULong: return csPVTULong;
00372 case Parameter::PVTURI: return csPVTURI;
00373 case Parameter::PVTIDPath: return csPVTIDPath;
00374 default: return csPVTString;
00375 };
00376 };
00377 }
00378
00379 }