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/FromString.h>
57 #include <utils/ToString.h>
58 
59 namespace mira {
60 
62 
84 {
85 public:
86 
87  typedef std::list<std::pair<std::string, std::string>> AttributeValueList;
88 
89 public:
90 
91  PropertyHint() : mAttributes(NULL) {}
92 
94  PropertyHint(const std::string& attribute, const std::string& value) {
95  mAttributes = new AttributeValueList;
96  mAttributes->push_back(std::make_pair(attribute, value));
97  }
98 
100  PropertyHint(PropertyHint&& other) noexcept : mAttributes(NULL) {
101  std::swap(mAttributes, other.mAttributes);
102  }
103 
105  delete mAttributes;
106  }
107 
108 public:
109 
112  return mAttributes ? *mAttributes : AttributeValueList();
113  }
114 
116  void fromList(const AttributeValueList& list) {
117 
118  if(list.empty()) {
119  delete mAttributes;
120  mAttributes = NULL;
121  return;
122  }
123 
124  if(!mAttributes)
125  mAttributes = new AttributeValueList;
126 
127  *mAttributes = list;
128  }
129 
130 public:
131 
133  PropertyHint& operator=(PropertyHint&& other) noexcept {
134  std::swap(mAttributes, other.mAttributes);
135  return *this;
136  }
137 
138 
139 public:
140 
147  {
148  PropertyHint copy;
149  if(mAttributes!=NULL) {
150  copy.mAttributes = new AttributeValueList;
151  *copy.mAttributes = *mAttributes;
152  }
153  return copy;
154  }
155 
156 public:
157 
159  bool has(const std::string& attribute) const
160  {
161  if(mAttributes!=NULL) {
162  for(auto it=mAttributes->begin(); it!=mAttributes->end(); ++it)
163  {
164  if(it->first==attribute)
165  return true;
166  }
167  }
168  return false;
169  }
170 
175  template <typename T>
176  T get(const std::string& attribute, const T& defaultValue = T()) const
177  {
178  if(mAttributes!=NULL) {
179  for(auto it=mAttributes->begin(); it!=mAttributes->end(); ++it)
180  {
181  if(it->first==attribute)
182  return fromString<T>(it->second);
183  }
184  }
185  return defaultValue;
186  }
187 
188 public:
189 
196  friend PropertyHint operator|(PropertyHint&& hint, PropertyHint&& otherHint)
197  {
198  if(otherHint.mAttributes==NULL)
199  return std::move(hint);
200 
201  if(hint.mAttributes==NULL) {
202  std::swap(hint.mAttributes, otherHint.mAttributes);
203  return std::move(hint);
204  }
205 
206  assert(hint.mAttributes);
207  assert(otherHint.mAttributes);
208 
209  // move attributes from otherHint to hint
210  hint.mAttributes->splice(hint.mAttributes->begin(), *otherHint.mAttributes,
211  otherHint.mAttributes->begin(), otherHint.mAttributes->end());
212  return std::move(hint);
213  }
214 
215 private:
217  PropertyHint(PropertyHint& other);
220 
221  AttributeValueList* mAttributes;
222 };
223 
234 namespace PropertyHints {
235 
237 
243 template <typename T>
244 inline PropertyHint minimum(const T& min) {
245  return PropertyHint("minimum",toString(min));
246 }
247 
253 template <typename T>
254 inline PropertyHint maximum(const T& max) {
255  return PropertyHint("maximum",toString(max));
256 }
257 
265 template <typename T>
266 inline PropertyHint step(const T& step) {
267  return PropertyHint("step",toString(step));
268 }
269 
275 template <typename T>
276 inline PropertyHint limits(const T& min, const T& max) {
277  return minimum<T>(min) | maximum<T>(max);
278 }
279 
286 inline PropertyHint precision(int p) {
287  return PropertyHint("precision", toString(p));
288 }
289 
296 inline PropertyHint type(const std::string& t) {
297  return PropertyHint("type", t);
298 }
299 
307 inline PropertyHint enumeration(const std::string& values) {
308  return PropertyHint("enumeration", values);
309 }
310 
317 template <typename T>
318 inline PropertyHint slider(const T& min, const T& max, const T& s) {
319  return type("slider") | limits<T>(min,max) | step<T>(s);
320 }
321 
328 template <typename T>
329 inline PropertyHint spin(const T& min, const T& max, const T& s) {
330  return type("spin") | limits<T>(min,max) | step<T>(s);
331 }
332 
334 
335 } // namespace
336 
338 
339 } // namespace
340 
341 #endif /* _MIRA_PROPERTYHINT_H_ */
void fromList(const AttributeValueList &list)
Sets the property hints from the specified list of attribute/value pairs.
Definition: PropertyHint.h:116
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:329
PropertyHint maximum(const T &max)
Sets the attribute "maximum" to the specified value.
Definition: PropertyHint.h:254
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Contains fromString functions for converting strings to data types.
PropertyHint enumeration(const std::string &values)
Sets the attribute "enumeration".
Definition: PropertyHint.h:307
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:318
PropertyHint & operator=(PropertyHint &&other) noexcept
move assignment operator
Definition: PropertyHint.h:133
Contains toString functions for converting data types to strings.
std::list< std::pair< std::string, std::string > > AttributeValueList
Definition: PropertyHint.h:87
bool has(const std::string &attribute) const
Returns true if the specified attribute exists.
Definition: PropertyHint.h:159
AttributeValueList toList() const
Returns the attributes/value list containing the property hints.
Definition: PropertyHint.h:111
A property hint gives optional instructions to the property editor, i.e.
Definition: PropertyHint.h:83
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:158
PropertyHint(const std::string &attribute, const std::string &value)
Constructs a single hint from the given attribute value pair.
Definition: PropertyHint.h:94
PropertyHint limits(const T &min, const T &max)
Sets both attributes "minimum" and "maximum" to the specified values.
Definition: PropertyHint.h:276
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:296
friend PropertyHint operator|(PropertyHint &&hint, PropertyHint &&otherHint)
Concatenates two hints.
Definition: PropertyHint.h:196
PropertyHint minimum(const T &min)
Sets the attribute "minimum" to the specified value.
Definition: PropertyHint.h:244
PropertyHint(PropertyHint &&other) noexcept
move constructor
Definition: PropertyHint.h:100
PropertyHint step(const T &step)
Sets the attribute "step" to the specified value.
Definition: PropertyHint.h:266
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:286
PropertyHint()
Definition: PropertyHint.h:91
PropertyHint clone() const
Creates an explicit copy as replacement for the copy constructor.
Definition: PropertyHint.h:146
~PropertyHint()
Definition: PropertyHint.h:104