Parameter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <iostream>
19 #include <algorithm>
20 #include <map>
21 #include <vector>
22 
23 #include <OpenKarto/Parameter.h>
24 #include <OpenKarto/Logger.h>
25 
26 namespace karto
27 {
28 
32 
33  ParameterDescription::ParameterDescription(const karto::String& rName, const karto::String& rDisplayName, const karto::String& rDescription)
34  : m_Name(rName)
35  , m_DisplayName(rDisplayName)
36  , m_Description(rDescription)
38  , m_nDecimalPlaces(4)
39  {
40  assert(m_Name.Size() != 0);
41 
42  m_FieldNames[0] = "X";
43  m_FieldNames[1] = "Y";
44  m_FieldNames[2] = "Z";
45  m_FieldNames[3] = "W";
46  }
47 
49  {
50  }
51 
55 
57  : m_pDescription(pDescription)
58  , m_pParameterSet(pParameterSet)
59  {
61  }
62 
63  AbstractParameter::~AbstractParameter()
64  {
65  }
66 
68  {
69  if (m_pParameterSet != NULL)
70  {
72  }
73  }
74 
78 
80  {
81  typedef std::map<karto::String, SmartPointer<AbstractParameter> > ParameterMap;
82 
84  ParameterMap m_ParametersMap;
85  };
86 
88  : m_pPrivate(new ParameterSetPrivate())
89  {
90  }
91 
92  ParameterSet::~ParameterSet()
93  {
94  delete m_pPrivate;
95  }
96 
98  {
99  if (pParameter != NULL)
100  {
101  if (m_pPrivate->m_ParametersMap.find(pParameter->GetName()) == m_pPrivate->m_ParametersMap.end())
102  {
103  m_pPrivate->m_ParametersMap[pParameter->GetName()] = pParameter;
104  m_pPrivate->m_Parameters.Add(pParameter);
105  }
106  else
107  {
108  throw Exception("ParameterSet::AddParameter - Paramter with name: '" + pParameter->GetName() + "' already exists. Please rename property before adding again.");
109  }
110  }
111  }
112 
114  {
115  if (pParameter != NULL)
116  {
117  ParameterSetPrivate::ParameterMap::iterator iter = m_pPrivate->m_ParametersMap.find(pParameter->GetName());
118  if (iter != m_pPrivate->m_ParametersMap.end())
119  {
120  m_pPrivate->m_ParametersMap.erase(iter);
121 
122  m_pPrivate->m_Parameters.Remove(pParameter);
123  }
124  }
125  }
126 
128  {
129  m_pPrivate->m_ParametersMap.clear();
131  }
132 
134  {
135  return m_pPrivate->m_Parameters;
136  }
137 
139  {
140  return m_pPrivate->m_Parameters;
141  }
142 
144  {
145  ParameterSetPrivate::ParameterMap::const_iterator iter = m_pPrivate->m_ParametersMap.find(rParameterName);
146  if (iter != m_pPrivate->m_ParametersMap.end())
147  {
148  return iter->second;
149  }
150 
151  return NULL;
152  }
153 
155  {
156  ParameterSetPrivate::ParameterMap::iterator iter = m_pPrivate->m_ParametersMap.find(rParameterName);
157  if (iter != m_pPrivate->m_ParametersMap.end())
158  {
159  return iter->second;
160  }
161 
162  return NULL;
163  }
164 
168 
170  {
171  typedef std::vector<EnumPair> EnumPairVector;
172  EnumPairVector m_EnumPairs;
173  };
174 
175  ParameterEnum::ParameterEnum(ParameterSet* pParent, const karto::String& rName, const karto::String& rDisplayName, const karto::String& rDescription, kt_int64s value)
176  : Parameter<kt_int64s>(pParent, rName, rDisplayName, rDescription, value)
178  {
179  }
180 
181  ParameterEnum::~ParameterEnum()
182  {
183  delete m_pPrivate;
184  }
185 
186  void ParameterEnum::SetValueFromString(const String& rStringValue)
187  {
188  ParameterEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rStringValue));
189  if (iter == m_pPrivate->m_EnumPairs.end())
190  {
191  StringBuilder validValues;
192 
194  {
195  validValues << iter->name << ", ";
196  }
197 
198  throw Exception("ParameterEnum::SetValueFromString - Unable to set enum: '" + rStringValue + "'. Valid values are: " + validValues.ToString());
199  }
200  else
201  {
202  SetValue(iter->value);
203  }
204  }
205 
207  {
208  ParameterEnumPrivate::EnumPairVector::const_iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByValue(m_Value));
209  if (iter == m_pPrivate->m_EnumPairs.end())
210  {
211  throw Exception("ParameterEnum::GetValueAsString - Unable to lookup enum");
212  }
213  else
214  {
215  return iter->name;
216  }
217  }
218 
220  {
221  ParameterEnumPrivate::EnumPairVector::iterator iter = std::find_if(m_pPrivate->m_EnumPairs.begin(), m_pPrivate->m_EnumPairs.end(), FindByName(rName));
222  if (iter == m_pPrivate->m_EnumPairs.end())
223  {
224  EnumPair enumPair;
225  enumPair.name = rName;
226  enumPair.value = value;
227  m_pPrivate->m_EnumPairs.push_back(enumPair);
228  }
229  else
230  {
231  karto::Log(karto::LOG_WARNING, "ParameterEnum::DefineEnumValue - Overriding enum value: " + rName + " with " + StringHelper::ToString(value));
232 
233  iter->value = value;
234  }
235  }
236 
238  {
239  EnumPairList enumPairList;
240 
242  {
243  enumPairList.Add(*iter);
244  }
245 
246  return enumPairList;
247  }
248 
249 }
ParameterEnum(ParameterSet *pParameterSet, const karto::String &rName, const karto::String &rDisplayName, const karto::String &rDescription, kt_int64s value)
Definition: Parameter.cpp:175
ParameterSet * m_pParameterSet
Definition: Parameter.h:331
#define forEach(listtype, list)
Definition: Macros.h:88
virtual void Add(const T &rValue)
Definition: List.h:111
virtual kt_bool Remove(const T &rValue)
Definition: List.h:148
void RemoveParameter(AbstractParameter *pParameter)
Definition: Parameter.cpp:113
AbstractParameter(ParameterDescription *pDescription, ParameterSet *pParameterSet=NULL)
Definition: Parameter.cpp:56
virtual void SetValue(const kt_int64s &rValue)
Definition: Parameter.h:492
const ParameterList & GetParameters() const
Definition: Parameter.cpp:133
void Log(LogLevel level, const karto::String &rMessage)
Definition: Logger.cpp:183
std::vector< EnumPair > EnumPairVector
Definition: Parameter.cpp:171
const karto::String & GetName() const
Definition: Parameter.h:251
karto::String name
Definition: MetaEnum.h:58
karto::String m_FieldNames[4]
Definition: Parameter.h:198
static String ToString(const char *value)
std::map< karto::String, SmartPointer< AbstractParameter > > ParameterMap
Definition: Parameter.cpp:81
ParameterDescription(const karto::String &rName, const karto::String &rDisplayName="", const karto::String &rDescription="")
Definition: Parameter.cpp:33
AbstractParameter * GetParameter(const karto::String &rParameterName) const
Definition: Parameter.cpp:143
void AddParameter(AbstractParameter *pParameter)
Definition: Parameter.cpp:97
const String & ToString() const
void DefineEnumValue(const String &rName, kt_int64s value)
Definition: Parameter.cpp:219
kt_int64s value
Definition: MetaEnum.h:63
signed long long kt_int64s
Definition: Types.h:127
virtual void SetValueFromString(const karto::String &rStringValue)
Definition: Parameter.cpp:186
virtual const karto::String GetValueAsString() const
Definition: Parameter.cpp:206
Definition: Any.cpp:20
ParameterSetPrivate * m_pPrivate
Definition: Parameter.h:420
virtual void Clear()
Definition: List.h:231
ParameterList m_Parameters
Definition: Parameter.cpp:83
kt_size_t Size() const
Definition: String.cpp:77
EnumPairVector m_EnumPairs
Definition: Parameter.cpp:172
ParameterMap m_ParametersMap
Definition: Parameter.cpp:84
virtual void InitializeParameters()
Definition: Parameter.cpp:67
ParameterEnumPrivate * m_pPrivate
Definition: Parameter.h:801
const EnumPairList GetEnumValues() const
Definition: Parameter.cpp:237


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36