Lumiverse  2.5
A framework for creating lighting control applications
LumiverseFloat.h
Go to the documentation of this file.
1 
4 #ifndef _LumiverseFLOAT_H_
5 #define _LumiverseFLOAT_H_
6 #pragma once
7 
8 #include "../LumiverseType.h"
9 #include <string>
10 #include <stdio.h>
11 
12 namespace Lumiverse {
22  {
23  public:
33  LumiverseFloat(float val = 0.0f, float def = 0.0f, float max = 1.0f, float min = 0.0f);
34 
40 
48 
53 
58  virtual string getTypeName() { return "float"; }
59 
60  // Override for =
61  void operator=(float val);
62  void operator=(LumiverseFloat val);
63 
64  // Arithmetic overrides
65  LumiverseFloat& operator+=(float val);
66  LumiverseFloat& operator+=(LumiverseFloat& val);
67 
68  LumiverseFloat& operator-=(float val);
69  LumiverseFloat& operator-=(LumiverseFloat& val);
70 
71  LumiverseFloat& operator*=(float val);
72  LumiverseFloat& operator*=(LumiverseFloat& val);
73 
74  LumiverseFloat& operator/=(float val);
75  LumiverseFloat& operator/=(LumiverseFloat& val);
76 
80  float getVal() { return m_val; }
81 
86  void setVal(float val) { m_val = val; clamp(); }
87 
91  void setVals(float val, float def, float min, float max);
92 
97  void setMax(float val) { m_max = val; }
98 
103  float getMax() { return m_max; }
104 
109  void setMin(float val) { m_min = val; }
110 
115  float getMin() { return m_min; }
116 
121  void setDefault(float val) { m_default = val; }
122 
127  float getDefault() { return m_default; }
128 
132  virtual void reset();
133 
137  void setValAsPercent(float val);
138 
143  float asPercent();
144 
145  // Converts a float to a JSON object with specified name.
146  virtual JSONNode toJSON(string name);
147 
153  virtual string asString();
154 
155  virtual bool isDefault();
156 
157  private:
161  void clamp();
162 
166  float m_val;
167 
169  float m_default;
170 
172  float m_max;
173 
175  float m_min;
176  };
177 
178  // Ops ops ops all overloaded woo
179 
180  // Compares two LumiverseFloats. Uses normal float comparison
181  inline bool operator==(LumiverseFloat& a, LumiverseFloat& b) {
182  if (a.getTypeName() != "float" || b.getTypeName() != "float")
183  return false;
184 
185  return a.getVal() == b.getVal();
186  }
187 
188  inline bool operator==(LumiverseFloat& a, float b) {
189  if (a.getTypeName() != "float")
190  return false;
191 
192  return a.getVal() == b;
193  }
194 
195  inline bool operator!=(LumiverseFloat& a, LumiverseFloat& b) {
196  return !(a == b);
197  }
198 
199  inline bool operator!=(LumiverseFloat& a, float b) {
200  return !(a == b);
201  }
202 
203  // LumiverseFloat uses the normal < op for floats.
204  inline bool operator<(LumiverseFloat& a, LumiverseFloat& b) {
205  if (a.getTypeName() != "float" || b.getTypeName() != "float")
206  return false;
207 
208  return a.getVal() < b.getVal();
209  }
210 
211  inline bool operator<(LumiverseFloat& a, float b) {
212  if (a.getTypeName() != "float")
213  return false;
214 
215  return a.getVal() < b;
216  }
217 
218  inline bool operator<(float a, LumiverseFloat& b) {
219  if (b.getTypeName() != "float")
220  return false;
221 
222  return a < b.getVal();
223  }
224 
225  inline bool operator>(LumiverseFloat& a, LumiverseFloat& b) {
226  return b < a;
227  }
228 
229  inline bool operator>(LumiverseFloat& a, float b) {
230  return b < a;
231  }
232 
233  inline bool operator<=(LumiverseFloat& a, LumiverseFloat& b) {
234  return !(a > b);
235  }
236 
237  inline bool operator<=(LumiverseFloat& a, float b) {
238  return !(a > b);
239  }
240 
241  inline bool operator>=(LumiverseFloat& a, LumiverseFloat b) {
242  return !(a < b);
243  }
244 
245  inline bool operator>=(LumiverseFloat& a, float b) {
246  return !(a < b);
247  }
248 
249  // Arithmetic overrides
250  inline LumiverseFloat operator+(LumiverseFloat& lhs, float rhs) {
251  LumiverseFloat val = LumiverseFloat(lhs);
252  val += rhs;
253  return val;
254  }
255 
256  //inline LumiverseFloat operator+(LumiverseFloat& lhs, LumiverseFloat& rhs) {
257  // LumiverseFloat val = LumiverseFloat(lhs);
258  // val += rhs;
259  // return val;
260  //}
261 
262  // Apparently clang needs this form of the overload to make it happy
263  inline LumiverseFloat operator+(LumiverseFloat lhs, LumiverseFloat rhs) {
264  LumiverseFloat val = LumiverseFloat(lhs);
265  val += rhs;
266  return val;
267  }
268 
269  inline LumiverseFloat operator-(LumiverseFloat& lhs, float rhs) {
270  LumiverseFloat val = LumiverseFloat(lhs);
271  val -= rhs;
272  return val;
273  }
274 
275  inline LumiverseFloat operator-(LumiverseFloat& lhs, LumiverseFloat& rhs) {
276  LumiverseFloat val = LumiverseFloat(lhs);
277  val -= rhs;
278  return val;
279  }
280 
281  inline LumiverseFloat operator*(LumiverseFloat& lhs, float rhs) {
282  LumiverseFloat val = LumiverseFloat(lhs);
283  val *= rhs;
284  return val;
285  }
286 
287  inline LumiverseFloat operator*(LumiverseFloat& lhs, LumiverseFloat& rhs) {
288  LumiverseFloat val = LumiverseFloat(lhs);
289  val *= rhs;
290  return val;
291  }
292 
293  inline LumiverseFloat operator/(LumiverseFloat& lhs, float rhs) {
294  LumiverseFloat val = LumiverseFloat(lhs);
295  val /= rhs;
296  return val;
297  }
298 
299  inline LumiverseFloat operator/(LumiverseFloat& lhs, LumiverseFloat& rhs) {
300  LumiverseFloat val = LumiverseFloat(lhs);
301  val /= rhs;
302  return val;
303  }
304 }
305 
306 #endif
void setDefault(float val)
Set the default value for the float.
Definition: LumiverseFloat.h:121
void setVal(float val)
Sets the value of the float.
Definition: LumiverseFloat.h:86
void setValAsPercent(float val)
Sets the value of the LumiverseFloat proportionally.
Definition: LumiverseFloat.cpp:74
float getMax()
Get the maximum value.
Definition: LumiverseFloat.h:103
This class is a wapper around a variety of different possible data types that might be needed by a De...
Definition: LumiverseType.h:33
float m_val
the value of this object
Definition: LumiverseFloat.h:166
float m_max
Maximum value for the float (default 1.0)
Definition: LumiverseFloat.h:172
void setMax(float val)
Set maximum value.
Definition: LumiverseFloat.h:97
void setVals(float val, float def, float min, float max)
Sets values for all float params.
Definition: LumiverseFloat.cpp:110
void setMin(float val)
Set miniumum value.
Definition: LumiverseFloat.h:109
~LumiverseFloat()
Destroys the float.
Definition: LumiverseFloat.cpp:28
virtual void reset()
Resets the value to the default value.
Definition: LumiverseFloat.cpp:68
virtual bool isDefault()
Returns true if the value is equal to the default value for the type.
Definition: LumiverseFloat.cpp:53
float getVal()
Gets the value of the float.
Definition: LumiverseFloat.h:80
float m_default
Default value for this float.
Definition: LumiverseFloat.h:169
float getMin()
Get the minimum value.
Definition: LumiverseFloat.h:115
float getDefault()
Gets the default value for the float.
Definition: LumiverseFloat.h:127
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
virtual JSONNode toJSON(string name)
Converts the type to a JSON object with the specified name.
Definition: LumiverseFloat.cpp:30
float m_min
Definition: LumiverseFloat.h:175
float asPercent()
Returns the value of this float as a percentage.
Definition: LumiverseFloat.cpp:80
Defines a float in Lumiverse.
Definition: LumiverseFloat.h:21
virtual string asString()
Returns the value of the LumiverseFloat as a string.
Definition: LumiverseFloat.cpp:43
virtual string getTypeName()
Says that this object is a float.
Definition: LumiverseFloat.h:58
LumiverseFloat(float val=0.0f, float def=0.0f, float max=1.0f, float min=0.0f)
Constructs a float, default value is 0.
Definition: LumiverseFloat.cpp:5
void clamp()
Ensures that the value of this float is between min and max.
Definition: LumiverseFloat.cpp:57