Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
maReference.h
1 #ifndef _MAREFERENCE_H
2 #define _MAREFERENCE_H
3 
5 #include "AttributeModifier.h"
6 
7 
8 
9 struct maReferenceObject
10 {
11  maReferenceObject() = default;
12 
13  maReferenceObject(CoreModifiable* lobj)
14  {
15  mObj = lobj;
16  }
17  maReferenceObject(const kstl::string& nametype) : mObj(nullptr)
18  {
19  mSearchString = nametype;
20  }
21 
22  CoreModifiable* mObj;
23  kstl::string mSearchString;
24 };
25 
26 // ****************************************
27 // * maReferenceHeritage class
28 // * --------------------------------------
34 // ****************************************
35 
36 template<int notificationLevel>
37 class maReferenceHeritage : public CoreModifiableAttributeData<maReferenceObject>
38 {
39  DECLARE_ATTRIBUTE_HERITAGE_NO_ASSIGN(maReferenceHeritage, maReferenceHeritage, maReferenceObject, CoreModifiable::ATTRIBUTE_TYPE::REFERENCE);
40 
41 
42 public:
43 
44  maReferenceHeritage(CoreModifiable& owner, bool isInitAttribute, KigsID ID, kstl::string value) : CoreModifiableAttributeData<maReferenceObject>(owner, isInitAttribute, ID)
45  {
46  mValue = maReferenceObject{ value };
47  Search();
48  }
49 
50  maReferenceHeritage() : CoreModifiableAttributeData<maReferenceObject>(KigsID{0u}, maReferenceObject{}) {}
51 
52  virtual ~maReferenceHeritage()
53  {
54  if (mValue.mObj)
55  {
56  UnreferenceModifiable(mValue.mObj);
57  }
58  }
59 
60 
61  // Sets the internal pointer to null, forcing to search again next time we query the reference
62  void ResetFoundModifiable()
63  {
64  mValue.mObj = nullptr;
65  }
66  virtual void CopyData(const CoreModifiableAttributeData<maReferenceObject>& toCopy) override
67  {
68  const auto& toCopyValue = toCopy.const_ref();
69  if (toCopyValue.mObj)
70  {
71  // check if value has changed
72  if (mValue.mObj != toCopyValue.mObj)
73  {
74  if (mValue.mObj)
75  UnreferenceModifiable(mValue.mObj);
76 
77  mValue = toCopyValue;
78 
79  if (mValue.mObj)
80  ReferenceModifiable(mValue.mObj);
81  }
82  }
83  else
84  {
85  if (mValue.mObj)
86  UnreferenceModifiable(mValue.mObj);
87 
88  mValue = toCopyValue;
89  }
90  }
91 
92  operator CoreModifiable*() { return SearchRef(); }
94  //operator CoreModifiable*() { return (CoreModifiable*)SearchRef(); }
95 
96  template<typename T>
97  operator T*(){ return static_cast<T*>(SearchRef()); }
98 
99  CoreModifiable* operator->()
100  {
101  return SearchRef();
102  }
103 
104  operator CoreModifiable&() { return (*SearchRef()); }
105 
107  CoreModifiable& ref() { return (*SearchRef()); }
109  const CoreModifiable& const_ref() { return (*SearchRef()); }
110 
111 
113  virtual bool getValue(kstl::string& value) const override
114  {
115 
116  ((maReferenceHeritage*)this)->SearchRef();
117  if (mValue.mObj)
118  {
119 #ifdef KEEP_NAME_AS_STRING
120  value = mValue.mObj->GetRuntimeType();
121 #else
122  value = std::to_string(mValue.mObj->GetRuntimeType().toUInt());
123 #endif
124 
125  value += ":";
126  value += mValue.mObj->getName();
127  return true;
128  }
129  else
130  {
131  value = mValue.mSearchString;
132  return true;
133  }
134 
135  return false;
136  }
137  virtual bool getValue(usString& value) const override
138  {
139  // TODO ?
140  return false;
141  }
142  virtual bool getValue(CoreModifiable*& value) const override
143  {
144  value = (CoreModifiable*)((maReferenceHeritage*)this)->SearchRef();
145  return true;
146  }
147  virtual bool getValue(void*& value) const override
148  {
149  value = (void*)((maReferenceHeritage*)this)->SearchRef();
150  return true;
151  }
152 
153 
155  virtual bool setValue(const char* value) override
156  {
157  if (this->isReadOnly())
158  return false;
159 
160  InitAndSearch(value);
161  DO_NOTIFICATION(notificationLevel);
162  return true;
163  }
164  virtual bool setValue(const kstl::string& value) override
165  {
166  if (this->isReadOnly())
167  return false;
168 
169  InitAndSearch(value);
170  DO_NOTIFICATION(notificationLevel);
171  return true;
172  }
173  virtual bool setValue(CoreModifiable* value) override
174  {
175  if (this->isReadOnly())
176  return false;
177 
178  if (mValue.mObj != value)
179  {
180  if (mValue.mObj)
181  UnreferenceModifiable(mValue.mObj);
182 
183  mValue.mObj = value;
184 
185  if (mValue.mObj)
186  ReferenceModifiable(mValue.mObj);
187  }
188 
189  DO_NOTIFICATION(notificationLevel);
190  return true;
191  }
192 
194  auto& operator=(const kstl::string &value)
195  {
196  InitAndSearch(value);
197  DO_NOTIFICATION(notificationLevel);
198  return *this;
199  }
200  auto& operator=(CoreModifiable* value)
201  {
202  setValue(value);
203  return *this;
204  }
205 
206 protected:
207 
210  {
211  auto& coremodigiablemap = KigsCore::Instance()->getReferenceMap();
212  auto found = coremodigiablemap.find(current);
213 
214  // ok, the CoreModifiable is here
215  if (found != coremodigiablemap.end())
216  {
217  kstl::vector<CoreModifiableAttribute*>& referencevector = (*found).second;
218 
219  kstl::vector<CoreModifiableAttribute*>::iterator itcurrent = referencevector.begin();
220  kstl::vector<CoreModifiableAttribute*>::iterator itend = referencevector.end();
221 
222  while (itcurrent != itend)
223  {
224  if ((*itcurrent) == this)
225  {
226  referencevector.erase(itcurrent);
227  break;
228  }
229  ++itcurrent;
230  }
231 
232  if (referencevector.size() == 0)
233  {
234  current->unflagAsReferenceRegistered();
235  coremodigiablemap.erase(found);
236  }
237  }
238  }
239  void ReferenceModifiable(CoreModifiable* current)
240  {
241  auto& coremodigiablemap = KigsCore::Instance()->getReferenceMap();
242  auto found = coremodigiablemap.find(current);
243 
244  // ok, the CoreModifiable is already there, add a vector mEntry
245  if (found != coremodigiablemap.end())
246  {
247  kstl::vector<CoreModifiableAttribute*>& referencevector = (*found).second;
248  referencevector.push_back(this);
249  }
250  else
251  {
252  // create a new map mEntry
253  kstl::vector<CoreModifiableAttribute*> toAdd;
254  toAdd.push_back(this);
255  coremodigiablemap[current] = toAdd;
256 
257  current->flagAsReferenceRegistered();
258  }
259  }
260 
261  void Search()
262  {
263  CMSP obj = nullptr;
264  if (!mValue.mSearchString.empty())
265  {
266  obj = CoreModifiable::SearchInstance(mValue.mSearchString, mOwner);
267  }
268  else
269  {
270  if (mValue.mObj)
271  {
272  UnreferenceModifiable(mValue.mObj);
273  mValue.mObj = nullptr;
274  }
275  return;
276  }
277  if (obj)
278  {
279  if (mValue.mObj != obj.get())
280  {
281  if (mValue.mObj)
282  UnreferenceModifiable(mValue.mObj);
283 
284  mValue.mObj = obj.get();
285  ReferenceModifiable(mValue.mObj);
286  }
287  }
288  else
289  {
290  if (mValue.mObj)
291  UnreferenceModifiable(mValue.mObj);
292  mValue.mObj = nullptr;
293  }
294  }
295  void InitAndSearch(const kstl::string& nametype)
296  {
297  CoreModifiable* old_obj = mValue.mObj;
298  mValue = maReferenceObject{ nametype };
299  mValue.mObj = old_obj;
300  Search();
301  }
302  CoreModifiable* SearchRef()
303  {
304  if (mValue.mObj)
305  {
306  return mValue.mObj;
307  }
308  Search();
309  return mValue.mObj;
310  }
311 
312 };
313 
314 // ****************************************
315 // * maReference class
316 // * --------------------------------------
322 // ****************************************
323 
325 
326 
327 
328 
329 
330 
331 
332 #endif //_MAREFERENCE_H
CoreModifiableAttribute::isReadOnly
virtual bool isReadOnly()
Read only attributes cannot be modified with setValue.
Definition: CoreModifiableAttribute.h:276
KigsCore::Instance
static KigsCore * Instance()
return unique instance of KigsCore
Definition: Core.cpp:478
maReferenceHeritage::operator=
auto & operator=(const kstl::string &value)
operators
Definition: maReference.h:194
maReferenceHeritage::getValue
virtual bool getValue(kstl::string &value) const override
getValue overloads
Definition: maReference.h:113
maReferenceHeritage
CoreModifiableAttributeData for reference on CoreModifiable.
Definition: maReference.h:37
KigsCore::getReferenceMap
static kigs::unordered_map< CoreModifiable *, kstl::vector< CoreModifiableAttribute * > > & getReferenceMap()
Get the map of referenced CoreModifiable (maReference)
Definition: Core.h:407
maReferenceHeritage::const_ref
const CoreModifiable & const_ref()
return a const reference on internal value
Definition: maReference.h:109
maReference
CoreModifiableAttribute managing a reference.
CoreModifiable
Base class for Kigs framework objects. CoreModifiable class manage a list of attributes supporting re...
Definition: CoreModifiable.h:400
maReferenceHeritage::UnreferenceModifiable
void UnreferenceModifiable(CoreModifiable *current)
Internals;.
Definition: maReference.h:209
maReferenceHeritage::setValue
virtual bool setValue(const char *value) override
setValue overloads
Definition: maReference.h:155
maReferenceHeritage::ref
CoreModifiable & ref()
return a reference on internal value
Definition: maReference.h:107
CoreModifiableAttribute.h
Base template class for CoreModifiable attributes managing data.