Wavelet - A library for online estimation of the Continuous Wavelet Transform  0.1-alpha
attribute.hpp
1 /*
2  * attribute.h
3  *
4  * Generic Attributes
5  *
6  * Contact:
7  * - Jules Françoise <jules.francoise@ircam.fr>
8  *
9  * This code has been authored by <a href="http://julesfrancoise.com">Jules Françoise</a>
10  * in the framework of the <a href="http://skatvg.iuav.it/">SkAT-VG</a> European project,
11  * with <a href="frederic-bevilacqua.net">Frederic Bevilacqua</a>, in the
12  * <a href="http://ismm.ircam.fr">Sound Music Movement Interaction</a> team of the
13  * <a href="http://www.ircam.fr/stms.html?&L=1">STMS Lab</a> - IRCAM - CNRS - UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 Ircam-Centre Pompidou.
16  *
17  * This File is part of Wavelet.
18  *
19  * Wavelet 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  * Wavelet 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 Wavelet. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef __xmm_lib__attribute__
34 #define __xmm_lib__attribute__
35 
36 #include <exception>
37 #include <stdexcept>
38 #include <limits>
39 #include <vector>
40 #include <boost/any.hpp>
41 
42 namespace wavelet {
43 
44 #pragma mark -
45 #pragma mark === Functions checkLimits ===
46 
54  template <typename T>
55  void checkLimits(T const& value,
56  T const& limit_min,
57  T const& limit_max)
58  {
59  throw std::runtime_error("Attribute limits are not implemented for the current type.");
60  }
61 
62  template <>
63  void checkLimits<std::size_t>(std::size_t const& value,
64  std::size_t const& limit_min,
65  std::size_t const& limit_max);
66 
67  template <>
68  void checkLimits<unsigned char>(unsigned char const& value,
69  unsigned char const& limit_min,
70  unsigned char const& limit_max);
71 
72  template <>
73  void checkLimits<char>(char const& value,
74  char const& limit_min,
75  char const& limit_max);
76 
77  template <>
78  void checkLimits<unsigned int>(unsigned int const& value,
79  unsigned int const& limit_min,
80  unsigned int const& limit_max);
81 
82  template <>
83  void checkLimits<int>(int const& value,
84  int const& limit_min,
85  int const& limit_max);
86 
87  template <>
88  void checkLimits<long>(long const& value,
89  long const& limit_min,
90  long const& limit_max);
91 
92  template <>
93  void checkLimits<float>(float const& value,
94  float const& limit_min,
95  float const& limit_max);
96 
97  template <>
98  void checkLimits<double>(double const& value,
99  double const& limit_min,
100  double const& limit_max);
101 
102 #pragma mark -
103 #pragma mark === Class AttributeBase ===
104 
107  class AttributeBase {
108  public:
112  AttributeBase() : changed(false)
113  {}
114 
118  bool changed;
119  };
120 
121 #pragma mark -
122 #pragma mark === Class AttributeHandler ===
123 
126  class AttributeHandler {
127  template <typename T> friend class Attribute;
128 
129  public:
133  virtual ~AttributeHandler() {}
134 
135  protected:
139  virtual void onAttributeChange(AttributeBase* attr_pointer) = 0;
140  };
142 
143 #pragma mark -
144 #pragma mark === Class Attribute ===
145 
149  template <typename T>
150  class Attribute : public AttributeBase {
151  public:
159  Attribute(AttributeHandler *parent=nullptr,
160  T const& value=T(),
161  T const& limit_min=default_limit_min(),
162  T const& limit_max=default_limit_max()) :
163  limit_min_(limit_min),
164  limit_max_(limit_max),
165  parent_(parent)
166  {
167  set(value, true);
168  changed = false;
169  }
170 
176  Attribute(Attribute const& src) :
177  value_(src.value_),
178  limit_min_(src.limit_min_),
179  limit_max_(src.limit_max_),
180  parent_(src.parent_)
181  {
182  changed = false;
183  }
184 
191  template<typename U>
193  {
194  if (this != &src) {
195  value_ = src.value_;
196  limit_min_ = src.limit_min_;
197  limit_max_ = src.limit_max_;
198  parent_ = src.parent_;
199  changed = false;
200  }
201  return *this;
202  }
203 
207  virtual ~Attribute()
208  {}
209 
217  void set(T const& value, bool silently = false)
218  {
219  checkLimits(value, limit_min_, limit_max_);
220  value_ = value;
221  changed = true;
222  if (!silently && parent_)
223  parent_->onAttributeChange(this);
224  }
225 
230  T get() const
231  {
232  return value_;
233  }
234 
239  T getCopy() const
240  {
241  return T(value_);
242  }
243 
248  void set_limit_min(T const& limit_min=default_limit_min())
249  {
250  limit_min_ = limit_min;
251  }
252 
257  void set_limit_max(T const& limit_max=default_limit_max())
258  {
259  limit_max_ = limit_max;
260  }
261 
267  void set_limits(T const& limit_min=default_limit_min(),
268  T const& limit_max=default_limit_max())
269  {
270  set_limit_min(limit_min);
271  set_limit_max(limit_max);
272  }
273 
278  void set_parent(AttributeHandler* parent)
279  {
280  parent_ = parent;
281  }
282 
287  AttributeHandler* get_parent() const
288  {
289  return parent_;
290  }
291 
292  protected:
294 
298  static T default_limit_min() { return std::numeric_limits<T>::lowest(); }
299 
303  static T default_limit_max() { return std::numeric_limits<T>::max(); }
304 
308  T limit_min_;
309 
313  T limit_max_;
314 
318  T value_;
319 
323  AttributeHandler *parent_;
324 
326  };
327 }
328 
329 #endif
virtual ~Attribute()
Destructor.
Definition: attribute.hpp:207
AttributeHandler * get_parent() const
get the parent to receive change notifications
Definition: attribute.hpp:287
void set_parent(AttributeHandler *parent)
set the parent to receive change notifications
Definition: attribute.hpp:278
T getCopy() const
get a copy of the attribute's current value
Definition: attribute.hpp:239
Attribute & operator=(Attribute< U > const &src)
Assignment operator.
Definition: attribute.hpp:192
Definition: attribute.hpp:42
void set_limit_max(T const &limit_max=default_limit_max())
set the attribute's maximum value
Definition: attribute.hpp:257
Attribute(AttributeHandler *parent=nullptr, T const &value=T(), T const &limit_min=default_limit_min(), T const &limit_max=default_limit_max())
Default Constructor.
Definition: attribute.hpp:159
void set_limit_min(T const &limit_min=default_limit_min())
set the attribute's minimum value
Definition: attribute.hpp:248
Attribute(Attribute const &src)
Copy Constructor.
Definition: attribute.hpp:176
void set(T const &value, bool silently=false)
Set the attribute value.
Definition: attribute.hpp:217
Generic Attribute.
Definition: attribute.hpp:150
void set_limits(T const &limit_min=default_limit_min(), T const &limit_max=default_limit_max())
set the attribute's limit values
Definition: attribute.hpp:267