MIRA
PropertyHint.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_PROPERTYHINT_H_
48 #define _MIRA_PROPERTYHINT_H_
49 
50 #include <assert.h>
51 
52 #include <list>
53 #include <string>
54 #include <utility>
55 
56 #include <utils/ToString.h>
57 
58 namespace mira {
59 
61 
83 {
84 public:
85 
86  typedef std::list<std::pair<std::string, std::string>> AttributeValueList;
87 
88 public:
89 
90  PropertyHint() : mAttributes(NULL) {}
91 
93  PropertyHint(const std::string& attribute, const std::string& value) {
94  mAttributes = new AttributeValueList;
95  mAttributes->push_back(std::make_pair(attribute, value));
96  }
97 
99  PropertyHint(PropertyHint&& other) noexcept : mAttributes(NULL) {
100  std::swap(mAttributes, other.mAttributes);
101  }
102 
104  delete mAttributes;
105  }
106 
107 public:
108 
111  return mAttributes ? *mAttributes : AttributeValueList();
112  }
113 
115  void fromList(const AttributeValueList& list) {
116 
117  if(list.empty()) {
118  delete mAttributes;
119  mAttributes = NULL;
120  return;
121  }
122 
123  if(!mAttributes)
124  mAttributes = new AttributeValueList;
125 
126  *mAttributes = list;
127  }
128 
129 public:
130 
132  PropertyHint& operator=(PropertyHint&& other) noexcept {
133  std::swap(mAttributes, other.mAttributes);
134  return *this;
135  }
136 
137 
138 public:
139 
146  {
147  PropertyHint copy;
148  if(mAttributes!=NULL) {
149  copy.mAttributes = new AttributeValueList;
150  *copy.mAttributes = *mAttributes;
151  }
152  return copy;
153  }
154 
155 public:
156 
158  bool has(const std::string& attribute) const
159  {
160  if(mAttributes!=NULL) {
161  for(auto it=mAttributes->begin(); it!=mAttributes->end(); ++it)
162  {
163  if(it->first==attribute)
164  return true;
165  }
166  }
167  return false;
168  }
169 
174  template <typename T>
175  T get(const std::string& attribute, const T& defaultValue = T()) const
176  {
177  if(mAttributes!=NULL) {
178  for(auto it=mAttributes->begin(); it!=mAttributes->end(); ++it)
179  {
180  if(it->first==attribute)
181  return fromString<T>(it->second);
182  }
183  }
184  return defaultValue;
185  }
186 
187 public:
188 
195  friend PropertyHint operator|(PropertyHint&& hint, PropertyHint&& otherHint)
196  {
197  if(otherHint.mAttributes==NULL)
198  return std::move(hint);
199 
200  if(hint.mAttributes==NULL) {
201  std::swap(hint.mAttributes, otherHint.mAttributes);
202  return std::move(hint);
203  }
204 
205  assert(hint.mAttributes);
206  assert(otherHint.mAttributes);
207 
208  // move attributes from otherHint to hint
209  hint.mAttributes->splice(hint.mAttributes->begin(), *otherHint.mAttributes,
210  otherHint.mAttributes->begin(), otherHint.mAttributes->end());
211  return std::move(hint);
212  }
213 
214 private:
216  PropertyHint(PropertyHint& other);
219 
220  AttributeValueList* mAttributes;
221 };
222 
233 namespace PropertyHints {
234 
236 
242 template <typename T>
243 inline PropertyHint minimum(const T& min) {
244  return PropertyHint("minimum",toString(min));
245 }
246 
252 template <typename T>
253 inline PropertyHint maximum(const T& max) {
254  return PropertyHint("maximum",toString(max));
255 }
256 
264 template <typename T>
265 inline PropertyHint step(const T& step) {
266  return PropertyHint("step",toString(step));
267 }
268 
274 template <typename T>
275 inline PropertyHint limits(const T& min, const T& max) {
276  return minimum<T>(min) | maximum<T>(max);
277 }
278 
285 inline PropertyHint precision(int p) {
286  return PropertyHint("precision", toString(p));
287 }
288 
295 inline PropertyHint type(const std::string& t) {
296  return PropertyHint("type", t);
297 }
298 
306 inline PropertyHint enumeration(const std::string& values) {
307  return PropertyHint("enumeration", values);
308 }
309 
316 template <typename T>
317 inline PropertyHint slider(const T& min, const T& max, const T& s) {
318  return type("slider") | limits<T>(min,max) | step<T>(s);
319 }
320 
327 template <typename T>
328 inline PropertyHint spin(const T& min, const T& max, const T& s) {
329  return type("spin") | limits<T>(min,max) | step<T>(s);
330 }
331 
333 
334 } // namespace
335 
337 
338 } // namespace
339 
340 #endif /* _MIRA_PROPERTYHINT_H_ */
void fromList(const AttributeValueList &list)
Sets the property hints from the specified list of attribute/value pairs.
Definition: PropertyHint.h:115
PropertyHint spin(const T &min, const T &max, const T &s)
Sets the attribute "type" to the value "spinbox" and sets the "minimum", "maximum" and "step" attribu...
Definition: PropertyHint.h:328
PropertyHint maximum(const T &max)
Sets the attribute "maximum" to the specified value.
Definition: PropertyHint.h:253
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
PropertyHint enumeration(const std::string &values)
Sets the attribute "enumeration".
Definition: PropertyHint.h:306
PropertyHint slider(const T &min, const T &max, const T &s)
Sets the attribute "type" to the value "slider" and sets the "minimum", "maximum" and "step" attribut...
Definition: PropertyHint.h:317
PropertyHint & operator=(PropertyHint &&other) noexcept
move assignment operator
Definition: PropertyHint.h:132
Contains toString and fromString functions for converting data types to strings and the other way rou...
std::list< std::pair< std::string, std::string > > AttributeValueList
Definition: PropertyHint.h:86
bool has(const std::string &attribute) const
Returns true if the specified attribute exists.
Definition: PropertyHint.h:158
AttributeValueList toList() const
Returns the attributes/value list containing the property hints.
Definition: PropertyHint.h:110
A property hint gives optional instructions to the property editor, i.e.
Definition: PropertyHint.h:82
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
PropertyHint(const std::string &attribute, const std::string &value)
Constructs a single hint from the given attribute value pair.
Definition: PropertyHint.h:93
PropertyHint limits(const T &min, const T &max)
Sets both attributes "minimum" and "maximum" to the specified values.
Definition: PropertyHint.h:275
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
friend PropertyHint operator|(PropertyHint &&hint, PropertyHint &&otherHint)
Concatenates two hints.
Definition: PropertyHint.h:195
PropertyHint minimum(const T &min)
Sets the attribute "minimum" to the specified value.
Definition: PropertyHint.h:243
PropertyHint(PropertyHint &&other) noexcept
move constructor
Definition: PropertyHint.h:99
PropertyHint step(const T &step)
Sets the attribute "step" to the specified value.
Definition: PropertyHint.h:265
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:285
PropertyHint()
Definition: PropertyHint.h:90
PropertyHint clone() const
Creates an explicit copy as replacement for the copy constructor.
Definition: PropertyHint.h:145
~PropertyHint()
Definition: PropertyHint.h:103