Object.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 #include <vector>
10 #include <exml/exml.hpp>
11 #include <mutex>
12 #include <ememory/memory.hpp>
13 #include <unordered_map>
14 
15 #include <ewol/debug.hpp>
16 #include <ememory/memory.hpp>
17 #include <eproperty/Interface.hpp>
18 #include <eproperty/Value.hpp>
19 #include <eproperty/Range.hpp>
20 #include <eproperty/List.hpp>
21 #include <esignal/Interface.hpp>
22 
23 
24 namespace ewol {
25  // some class need to define element befor other ...
26  class Object;
27  namespace object {
28  class Manager;
29  }
30  class Context;
31 }
32 
33 template<class TYPE_OBJECT> static void baseInit(const ememory::SharedPtr<TYPE_OBJECT>& _object) {
34  // end of recurtion
35  return;
36 }
37 
38 template<class TYPE_OBJECT, class TYPE_VAL, class ... TYPE> static void baseInit(const ememory::SharedPtr<TYPE_OBJECT>& _object, const std::string& _name, const TYPE_VAL& _val, TYPE&& ... _all ) {
39  eproperty::Property* prop(nullptr);
40  eproperty::PropertyType<TYPE_VAL>* propType(nullptr);
41  if (_object == nullptr) {
42  EWOL_ERROR("EMPTY pointer");
43  return;
44  }
45  prop = _object->properties.getRaw(_name);
46  if (prop == nullptr) {
47  EWOL_ERROR("property does not exit ... '" << _name << "'");
48  goto exit_on_error;
49  }
50  propType = dynamic_cast<eproperty::PropertyType<TYPE_VAL>*>(prop);
51  if (propType == nullptr) {
52  EWOL_ERROR("property does not cast in requested type ... '" << _name << "' require type : " << typeid(_val).name() << "' instead of '" << prop->getType() << "'");
53  goto exit_on_error;
54  }
55  propType->setDirectCheck(_val);
56 exit_on_error:
57  baseInit(_object, std::forward<TYPE>(_all)... );
58  return;
59 }
60 
61 #define UN_DECLARE_FACTORY(className) \
62  template<class ... EWOL_FACTORY_CREATE_TYPE> static ememory::SharedPtr<className> create(const EWOL_FACTORY_CREATE_TYPE& ... _all) = delete;
63 
64 #define DECLARE_FACTORY(className) \
65  template<class ... EWOL_FACTORY_CREATE_TYPE> static ememory::SharedPtr<className> create(const EWOL_FACTORY_CREATE_TYPE& ... _all) { \
66  ememory::SharedPtr<className> object(new className()); \
67  if (object == nullptr) { \
68  EWOL_ERROR("Factory error"); \
69  return nullptr; \
70  } \
71  baseInit(object, _all... ); \
72  object->init(); \
73  if (object->objectHasBeenCorectlyInit() == false) { \
74  EWOL_CRITICAL("Object Is not correctly init : " << #className ); \
75  } \
76  return object; \
77  }
78 
79 #define DECLARE_SINGLE_FACTORY(className, uniqueName) \
80  template<class ... EWOL_FACTORY_CREATE_TYPE> static ememory::SharedPtr<className> create(const EWOL_FACTORY_CREATE_TYPE& ... _all) { \
81  ememory::SharedPtr<className> object; \
82  ememory::SharedPtr<ewol::Object> object2 = getObjectNamed(uniqueName); \
83  if (object2 != nullptr) { \
84  object = ememory::dynamicPointerCast<className>(object2); \
85  if (object == nullptr) { \
86  EWOL_CRITICAL("Request object element: '" << uniqueName << "' With the wrong type (dynamic cast error)"); \
87  return nullptr; \
88  } \
89  } \
90  if (object != nullptr) { \
91  return object; \
92  } \
93  object = ememory::SharedPtr<className>(new className()); \
94  if (object == nullptr) { \
95  EWOL_ERROR("Factory error"); \
96  return nullptr; \
97  } \
98  baseInit(object, "name", std::string(uniqueName), _all... ); \
99  object->init(); \
100  if (object->objectHasBeenCorectlyInit() == false) { \
101  EWOL_CRITICAL("Object Is not correctly init : " << #className ); \
102  } \
103  return object; \
104  }
105 
106 namespace ewol {
107  using ObjectShared = ememory::SharedPtr<ewol::Object>;
108  using ObjectWeak = ememory::WeakPtr<ewol::Object>;
113  class Object : public ememory::EnableSharedFromThis<Object>,
114  public eproperty::Interface,
115  public esignal::Interface {
116  public: // Event list
117 
118  public: // propertie list
120  private:
121  static size_t m_valUID;
122  private:
123  bool m_objectHasBeenInit;
124  protected:
128  Object();
129  virtual void init();
130  public:
134  DECLARE_FACTORY(Object);
138  virtual ~Object();
139  bool objectHasBeenCorectlyInit();
140  protected:
142  bool m_destroy;
143  protected:
147  virtual void autoDestroy();
148  public:
152  virtual void destroy();
158  bool isDestroyed() const;
159  protected:
164  virtual void requestDestroyFromChild(const ewol::ObjectShared& _child);
165  public:
170  virtual void setParent(const ewol::ObjectShared& _newParent);
174  virtual void removeParent();
175  private:
176  std::vector<const char*> m_listType;
177  public:
182  const char * const getObjectType() const;
187  std::string getTypeDescription() const;
193  bool isTypeCompatible(const std::string& _type) const;
194  protected:
199  void addObjectType(const char* _type);
200  protected:
201  bool m_static;
202  public:
207  bool getStatic(){
208  return m_static;
209  };
210  private:
211  int32_t m_uniqueId;
212  public:
217  int32_t getId(){
218  return m_uniqueId;
219  };
220  public:
221  // TODO : Rework the position on this function ... This is a convignent function ...
222  bool propertySetOnWidgetNamed(const std::string& _objectName, const std::string& _config, const std::string& _value);
223  public:
230  virtual bool loadXML(const exml::Element& _node);
237  virtual bool storeXML(exml::Element& _node) const;
238  public:
243  static ewol::object::Manager& getObjectManager();
248  static ewol::Context& getContext();
249  private:
250  bool m_isResource;
251  public:
257  void setStatusResource(bool _val) {
258  m_isResource = _val;
259  }
264  bool getStatusResource() const {
265  return m_isResource;
266  }
272  static ewol::ObjectShared getObjectNamed(const std::string& _objectName);
278  virtual ewol::ObjectShared getSubObjectNamed(const std::string& _objectName);
279  protected:
280  // TODO : Create a template ...
284  #define subBind(_type, _name, _event, _shared_ptr, _func, ...) do {\
285  ememory::SharedPtr<_type> myObject = ememory::dynamicPointerCast<_type>(getSubObjectNamed(_name)); \
286  if (myObject != nullptr) { \
287  myObject->_event.connect(_shared_ptr, _func, ##__VA_ARGS__); \
288  } else { \
289  EWOL_ERROR("object named='" << _name << "' not exit or can not be cast in : " << #_type); \
290  } \
291  } while (false)
292  /*
293  template<class TYPE> void subBind(ememory::SharedPtr<ewol::Object> _obj, void (TYPE::*_func)()) {
294  ememory::SharedPtr<TYPE> obj2 = ememory::dynamicPointerCast<TYPE>(_obj);
295  if (obj2 == nullptr) {
296  EWOL_ERROR("Can not connect signal ...");
297  return;
298  }
299  m_callerList.push_back(std::make_pair(ewol::ObjectWeak(_obj), std::connect(_func, obj2.get())));
300  }
301  */
302  };
303  bool propertySetOnObjectNamed(const std::string& _objectName, const std::string& _config, const std::string& _value);
304 };
305 
309 #define globalBind(_type, _name, _event, _obj, _func, ...) do {\
310  ememory::SharedPtr<_type> myObject = ememory::dynamicPointerCast<_type>(ewol::getContext().getEObjectManager().getObjectNamed(_name)); \
311  if (myObject != nullptr) { \
312  myObject->_event.connect(_obj, _func, ##__VA_ARGS__); \
313  } else { \
314  EWOL_ERROR("object named='" << _name << "' not exit or can not be cast in : " << #_type); \
315  } \
316 } while (false)
317 
321 #define externSubBind(_object, _type, _name, _event, _obj, _func, ...) do {\
322  ememory::SharedPtr<_type> myObject = ememory::dynamicPointerCast<_type>(_object->getObjectNamed(_name)); \
323  if (myObject != nullptr) { \
324  myObject->_event.connect(_obj, _func, ##__VA_ARGS__); \
325  } else { \
326  EWOL_ERROR("object named='" << _name << "' not exit or can not be cast in : " << #_type); \
327  } \
328 } while (false)
329 
Basic message classes for ewol system this class mermit at every Object to communicate between them...
Definition: Object.hpp:113
bool m_destroy
Flag to know if the object is requesting has destroy.
Definition: Object.hpp:142
ewol::ObjectWeak m_parent
Reference on the current parrent.
Definition: Object.hpp:141
void init(int _argc, const char **_argv)
Definition: Area.hpp:16
virtual void setDirectCheck(const TYPE &_newVal)
void setStatusResource(bool _val)
Declare this element as a resource (or singleton) this mean the element will not be auto Remove at th...
Definition: Object.hpp:257
bool getStatusResource() const
Get the resource status of the element.
Definition: Object.hpp:264
eproperty::Value< std::string > propertyName
name of the element ...
Definition: Object.hpp:119
bool getStatic()
get the static status of the Object == > mark at true if the user set the object mark as static alloc...
Definition: Object.hpp:207
Definition: Context.hpp:26
int32_t getId()
get the UniqueId of the Object
Definition: Object.hpp:217
Definition: Manager.hpp:18
bool m_static
set this variable at true if this element must not be auto destroy (exemple : use static object) ...
Definition: Object.hpp:201
Context & getContext()
From everyware in the program, we can get the context inteface.