Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
maNumeric.h
1 #pragma once
2 
4 #include "AttributeModifier.h"
5 
6 
7 
8 // ****************************************
9 // * maNumericHeritage class
10 // * --------------------------------------
16 // ****************************************
17 template<s32 notificationLevel, typename T, CoreModifiable::ATTRIBUTE_TYPE attributeType> class maNumericHeritage : public CoreModifiableAttributeData<T>
18 {
19  template<s32 notiflevel>
21 
22  DECLARE_ATTRIBUTE_HERITAGE(maNumericHeritage, TemplateForPlacementNew, T, attributeType);
23 
24 public:
25 
26  // Shouldn't they return *this instead ?
27  T operator+=(T value)
28  {
29  mValue += value;
30  return operator T();
31  }
32  T operator-=(T value)
33  {
34  mValue -= value;
35  return operator T();
36  }
37  T operator*=(T value)
38  {
39  mValue *= value;
40  return operator T();
41  }
42  T operator/=(T value)
43  {
44  mValue /= value;
45  return operator T();
46  }
47  T operator|=(T value)
48  {
49  mValue |= value;
50  return operator T();
51  }
52  T operator^=(T value)
53  {
54  mValue ^= value;
55  return operator T();
56  }
57  T operator&=(T value)
58  {
59  mValue &= value;
60  return operator T();
61  }
62 
63 
64  //@REFACTOR: use expand macro
65 
66  virtual bool setValue(bool value) override { if (this->isReadOnly()) { return false; } T tmpValue = value ? std::numeric_limits<T>::max() : 0; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
67  virtual bool setValue(s8 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
68  virtual bool setValue(s16 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
69  virtual bool setValue(s32 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
70  virtual bool setValue(s64 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
71  virtual bool setValue(u8 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
72  virtual bool setValue(u16 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
73  virtual bool setValue(u32 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
74  virtual bool setValue(u64 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
75  virtual bool setValue(kfloat value) override
76  {
77  if (this->isReadOnly())
78  {
79  return false;
80  }
81  T tmpValue = (T)value;
82  CALL_SETMODIFIER(notificationLevel, tmpValue);
83  this->mValue = tmpValue;
84  DO_NOTIFICATION(notificationLevel);
85  return true;
86  }
87  virtual bool setValue(kdouble value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue; DO_NOTIFICATION(notificationLevel); return true; }
88  virtual bool setValue(const char* value) override { kstl::string localstr(value); return setValue(localstr); }
89  virtual bool setValue(const usString& value) override { kstl::string localstr(value); return setValue(localstr); }
90  virtual bool setValue(const kstl::string& value) override
91  {
92  if (this->isReadOnly()) { return false; }
93 
94  if (CoreConvertString2Value<T>(value, this->mValue))
95  {
96  T tmpValue = this->mValue; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mValue = tmpValue;
97  DO_NOTIFICATION(notificationLevel); return true;
98  }
99  return false;
100  }
101 
102  virtual bool getValue(bool& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (tmpValue != (T)0); return true; }
103  virtual bool getValue(s8& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s8)tmpValue; return true; }
104  virtual bool getValue(s16& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s16)tmpValue; return true; }
105  virtual bool getValue(s32& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s32)tmpValue; return true; }
106  virtual bool getValue(s64& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s64)tmpValue; return true; }
107  virtual bool getValue(u8& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u8)tmpValue; return true; }
108  virtual bool getValue(u16& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u16)tmpValue; return true; }
109  virtual bool getValue(u32& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u32)tmpValue; return true; }
110  virtual bool getValue(u64& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u64)tmpValue; return true; }
111  virtual bool getValue(kfloat& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (kfloat)tmpValue; return true; }
112  virtual bool getValue(kdouble& value) const override { T tmpValue = this->mValue; CALL_GETMODIFIER(notificationLevel, tmpValue); value = (kdouble)tmpValue; return true; }
113  virtual bool getValue(kstl::string& value) const override
114  {
115  T tmpValue = this->mValue;
116  CALL_GETMODIFIER(notificationLevel, tmpValue);
117  return CoreConvertValue2String<T>(value, tmpValue);
118  }
119  virtual bool getValue(usString& value) const override
120  {
121  kstl::string tmp;
122  T tmpValue = this->mValue;
123  CALL_GETMODIFIER(notificationLevel, tmpValue);
124  if (CoreConvertValue2String<T>(tmp, tmpValue))
125  {
126  value = tmp; return true;
127  }
128  return false;
129  }
130 
131 };
132 
133 template<typename T, CoreModifiable::ATTRIBUTE_TYPE attributeType = TypeToEnum<T>::value>
135 
136 
137 using maChar = maNumeric<s8>;
138 using maShort = maNumeric<s16>;
139 using maInt = maNumeric<s32>;
140 using maLong = maNumeric<s64>;
141 using maUChar = maNumeric<u8>;
142 using maUShort = maNumeric<u16>;
143 using maUInt = maNumeric<u32>;
144 using maULong = maNumeric<u64>;
145 using maFloat = maNumeric<float>;
147 
148 // ****************************************
149 // * maComputedNumericHeritage class
150 // * --------------------------------------
156 // ****************************************
157 template<s32 notificationLevel, typename T, CoreModifiable::ATTRIBUTE_TYPE attributeType> class maComputedNumericHeritage : public CoreModifiableAttribute
158 {
159  template<s32 notiflevel>
161 
162 
163 
164 private:
165 
166  maComputedNumericHeritage(CoreModifiable& owner, bool isInitParam, KigsID ID, const T& value) : CoreModifiableAttribute(&owner, isInitParam, ID)
167  {
168 
169  }
171  {
172 
173  }
174  maComputedNumericHeritage(KigsID ID, const T& value) : CoreModifiableAttribute(nullptr, false, ID)
175  {
176  }
177 
178 public:
179 
180  explicit maComputedNumericHeritage(InheritanceSwitch tag) : CoreModifiableAttribute(tag) {}
181 
182  maComputedNumericHeritage(CoreModifiable& owner, bool isInitParam, KigsID ID, KigsID g, KigsID s) : CoreModifiableAttribute(&owner, isInitParam, ID), mGetter(g), mSetter(s)
183  {
184 
185  }
186 
188  typedef T CurrentAttributeType;
189  static constexpr CoreModifiable::ATTRIBUTE_TYPE type = attributeType;
190  CoreModifiable::ATTRIBUTE_TYPE getType() const override { return attributeType; }
191 
192  auto& operator=(const TemplateForPlacementNew<notificationLevel>& attribute)
193  {
194  this->CopyData(attribute);
195  return *this;
196  }
197 protected:
198 
199  KigsID mGetter;
200  KigsID mSetter;
201 
202  void doPlacementNew(u32 level) override
203  {
204  switch (level)
205  {
206  case 0: new (this) TemplateForPlacementNew<0>(InheritanceSwitch{}); break; \
207  case 1: new (this) TemplateForPlacementNew<1>(InheritanceSwitch{}); break; \
208  case 2: new (this) TemplateForPlacementNew<2>(InheritanceSwitch{}); break; \
209  case 3: new (this) TemplateForPlacementNew<3>(InheritanceSwitch{}); break; \
210  default: assert(false); break;
211  }
212  }
213  virtual void changeInheritance() final
214  {
215  KigsID old_id = mID;
216  mID.~KigsID();
217  KigsID old_getter = mGetter;
218  mGetter.~KigsID();
219  KigsID old_setter = mSetter;
220  mSetter.~KigsID();
221  AttachedModifierBase* modifier = mAttachedModifier;
222  u32 old_flags = mFlags;
223  u32 inheritlevel = (mFlags >> INHERIT_LEVEL_SHIFT)& INHERIT_LEVEL_MOD;
224  doPlacementNew(inheritlevel);
225  mID = old_id;
226  mGetter = old_getter;
227  mSetter = old_setter;
228 
229  mFlags = old_flags;
230  mAttachedModifier = modifier;
231  }
232 
233 public:
234  virtual operator CurrentAttributeType() const
235  {
236  CurrentAttributeType tmpValue = mOwner->template SimpleCall<T>(mGetter);
237 
238  CALL_GETMODIFIER(notificationLevel, tmpValue);
239  return tmpValue;
240  };
241  auto& operator=(const CurrentAttributeType& value)
242  {
243  if (mSetter != "") mOwner->SimpleCall(mSetter,value);
244  return *this;
245  }
246 
247 public:
248 
249  virtual void* getRawValue() final { return nullptr; }
250  bool CopyAttribute(const CoreModifiableAttribute& other) override
251  {
252 
253  return false;
254 
255  }
256 
257 
258  virtual bool setValue(bool value) override { if (this->isReadOnly()) { return false; } T tmpValue = value ? std::numeric_limits<T>::max() : 0; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
259  virtual bool setValue(s8 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
260  virtual bool setValue(s16 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
261  virtual bool setValue(s32 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
262  virtual bool setValue(s64 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
263  virtual bool setValue(u8 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
264  virtual bool setValue(u16 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
265  virtual bool setValue(u32 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
266  virtual bool setValue(u64 value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
267  virtual bool setValue(kfloat value) override
268  {
269  if (this->isReadOnly())
270  {
271  return false;
272  }
273  T tmpValue = (T)value;
274  CALL_SETMODIFIER(notificationLevel, tmpValue);
275  if (mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue);
276  DO_NOTIFICATION(notificationLevel);
277  return true;
278  }
279  virtual bool setValue(kdouble value) override { if (this->isReadOnly()) { return false; } T tmpValue = (T)value; CALL_SETMODIFIER(notificationLevel, tmpValue); this->mOwner->SimpleCall(mSetter,tmpValue); DO_NOTIFICATION(notificationLevel); return true; }
280  virtual bool setValue(const char* value) override { kstl::string localstr(value); return setValue(localstr); }
281  virtual bool setValue(const usString& value) override { kstl::string localstr(value); return setValue(localstr); }
282  virtual bool setValue(const kstl::string& value) override
283  {
284  if (this->isReadOnly()) { return false; }
285 
286  T tmpValue;
287  if (CoreConvertString2Value<T>(value, tmpValue))
288  {
289  CALL_SETMODIFIER(notificationLevel, tmpValue); if(mSetter != "") this->mOwner->SimpleCall(mSetter,tmpValue);
290  DO_NOTIFICATION(notificationLevel); return true;
291  }
292  return false;
293  }
294 
295  virtual bool getValue(bool& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (tmpValue != (T)0); return true; }
296  virtual bool getValue(s8& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s8)tmpValue; return true; }
297  virtual bool getValue(s16& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s16)tmpValue; return true; }
298  virtual bool getValue(s32& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s32)tmpValue; return true; }
299  virtual bool getValue(s64& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (s64)tmpValue; return true; }
300  virtual bool getValue(u8& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u8)tmpValue; return true; }
301  virtual bool getValue(u16& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u16)tmpValue; return true; }
302  virtual bool getValue(u32& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u32)tmpValue; return true; }
303  virtual bool getValue(u64& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (u64)tmpValue; return true; }
304  virtual bool getValue(kfloat& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (kfloat)tmpValue; return true; }
305  virtual bool getValue(kdouble& value) const override { T tmpValue = this->mOwner->template SimpleCall<T>(mGetter); CALL_GETMODIFIER(notificationLevel, tmpValue); value = (kdouble)tmpValue; return true; }
306  virtual bool getValue(kstl::string& value) const override
307  {
308  T tmpValue = this->mOwner->template SimpleCall<T>(mGetter);
309  CALL_GETMODIFIER(notificationLevel, tmpValue);
310  return CoreConvertValue2String<T>(value, tmpValue);
311  }
312  virtual bool getValue(usString& value) const override
313  {
314  kstl::string tmp;
315  T tmpValue = this->mOwner->template SimpleCall<T>(mGetter);
316  CALL_GETMODIFIER(notificationLevel, tmpValue);
317  if (CoreConvertValue2String<T>(tmp, tmpValue))
318  {
319  value = tmp; return true;
320  }
321  return false;
322  }
323 
324 };
325 
326 template<typename T, CoreModifiable::ATTRIBUTE_TYPE attributeType = TypeToEnum<T>::value>
CoreModifiableAttribute::isReadOnly
virtual bool isReadOnly()
Read only attributes cannot be modified with setValue.
Definition: CoreModifiableAttribute.h:276
maNumericHeritage
CoreModifiableAttributeData for numeric without different level of notification.
Definition: maNumeric.h:17
CoreModifiableAttribute::isInitParam
virtual bool isInitParam()
return true if attribute is an init attribute (necessary for the CoreModifiable Init to be done)
Definition: CoreModifiableAttribute.h:278
CoreModifiable
Base class for Kigs framework objects. CoreModifiable class manage a list of attributes supporting re...
Definition: CoreModifiable.h:480
maComputedNumericHeritage
numeric calling onwer get / set method to compute value
Definition: maNumeric.h:157
CoreModifiableAttribute.h
Base template class for CoreModifiable attributes managing data.
CoreModifiableAttribute
Base class for all CoreModifiableAttribute . This class is just composed of virtual methods.
Definition: CoreModifiableAttribute.h:145