libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNConfigElement.h
Go to the documentation of this file.
1 /* Copyright 2011-2016 CoReNum, INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNConfigElement.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNConfigElement_HEADER
23 #define CRNConfigElement_HEADER
24 
25 #include <CRNData/CRNVector.h>
26 #include <CRNData/CRNData.h>
27 
28 namespace crn
29 {
40  {
41  public:
43  ConfigElement();
45  ConfigElement(const String& nam, int val, const String& desc = U"");
47  ConfigElement(const String& nam, double val, const String& desc = U"");
49  ConfigElement(const String& nam, bool val, const String& desc = U"");
51  ConfigElement(const String& nam, const Prop3& val, const String& desc = U"");
53  ConfigElement(const String& nam, const String& val, const String& desc = U"");
55  ConfigElement(const String& nam, const StringUTF8& val, const String& desc = U"");
57  ConfigElement(const String& nam, const Path& val, const String& desc = U"");
58  ConfigElement(const ConfigElement&) = delete;
59  ConfigElement(ConfigElement&&) = default;
60  ConfigElement& operator=(const ConfigElement&) = delete;
62  ~ConfigElement() = default;
63 
65  String GetType() const;
67  const String& GetName() const noexcept { return name; }
69  const String& GetDescription() const noexcept { return description; }
71  const SObject& GetValue() noexcept { return value; }
73  SCObject GetValue() const { return value; }
78  template<typename T> T GetValue() const
79  {
80  return Data::Convert<T>(*value);
81  }
86  template<typename T> void SetValue(T val)
87  {
88  setValue(val, value);
89  }
91  bool HasMinValue() const noexcept { return minValue.get() != nullptr; }
93  const SObject& GetMinValue() noexcept { return minValue; }
95  SCObject GetMinValue() const { return minValue; }
100  template<typename T> T GetMinValue() const
101  {
102  if (!minValue)
103  throw ExceptionUninitialized("The element has no minimal value.");
104  return Data::Convert<T>(*minValue);
105  }
110  template<typename T> void SetMinValue(T val)
111  {
112  const String t(GetType());
113  if (!minValue)
114  minValue = Clone(*value);
115  setValue(val, minValue);
116  }
118  bool HasMaxValue() const noexcept { return maxValue.get() != nullptr; }
120  const SObject& GetMaxValue() noexcept { return maxValue; }
122  SCObject GetMaxValue() const { return maxValue; }
127  template<typename T> T GetMaxValue() const
128  {
129  if (!maxValue)
130  throw ExceptionUninitialized("The element has no maximal value.");
131  return Data::Convert<T>(*maxValue);
132  }
137  template<typename T> void SetMaxValue(T val)
138  {
139  const String t(GetType());
140  if (!maxValue)
141  maxValue = Clone(*value);
142  setValue(val, maxValue);
143  }
144 
146  const crn::Vector& GetAllowedValues() const { return allowedValues; }
151  template<typename T> const std::vector<T> GetAllowedValues() const
152  {
153  std::vector<T> lst;
154  for (SCObject obj : allowedValues)
155  {
156  lst.push_back(Data::Convert<T>(*obj));
157  }
158  return lst;
159  }
164  template<typename Iter> void SetAllowedValues(Iter b, Iter e)
165  {
166  allowedValues.Clear();
167  for (Iter tmp = b; tmp != e; ++tmp)
168  AddAllowedValue(*tmp);
169  }
173  template<typename T> void AddAllowedValue(T val)
174  {
175  allowedValues.PushBack(Clone(*value));
176  setValue(val, allowedValues.Back());
177  }
178 
179  private:
184  template<typename T> static void setValue(T val, const SObject &value)
185  {
186  const String valstring(val);
187  auto i = std::static_pointer_cast<Int>(value);
188  if (i)
189  {
190  *i = valstring.ToInt();
191  return;
192  }
193  auto r = std::static_pointer_cast<Real>(value);
194  if (r)
195  {
196  *r = valstring.ToDouble();
197  return;
198  }
199  auto p = std::static_pointer_cast<Prop3>(value);
200  if (p)
201  {
202  *p = valstring.ToProp3();
203  return;
204  }
205  auto s = std::static_pointer_cast<String>(value);
206  if (s)
207  {
208  *s = valstring;
209  return;
210  }
211  auto su = std::static_pointer_cast<StringUTF8>(value);
212  if (su)
213  {
214  *su = valstring.CStr();
215  return;
216  }
217  auto pa = std::static_pointer_cast<Path>(value);
218  if (pa)
219  {
220  *pa = valstring.CStr();
221  return;
222  }
223  }
224 
225  SObject value;
226  SObject minValue;
227  SObject maxValue;
228  String name;
229  String description;
230  Vector allowedValues;
231  };
232 
233  CRN_ALIAS_SMART_PTR(ConfigElement)
234 }
235 
236 #endif
237 
238 
SCObject GetMaxValue() const
Gets the inner representation of the element's max value.
SCObject GetValue() const
Gets the inner representation of the element's value.
const SObject & GetMinValue() noexcept
Gets the inner representation of the element's min value.
T GetMinValue() const
Gets the element's minimal value.
const crn::Vector & GetAllowedValues() const
Returns the list of allowed values.
ConfigElement()
Default constructor for serialization.
Unintialized object error.
Definition: CRNException.h:155
SObject & Back()
Returns a reference to the last element.
Definition: CRNVector.h:74
bool HasMaxValue() const noexcept
Tells if the element has max value.
T GetValue() const
Gets the element's value.
SCObject GetMinValue() const
Gets the inner representation of the element's min value.
const std::vector< T > GetAllowedValues() const
Returns the list of allowed values.
A UTF32 character string class.
Definition: CRNString.h:61
void AddAllowedValue(T val)
Adds a value to the list of allowed values.
const SObject & GetMaxValue() noexcept
Gets the inner representation of the element's max value.
ConfigElement & operator=(const ConfigElement &)=delete
A convenience class for file paths.
Definition: CRNPath.h:39
void Clear() noexcept
Empties the vector.
Definition: CRNVector.h:96
Data vector class.
Definition: CRNVector.h:42
void SetAllowedValues(Iter b, Iter e)
Sets the list of allowed values.
const String & GetName() const noexcept
Gets the translated name of the element.
void SetMinValue(T val)
Sets the element's min value.
A element of configuration.
String GetType() const
Gets the inner type of data in the element.
const String & GetDescription() const noexcept
Gets the description of the element.
const SObject & GetValue() noexcept
Gets the inner representation of the element's value.
void SetMaxValue(T val)
Sets the element's max value.
void SetValue(T val)
Sets the element's value.
Interface class for the metric real number class.
Definition: CRNInt.h:40
CRN_ALIAS_SMART_PTR(ImageBW)
A character string class.
Definition: CRNStringUTF8.h:49
void PushBack(const SObject &d)
Adds an object at the end of the vector.
Definition: CRNVector.cpp:61
T GetMaxValue() const
Gets the element's minimal value.
UObject Clone(const Object &obj)
Clones an object if possible.
Definition: CRNObject.cpp:35
A ternary proposition.
Definition: CRNProp3.h:40
~ConfigElement()=default
bool HasMinValue() const noexcept
Tells if the element has min value.