Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
maEnum.h
1 #pragma once
2 
4 #include "AttributeModifier.h"
5 #include <array>
6 
7 // ****************************************
8 // * maEnumBase class
9 // * --------------------------------------
15 // ****************************************
16 
17 template<unsigned int N>
18 struct maEnumValue
19 {
20  int current_value = 0;
21  std::array<kstl::string, N> value_list;
22 };
23 
24 
25 // ****************************************
26 // * maEnumHeritage class
27 // * --------------------------------------
33 // ****************************************
34 template<int notificationLevel, unsigned int nbElements>
35 class maEnumHeritage : public CoreModifiableAttributeData<maEnumValue<nbElements>>
36 {
37  template<int notiflevel>
39 
40  DECLARE_ATTRIBUTE_HERITAGE_NO_ASSIGN(maEnumHeritage, TemplateForPlacementNew, maEnumValue<nbElements>, CoreModifiable::ATTRIBUTE_TYPE::ENUM);
41 
42 public:
43 
44 
45 
46  maEnumHeritage(CoreModifiable& owner, bool isInitAttribute, KigsID ID, kstl::string val0, kstl::string val1, kstl::string val2 = "", kstl::string val3 = "", kstl::string val4 = "", kstl::string val5 = "", kstl::string val6 = "", kstl::string val7 = "", kstl::string val8 = "", kstl::string val9 = "")
47  : CoreModifiableAttributeData<maEnumValue<nbElements>>(owner, isInitAttribute, ID)
48  {
49  //@TODO variadic template constructor?
50  // Copy and move idiom
51  if (nbElements>0) mValue.value_list[0] = std::move(val0);
52  if (nbElements>1) mValue.value_list[1] = std::move(val1);
53  if (nbElements>2) mValue.value_list[2] = std::move(val2);
54  if (nbElements>3) mValue.value_list[3] = std::move(val3);
55  if (nbElements>4) mValue.value_list[4] = std::move(val4);
56  if (nbElements>5) mValue.value_list[5] = std::move(val5);
57  if (nbElements>6) mValue.value_list[6] = std::move(val6);
58  if (nbElements>7) mValue.value_list[7] = std::move(val7);
59  if (nbElements>8) mValue.value_list[8] = std::move(val8);
60  if (nbElements>9) mValue.value_list[9] = std::move(val9);
61  mValue.current_value = 0;
62  }
63 
64  kstl::vector<kstl::string> getEnumElements() const override { return {mValue.value_list.data(), mValue.value_list.data() + nbElements }; }
65 
66  virtual bool CopyAttribute(const CoreModifiableAttribute& attribute) override
67  {
68  if (CoreModifiableAttributeData<maEnumValue<nbElements>>::CopyAttribute(attribute)) return true;
69  int val;
70  if (attribute.getValue(val))
71  {
72  if (val >= 0 && val < nbElements)
73  {
74  mValue.current_value = val;
75  return true;
76  }
77  }
78  return false;
79  }
80 
81 
82  kstl::string& operator[](unsigned int index) { KIGS_ASSERT(index < nbElements); return mValue.value_list[index]; }
83  const kstl::string& operator[](unsigned int index) const { KIGS_ASSERT(index < nbElements); return mValue.value_list[index]; }
84 
85 
86 
87  operator int() { int val = 0; getValue(val); return val; }
88  operator const kstl::string&() const
89  {
90  int val; getValue(val);
91  return mValue.value_list[val];
92  }
93 
94 
95 #define IMPLEMENT_SET_VALUE_ENUM(type)\
96  virtual bool setValue(type value) override { if (this->isReadOnly()) { return false; } unsigned int tmpValue = (unsigned int)value; CALL_SETMODIFIER(notificationLevel, tmpValue); if (tmpValue < nbElements) { mValue.current_value = tmpValue; DO_NOTIFICATION(notificationLevel); return true; } return false; }
97 
98  EXPAND_MACRO_FOR_NUMERIC_TYPES(NOQUALIFIER, NOQUALIFIER, IMPLEMENT_SET_VALUE_ENUM);
99 
100 #define IMPLEMENT_GET_VALUE_ENUM(type)\
101  virtual bool getValue(type value) const override { unsigned int tmpValue=mValue.current_value; CALL_GETMODIFIER(notificationLevel, tmpValue); if(tmpValue<nbElements){ value = (type)tmpValue; return true;} return false; }
102 
103  EXPAND_MACRO_FOR_NUMERIC_TYPES(NOQUALIFIER, &, IMPLEMENT_GET_VALUE_ENUM);
104 
105 
106 #undef IMPLEMENT_SET_VALUE_ENUM
107 #undef IMPLEMENT_GET_VALUE_ENUM
108 
109  virtual bool setValue(const kstl::string& value) override
110  {
111  if (this->isReadOnly()) { return false; }
112  unsigned int i;
113  for (i = 0; i<nbElements; i++)
114  {
115  if (mValue.value_list[i] == value)
116  {
117  CALL_SETMODIFIER(notificationLevel, i);
118  if (i < nbElements)
119  {
120  mValue.current_value = i;
121  DO_NOTIFICATION(notificationLevel);
122  return true;
123  }
124  else return false;
125  }
126  }
127  return false;
128  }
129 
130  virtual bool getValue(kstl::string& value) const override
131  {
132  unsigned int tmpValue = mValue.current_value;
133  CALL_GETMODIFIER(notificationLevel, tmpValue);
134  if (tmpValue < nbElements)
135  {
136  value = mValue.value_list[tmpValue];
137  }
138  return false;
139  }
140 
141  virtual bool setValue(const char* value) override
142  {
143  kstl::string localstr(value);
144  return setValue(localstr);
145  }
146 
147 };
148 
149 
150 
151 template<unsigned int nbElements>
153 
CoreModifiableAttribute::isReadOnly
virtual bool isReadOnly()
Read only attributes cannot be modified with setValue.
Definition: CoreModifiableAttribute.h:276
maEnumHeritage
CoreModifiableAttributeData for an enum with different level of notification.
Definition: maEnum.h:35
CoreModifiable
Base class for Kigs framework objects. CoreModifiable class manage a list of attributes supporting re...
Definition: CoreModifiable.h:480
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