Parameter.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <iostream>
00019 #include <algorithm>
00020 #include <map>
00021 #include <vector>
00022 
00023 #include <OpenKarto/Parameter.h>
00024 #include <OpenKarto/Logger.h>
00025 
00026 namespace karto
00027 {
00028 
00032 
00033   ParameterDescription::ParameterDescription(const karto::String& rName, const karto::String& rDisplayName, const karto::String& rDescription)
00034     : m_Name(rName)
00035     , m_DisplayName(rDisplayName)
00036     , m_Description(rDescription)
00037     , m_Flags(ParameterFlag_Write | ParameterFlag_Read)
00038     , m_nDecimalPlaces(4)
00039   {
00040     assert(m_Name.Size() != 0);
00041 
00042     m_FieldNames[0] = "X";
00043     m_FieldNames[1] = "Y";
00044     m_FieldNames[2] = "Z";
00045     m_FieldNames[3] = "W";
00046   }
00047 
00048   ParameterDescription::~ParameterDescription()
00049   {
00050   }
00051 
00055 
00056   AbstractParameter::AbstractParameter(ParameterDescription* pDescription, ParameterSet* pParameterSet)
00057     : m_pDescription(pDescription)
00058     , m_pParameterSet(pParameterSet)
00059   {
00060     InitializeParameters();
00061   }
00062 
00063   AbstractParameter::~AbstractParameter()
00064   {
00065   }
00066 
00067   void AbstractParameter::InitializeParameters()
00068   {
00069     if (m_pParameterSet != NULL)
00070     {
00071       m_pParameterSet->AddParameter(this);
00072     }
00073   }
00074 
00078 
00079   struct ParameterSetPrivate
00080   {
00081     typedef std::map<karto::String, SmartPointer<AbstractParameter> > ParameterMap;
00082 
00083     ParameterList m_Parameters;
00084     ParameterMap m_ParametersMap;
00085   };
00086 
00087   ParameterSet::ParameterSet()
00088     : m_pPrivate(new ParameterSetPrivate())
00089   {
00090   }
00091 
00092   ParameterSet::~ParameterSet()
00093   {
00094     delete m_pPrivate;
00095   }
00096 
00097   void ParameterSet::AddParameter(AbstractParameter* pParameter)
00098   {
00099     if (pParameter != NULL)
00100     {
00101       if (m_pPrivate->m_ParametersMap.find(pParameter->GetName()) == m_pPrivate->m_ParametersMap.end())
00102       {
00103         m_pPrivate->m_ParametersMap[pParameter->GetName()] = pParameter;
00104         m_pPrivate->m_Parameters.Add(pParameter);
00105       }
00106       else
00107       {
00108         throw Exception("ParameterSet::AddParameter - Paramter with name: '" + pParameter->GetName() + "' already exists. Please rename property before adding again.");
00109       }
00110     }
00111   }
00112 
00113   void ParameterSet::RemoveParameter(AbstractParameter* pParameter)
00114   {
00115     if (pParameter != NULL)
00116     {
00117       ParameterSetPrivate::ParameterMap::iterator iter = m_pPrivate->m_ParametersMap.find(pParameter->GetName());
00118       if (iter != m_pPrivate->m_ParametersMap.end())
00119       {
00120         m_pPrivate->m_ParametersMap.erase(iter);
00121 
00122         m_pPrivate->m_Parameters.Remove(pParameter);
00123       }
00124     }
00125   }
00126 
00127   void ParameterSet::Clear()
00128   {
00129     m_pPrivate->m_ParametersMap.clear();
00130     m_pPrivate->m_Parameters.Clear();
00131   }
00132 
00133   const ParameterList& ParameterSet::GetParameters() const
00134   {
00135     return m_pPrivate->m_Parameters;
00136   }
00137 
00138   ParameterList& ParameterSet::GetParameters()
00139   {
00140     return m_pPrivate->m_Parameters;
00141   }
00142 
00143   AbstractParameter* ParameterSet::GetParameter(const karto::String& rParameterName) const
00144   {
00145     ParameterSetPrivate::ParameterMap::const_iterator iter = m_pPrivate->m_ParametersMap.find(rParameterName);
00146     if (iter != m_pPrivate->m_ParametersMap.end())
00147     {
00148       return iter->second;
00149     }
00150 
00151     return NULL;
00152   }
00153 
00154   AbstractParameter* ParameterSet::GetParameter(const karto::String& rParameterName)
00155   {
00156     ParameterSetPrivate::ParameterMap::iterator iter = m_pPrivate->m_ParametersMap.find(rParameterName);
00157     if (iter != m_pPrivate->m_ParametersMap.end())
00158     {
00159       return iter->second;
00160     }
00161 
00162     return NULL;
00163   }
00164 
00168 
00169   struct ParameterEnumPrivate
00170   {
00171     typedef std::vector<EnumPair> EnumPairVector;
00172     EnumPairVector m_EnumPairs;
00173   };
00174 
00175   ParameterEnum::ParameterEnum(ParameterSet* pParent, const karto::String& rName, const karto::String& rDisplayName, const karto::String& rDescription, kt_int64s value)
00176     : Parameter<kt_int64s>(pParent, rName, rDisplayName, rDescription, value)
00177     , m_pPrivate(new ParameterEnumPrivate)
00178   {
00179   }
00180 
00181   ParameterEnum::~ParameterEnum()
00182   {
00183     delete m_pPrivate;
00184   }
00185 
00186   void ParameterEnum::SetValueFromString(const String& rStringValue)
00187   {
00188     ParameterEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rStringValue));
00189     if (iter == m_pPrivate->m_EnumPairs.end())
00190     {
00191       StringBuilder validValues;
00192 
00193       forEach(ParameterEnumPrivate::EnumPairVector, &(m_pPrivate->m_EnumPairs))
00194       {
00195         validValues << iter->name << ", ";
00196       }
00197 
00198       throw Exception("ParameterEnum::SetValueFromString - Unable to set enum: '" + rStringValue + "'. Valid values are: " + validValues.ToString());
00199     }
00200     else
00201     {
00202       SetValue(iter->value);
00203     }
00204   }
00205 
00206   const String ParameterEnum::GetValueAsString() const
00207   {
00208     ParameterEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByValue(m_Value));
00209     if (iter == m_pPrivate->m_EnumPairs.end())
00210     {
00211       throw Exception("ParameterEnum::GetValueAsString - Unable to lookup enum");
00212     }
00213     else
00214     {
00215       return iter->name;
00216     }
00217   }
00218 
00219   void ParameterEnum::DefineEnumValue(const String& rName, kt_int64s value)
00220   {
00221     ParameterEnumPrivate::EnumPairVector::iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rName));
00222     if (iter == m_pPrivate->m_EnumPairs.end())
00223     {
00224       EnumPair enumPair;
00225       enumPair.name = rName;
00226       enumPair.value = value;
00227       m_pPrivate->m_EnumPairs.push_back(enumPair);
00228     }
00229     else
00230     {
00231       karto::Log(karto::LOG_WARNING, "ParameterEnum::DefineEnumValue - Overriding enum value: " + rName + " with " + StringHelper::ToString(value));
00232 
00233       iter->value = value;
00234     }
00235   }
00236 
00237   const EnumPairList ParameterEnum::GetEnumValues() const
00238   {
00239     EnumPairList enumPairList;
00240 
00241     forEach(ParameterEnumPrivate::EnumPairVector, &(m_pPrivate->m_EnumPairs))
00242     {
00243       enumPairList.Add(*iter);
00244     }
00245 
00246     return enumPairList;
00247   }
00248 
00249 }


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Sun Apr 2 2017 04:05:24