XMM - Probabilistic Models for Motion Recognition and Mapping

xmmAttribute.hpp
Go to the documentation of this file.
1 /*
2  * xmmAttribute.hpp
3  *
4  * Generic Attributes
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmAttribute_h
34 #define xmmAttribute_h
35 
36 #include <functional>
37 #include <limits>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 
42 namespace xmm {
43 
44 #pragma mark -
45 #pragma mark === Functions checkLimits ===
46 
54 template <typename T>
55 void checkLimits(T const& value, T const& limit_min, T const& limit_max) {
56  throw std::runtime_error(
57  "Attribute limits are not implemented for the current type.");
58 }
59 
60 template <>
61 void checkLimits<bool>(bool const& value, bool const& limit_min,
62  bool const& limit_max);
63 
64 template <>
65 void checkLimits<unsigned char>(unsigned char const& value,
66  unsigned char const& limit_min,
67  unsigned char const& limit_max);
68 
69 template <>
70 void checkLimits<char>(char const& value, char const& limit_min,
71  char const& limit_max);
72 
73 template <>
74 void checkLimits<unsigned int>(unsigned int const& value,
75  unsigned int const& limit_min,
76  unsigned int const& limit_max);
77 
78 template <>
79 void checkLimits<int>(int const& value, int const& limit_min,
80  int const& limit_max);
81 
82 template <>
83 void checkLimits<long>(long const& value, long const& limit_min,
84  long const& limit_max);
85 
86 template <>
87 void checkLimits<float>(float const& value, float const& limit_min,
88  float const& limit_max);
89 
90 template <>
91 void checkLimits<double>(double const& value, double const& limit_min,
92  double const& limit_max);
93 
94 template <>
95 void checkLimits<std::string>(std::string const& value,
96  std::string const& limit_min,
97  std::string const& limit_max);
98 
99 #pragma mark -
100 #pragma mark === Class AttributeBase ===
101 
106  public:
107  virtual ~AttributeBase() {}
108 
112  AttributeBase() : changed(false) {}
113 
117  bool changed;
118 };
119 
120 #pragma mark -
121 #pragma mark === Class Attribute ===
122 
127 template <typename T>
128 class Attribute : public AttributeBase {
129  public:
130  typedef std::function<void(AttributeBase*)> AttributeChangeCallback;
131 
132  template <typename U, typename args, class ListenerClass>
133  void onAttributeChange(U* owner,
134  void (ListenerClass::*listenerMethod)(args)) {
135  using namespace std::placeholders;
136  if (owner)
137  callback_ = std::bind(listenerMethod, owner, _1);
138  else
139  callback_ = nullptr;
140  }
141 
148  Attribute(T const& value = T(), T const& limit_min = defaultLimitMin(),
149  T const& limit_max = defaultLimitMax())
150  : limit_min_(limit_min), limit_max_(limit_max) {
151  set(value, true);
152  changed = false;
153  }
154 
160  Attribute(Attribute const& src)
161  : value_(src.value_),
162  limit_min_(src.limit_min_),
163  limit_max_(src.limit_max_) {
164  changed = false;
165  }
166 
173  template <typename U>
175  if (this != &src) {
176  value_ = src.value_;
177  limit_min_ = src.limit_min_;
178  limit_max_ = src.limit_max_;
179  changed = false;
180  }
181  return *this;
182  }
183 
192  void set(T const& value, bool silently = false) {
193  checkLimits(value, limit_min_, limit_max_);
194  value_ = value;
195  changed = true;
196  if (!silently && callback_) callback_(this);
197  }
198 
203  T get() const { return value_; }
204 
209  void setLimitMin(T const& limit_min = defaultLimitMin()) {
210  limit_min_ = limit_min;
211  }
212 
217  void setLimitMax(T const& limit_max = defaultLimitMax()) {
218  limit_max_ = limit_max;
219  }
220 
226  void setLimits(T const& limit_min = defaultLimitMin(),
227  T const& limit_max = defaultLimitMax()) {
228  setLimitMin(limit_min);
229  setLimitMax(limit_max);
230  }
231 
232  protected:
236  static T defaultLimitMin() { return std::numeric_limits<T>::lowest(); }
237 
241  static T defaultLimitMax() { return std::numeric_limits<T>::max(); }
242 
247 
252 
257 
261  AttributeChangeCallback callback_;
262 };
263 
264 #pragma mark -
265 #pragma mark === Class Attribute: vector specialization ===
266 
271 template <typename T>
272 class Attribute<std::vector<T>> : public AttributeBase {
273  public:
274  typedef std::function<void(AttributeBase*)> AttributeChangeCallback;
275 
276  template <typename U, typename args, class ListenerClass>
277  void onAttributeChange(U* owner,
278  void (ListenerClass::*listenerMethod)(args)) {
279  using namespace std::placeholders;
280  if (owner)
281  callback_ = std::bind(listenerMethod, owner, _1);
282  else
283  callback_ = nullptr;
284  }
285 
293  Attribute(std::vector<T> const& value = std::vector<T>(),
294  T const& limit_min = defaultLimitMin(),
295  T const& limit_max = defaultLimitMax(), unsigned int size = 0)
296  : limit_min_(limit_min), limit_max_(limit_max), size_(size) {
297  set(value, true);
298  changed = false;
299  }
300 
306  Attribute(Attribute const& src)
307  : value_(src.value_),
308  limit_min_(src.limit_min_),
309  limit_max_(src.limit_max_),
310  size_(src.size_) {
311  changed = false;
312  }
313 
320  template <typename U>
322  if (this != &src) {
323  value_ = src.value_;
324  limit_min_ = src.limit_min_;
325  limit_max_ = src.limit_max_;
326  size_ = src.size_;
327  changed = false;
328  }
329  return *this;
330  }
331 
342  void set(std::vector<T> const& value, bool silently = false) {
343  if (size_ > 0 && value.size() != size_) {
344  throw std::domain_error("Attribute value has the wrong size");
345  }
346  for (auto& val : value) {
347  checkLimits(val, limit_min_, limit_max_);
348  }
349  value_ = value;
350  changed = true;
351  if (!silently && callback_) callback_(this);
352  }
353 
358  std::vector<T> get() const { return value_; }
359 
365  T at(unsigned int index) const {
366  if (index < size_) {
367  return value_[index];
368  } else {
369  throw std::out_of_range("Index out of range");
370  }
371  }
372 
377  void setLimitMin(T const& limit_min = defaultLimitMin()) {
378  limit_min_ = limit_min;
379  }
380 
385  void setLimitMax(T const& limit_max = defaultLimitMax()) {
386  limit_max_ = limit_max;
387  }
388 
394  void setLimits(T const& limit_min = defaultLimitMin(),
395  T const& limit_max = defaultLimitMax()) {
396  setLimitMin(limit_min);
397  setLimitMax(limit_max);
398  }
399 
405  void resize(unsigned int size) {
406  value_.resize(size, T());
407  size_ = size;
408  }
409 
414  unsigned int size() const { return value_.size(); }
415 
416  protected:
420  static T defaultLimitMin() { return std::numeric_limits<T>::lowest(); }
421 
425  static T defaultLimitMax() { return std::numeric_limits<T>::max(); }
426 
430  std::vector<T> value_;
431 
436 
441 
445  unsigned int size_;
446 
450  AttributeChangeCallback callback_;
451 };
452 
453 #pragma mark -
454 #pragma mark === Class Attribute: vector<string> specialization ===
455 
459 template <>
460 class Attribute<std::vector<std::string>> : public AttributeBase {
461  public:
462  typedef std::function<void(AttributeBase*)> AttributeChangeCallback;
463 
464  template <typename U, typename args, class ListenerClass>
465  void onAttributeChange(U* owner,
466  void (ListenerClass::*listenerMethod)(args)) {
467  using namespace std::placeholders;
468  if (owner)
469  callback_ = std::bind(listenerMethod, owner, _1);
470  else
471  callback_ = nullptr;
472  }
473 
481  Attribute(std::vector<std::string> const& value = {},
482  std::string const& limit_min = "",
483  std::string const& limit_max = "", unsigned int size = 0)
484  : limit_min_(limit_min), limit_max_(limit_max), size_(size) {
485  set(value, true);
486  changed = false;
487  }
488 
494  Attribute(Attribute const& src)
495  : value_(src.value_),
496  limit_min_(src.limit_min_),
497  limit_max_(src.limit_max_),
498  size_(src.size_) {
499  changed = false;
500  }
501 
508  template <typename U>
510  if (this != &src) {
511  value_ = src.value_;
512  limit_min_ = src.limit_min_;
513  limit_max_ = src.limit_max_;
514  size_ = src.size_;
515  changed = false;
516  }
517  return *this;
518  }
519 
530  void set(std::vector<std::string> const& value, bool silently = false) {
531  if (size_ > 0 && value.size() != size_) {
532  throw std::domain_error("Attribute value has the wrong size");
533  }
534  for (auto& val : value) {
535  checkLimits(val, limit_min_, limit_max_);
536  }
537  value_ = value;
538  changed = true;
539  if (!silently && callback_) callback_(this);
540  }
541 
546  std::vector<std::string> get() const { return value_; }
547 
553  std::string at(unsigned int index) const {
554  if (index < size_) {
555  return value_[index];
556  } else {
557  throw std::out_of_range("Index out of range");
558  }
559  }
560 
565  void setLimitMin(std::string const& limit_min = defaultLimitMin()) {
566  limit_min_ = limit_min;
567  }
568 
573  void setLimitMax(std::string const& limit_max = defaultLimitMax()) {
574  limit_max_ = limit_max;
575  }
576 
582  void setLimits(std::string const& limit_min = defaultLimitMin(),
583  std::string const& limit_max = defaultLimitMax()) {
584  setLimitMin(limit_min);
585  setLimitMax(limit_max);
586  }
587 
593  void resize(unsigned int size) {
594  value_.resize(size, "");
595  size_ = size;
596  }
597 
602  unsigned int size() const {
603  return static_cast<unsigned int>(value_.size());
604  }
605 
606  protected:
610  static std::string defaultLimitMin() { return ""; }
611 
615  static std::string defaultLimitMax() { return ""; }
616 
620  std::vector<std::string> value_;
621 
625  std::string limit_min_;
626 
630  std::string limit_max_;
631 
635  unsigned int size_;
636 
640  AttributeChangeCallback callback_;
641 };
642 }
643 
644 #endif
void checkLimits< double >(double const &value, double const &limit_min, double const &limit_max)
Definition: xmmAttribute.cpp:97
Attribute(std::vector< std::string > const &value={}, std::string const &limit_min="", std::string const &limit_max="", unsigned int size=0)
Default Constructor.
Definition: xmmAttribute.hpp:481
void resize(unsigned int size)
set the attribute&#39;s size (vector Attribute). if 0, there is not size-checking on set.
Definition: xmmAttribute.hpp:593
Attribute & operator=(Attribute< U > const &src)
Assignment operator.
Definition: xmmAttribute.hpp:321
void checkLimits< unsigned int >(unsigned int const &value, unsigned int const &limit_min, unsigned int const &limit_max)
Definition: xmmAttribute.cpp:60
AttributeChangeCallback callback_
Callback function to be called on attribute change.
Definition: xmmAttribute.hpp:640
Attribute(Attribute const &src)
Copy Constructor.
Definition: xmmAttribute.hpp:494
void setLimits(T const &limit_min=defaultLimitMin(), T const &limit_max=defaultLimitMax())
set the attribute&#39;s limit values
Definition: xmmAttribute.hpp:394
AttributeChangeCallback callback_
Callback function to be called on attribute change.
Definition: xmmAttribute.hpp:450
AttributeChangeCallback callback_
Callback function to be called on attribute change.
Definition: xmmAttribute.hpp:261
void checkLimits< bool >(bool const &value, bool const &limit_min, bool const &limit_max)
Definition: xmmAttribute.cpp:37
static T defaultLimitMin()
Attribute default minimum value.
Definition: xmmAttribute.hpp:236
void setLimitMax(T const &limit_max=defaultLimitMax())
set the attribute&#39;s maximum value
Definition: xmmAttribute.hpp:217
void onAttributeChange(U *owner, void(ListenerClass::*listenerMethod)(args))
Definition: xmmAttribute.hpp:465
std::function< void(AttributeBase *)> AttributeChangeCallback
Definition: xmmAttribute.hpp:462
T limit_min_
Minimum value of the attribute.
Definition: xmmAttribute.hpp:251
void setLimits(std::string const &limit_min=defaultLimitMin(), std::string const &limit_max=defaultLimitMax())
set the attribute&#39;s limit values
Definition: xmmAttribute.hpp:582
T value_
Current value of the attribute.
Definition: xmmAttribute.hpp:246
unsigned int size() const
get the attribute&#39;s current size (vector Attribute)
Definition: xmmAttribute.hpp:414
static std::string defaultLimitMax()
Attribute default maximum value.
Definition: xmmAttribute.hpp:615
void setLimitMin(T const &limit_min=defaultLimitMin())
set the attribute&#39;s minimum value
Definition: xmmAttribute.hpp:209
void checkLimits(T const &value, T const &limit_min, T const &limit_max)
checks the validity of the requested value with respect to the current limits
Definition: xmmAttribute.hpp:55
T limit_max_
Maximum value of the attribute.
Definition: xmmAttribute.hpp:256
static T defaultLimitMin()
Attribute default minimum value.
Definition: xmmAttribute.hpp:420
Base Class for Generic Attributes.
Definition: xmmAttribute.hpp:105
void resize(unsigned int size)
set the attribute&#39;s size (vector Attribute). if 0, there is not size-checking on set.
Definition: xmmAttribute.hpp:405
Attribute & operator=(Attribute< U > const &src)
Assignment operator.
Definition: xmmAttribute.hpp:509
virtual ~AttributeBase()
Definition: xmmAttribute.hpp:107
unsigned int size_
Size of the vector of values.
Definition: xmmAttribute.hpp:445
std::vector< std::string > value_
Current value of the attribute.
Definition: xmmAttribute.hpp:620
Attribute(T const &value=T(), T const &limit_min=defaultLimitMin(), T const &limit_max=defaultLimitMax())
Default Constructor.
Definition: xmmAttribute.hpp:148
std::vector< T > value_
Current value of the attribute.
Definition: xmmAttribute.hpp:430
std::string limit_max_
Maximum value of the attribute.
Definition: xmmAttribute.hpp:630
void checkLimits< long >(long const &value, long const &limit_min, long const &limit_max)
Definition: xmmAttribute.cpp:79
Attribute(Attribute const &src)
Copy Constructor.
Definition: xmmAttribute.hpp:306
void setLimitMax(std::string const &limit_max=defaultLimitMax())
set the attribute&#39;s maximum value
Definition: xmmAttribute.hpp:573
static std::string defaultLimitMin()
Attribute default minimum value.
Definition: xmmAttribute.hpp:610
std::function< void(AttributeBase *)> AttributeChangeCallback
Definition: xmmAttribute.hpp:130
void checkLimits< int >(int const &value, int const &limit_min, int const &limit_max)
Definition: xmmAttribute.cpp:70
void setLimitMin(T const &limit_min=defaultLimitMin())
set the attribute&#39;s minimum value
Definition: xmmAttribute.hpp:377
void setLimits(T const &limit_min=defaultLimitMin(), T const &limit_max=defaultLimitMax())
set the attribute&#39;s limit values
Definition: xmmAttribute.hpp:226
void setLimitMax(T const &limit_max=defaultLimitMax())
set the attribute&#39;s maximum value
Definition: xmmAttribute.hpp:385
Attribute(std::vector< T > const &value=std::vector< T >(), T const &limit_min=defaultLimitMin(), T const &limit_max=defaultLimitMax(), unsigned int size=0)
Default Constructor.
Definition: xmmAttribute.hpp:293
void setLimitMin(std::string const &limit_min=defaultLimitMin())
set the attribute&#39;s minimum value
Definition: xmmAttribute.hpp:565
T at(unsigned int index) const
get the attribute&#39;s current value at a given index
Definition: xmmAttribute.hpp:365
void onAttributeChange(U *owner, void(ListenerClass::*listenerMethod)(args))
Definition: xmmAttribute.hpp:277
T limit_max_
Maximum value of the attribute.
Definition: xmmAttribute.hpp:440
T limit_min_
Minimum value of the attribute.
Definition: xmmAttribute.hpp:435
Attribute(Attribute const &src)
Copy Constructor.
Definition: xmmAttribute.hpp:160
Generic Attribute.
Definition: xmmAttribute.hpp:128
std::function< void(AttributeBase *)> AttributeChangeCallback
Definition: xmmAttribute.hpp:274
Definition: xmmAttribute.hpp:42
bool changed
Defines if the value has been changed.
Definition: xmmAttribute.hpp:117
void checkLimits< unsigned char >(unsigned char const &value, unsigned char const &limit_min, unsigned char const &limit_max)
Definition: xmmAttribute.cpp:41
static T defaultLimitMax()
Attribute default maximum value.
Definition: xmmAttribute.hpp:425
void checkLimits< float >(float const &value, float const &limit_min, float const &limit_max)
Definition: xmmAttribute.cpp:88
Attribute & operator=(Attribute< U > const &src)
Assignment operator.
Definition: xmmAttribute.hpp:174
void checkLimits< char >(char const &value, char const &limit_min, char const &limit_max)
Definition: xmmAttribute.cpp:51
void onAttributeChange(U *owner, void(ListenerClass::*listenerMethod)(args))
Definition: xmmAttribute.hpp:133
unsigned int size() const
get the attribute&#39;s current size (vector Attribute)
Definition: xmmAttribute.hpp:602
std::string limit_min_
Minimum value of the attribute.
Definition: xmmAttribute.hpp:625
unsigned int size_
Size of the vector of values.
Definition: xmmAttribute.hpp:635
AttributeBase()
Default Constructor.
Definition: xmmAttribute.hpp:112
static T defaultLimitMax()
Attribute default maximum value.
Definition: xmmAttribute.hpp:241
std::string at(unsigned int index) const
get the attribute&#39;s current value at a given index
Definition: xmmAttribute.hpp:553