ubit
uobject.hpp
1 /************************************************************************
2  *
3  * uobject.hpp: Base Class for all Ubit Objects
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _uobject_hpp_
18 #define _uobject_hpp_ 1
19 #include <stdlib.h> // for size_t, new(), delete()
20 #include <exception>
21 #include <ubit/udefs.hpp>
22 #include <ubit/uclass.hpp>
23 namespace ubit {
24 
27  class UError : public std::exception {
28  public:
29  enum {
30  FATAL_ERROR = -1, WARNING, ERROR, INTERNAL_ERROR, STYLE_ERROR, CSS_ERROR, XML_ERROR
31  };
32 
33  UError(int errnum, const UObject*, const char* funcname);
34  virtual ~UError() throw ();
35 
36  virtual const char* what() const throw();
37 
38  static const int message_capacity = 2000;
39  int errnum;
40  const UObject* object;
41  const char* function_name;
42  char message[message_capacity];
43  };
44 
45  void uerror(const char* function_name, const char* format, ...);
54  void uwarning(const char* function_name, const char* format, ...);
56 
57 
58  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113  class UObject {
114  public:
115  struct MetaClass : public UClass {
116  MetaClass() : UClass("UObject") {}
117  //virtual bool isInstance(UObject& obj) const {return dynamic_cast<UObject&>(obj);}
118  virtual bool isInstance(UObject* obj) const {return dynamic_cast<UObject*>(obj);}
119  virtual UObject* newInstance() const {return null;}
120  virtual UStyle* newStyle() const {return null;}
121  };
122 
123  UObject();
124  UObject(const UObject&);
125  virtual ~UObject();
126  UObject& operator=(const UObject&);
127 
128  static const char* getVersion();
130 
131  static const UClass& Class() {static MetaClass& c = *new MetaClass; return c;}
133 
134  virtual const UClass& getClass() const {return Class();}
143  const UStr& getClassName() const;
145 
146  static UStyle* createStyle() {return null;}
147 
148  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149 
150  virtual void error(const char* function_name, const char* format, ...) const;
159  virtual void warning(const char* function_name, const char* format, ...) const;
161 
162  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
163 
164  bool isDeletable() const {return omodes.IS_DYNAMIC;}
170  bool isConst() const {return omodes.IS_CONST;}
172 
173  UObject& setConst();
185  bool isAutoUpdate() const {return !omodes.DONT_AUTO_UPDATE;}
187 
188  UObject& setAutoUpdate(bool state = true);
194  bool isIgnoringChangeCallbacks() const {return omodes.IGNORE_CHANGE_CALLBACKS;}
196 
197  UObject& ignoreChangeCallbacks(bool state);
199 
200  // - - - dynamic cast - - - - - - - - - - - - - - - - - - - - - - - - - - - -
201 
202  virtual UNode* toNode() {return null;}
204 
205  virtual const UNode* toNode() const {return null;}
207 
208  virtual UAttr* toAttr() {return null;}
210 
211  virtual const UAttr* toAttr() const {return null;}
213 
214  virtual UElem* toElem() {return null;}
216 
217  virtual const UElem* toElem() const {return null;}
219 
220  virtual UData* toData() {return null;}
222 
223  virtual const UData* toData() const {return null;}
225 
226  virtual UStr* toStr() {return null;}
228 
229  virtual const UStr* toStr() const {return null;}
231 
232  virtual UCall* toCall() {return null;}
234 
235  virtual const UCall* toCall() const {return null;}
237 
238  virtual UBox* toBox() {return null;}
240 
241  virtual const UBox* toBox() const {return null;}
243 
244  virtual UWin* toWin() {return null;}
246 
247  virtual const UWin* toWin() const {return null;}
249 
250  virtual UMenu* toMenu() {return null;}
252 
253  virtual const UMenu* toMenu() const {return null;}
255 
256  // - - - Impl - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257 
258  typedef unsigned char State;
259  typedef short PtrCount;
260 
261  State getState() const {return ostate;}
263 
264  void setState(State s) {ostate = s;}
266 
267  PtrCount getPtrCount() const {return ptr_count;}
269 
271  struct Modes {
272  unsigned IS_DYNAMIC:1; // this obj was allocated in dynamic memory
273  unsigned IS_CONST:1; // this obj is const, its value can't be changed
274  unsigned IS_DESTRUCTED:1; // this obj has been destructed
275  unsigned IS_DESTRUCTING:1; // this obj is being destructed
276  unsigned DONT_AUTO_UPDATE:1; // set(), add() and remove() do NOT call update().
277  unsigned IS_UPDATING:1; // is being updated (repainted or re-layed out).
278  unsigned IS_IN_SELECTION:1; // is part of a text selection
279  unsigned IGNORE_CHANGE_CALLBACKS:1;
280  };
281 
282  struct UConst {};
283 
284  static UConst UCONST;
290  bool checkConst() const;
292 
293  bool isDestructed() const {return omodes.IS_DESTRUCTED;}
295 
296  bool isDestructing() const {return omodes.IS_DESTRUCTING;}
298 
299  virtual bool hasSceneGraphParent() const {return false;}; // !MUST be virtual!
301 
302  void* operator new(size_t);
304 
305  void operator delete(void*);
307 
308  void addPtr() const;
309  void removePtr() const;
310 
311  protected:
312  friend class UNode;
313  friend class UUpdateContext;
314  friend class UAppliImpl;
315  friend class USelection;
316  friend class UPtr;
317 
318  mutable PtrCount ptr_count; // uptr<> reference count
319  Modes omodes;
320  State ostate;
321  };
322 
323 
324  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
355  class UPtr {
356  protected:
357  //void addPtr(const UObject* o) {if (o) o->addPtr();}
358  //void removePtr(const UObject* o) {if (o) o->removePtr();}
359  void deferenceError() const;
360  };
361 
362 
365  template <class CC> class uptr : public UPtr {
366  public:
367  void addPtr(const CC* o) {if (o) o->addPtr();}
368  void removePtr(const CC* o) {if (o) o->removePtr();}
369 
370  uptr() {obj = null;}
371  uptr(CC& _obj) {obj = &_obj; addPtr(obj);}
372  uptr(CC* _obj) {obj = _obj; addPtr(obj);}
374 
375  uptr(const uptr<CC>& p2) {obj = p2.obj; addPtr(obj);}
377 
378  ~uptr() {removePtr(obj);}
380 
381  uptr<CC>& operator=(CC& _obj) {
382  removePtr(obj); obj = &_obj; addPtr(obj); return *this;
383  }
384  uptr<CC>& operator=(CC* _obj) {
385  removePtr(obj); obj = _obj; addPtr(obj); return *this;
386  }
388 
390  removePtr(obj); obj = p2.obj; addPtr(obj); return *this;
391  }
393 
394  operator CC*() const {return obj;}
396 
397  CC* operator&() const {return obj;}
399 
400  CC* operator()() const {return obj;}
402 
403  CC& operator*() const {
404  if (!obj) deferenceError();
405  return *obj;
406  }
408 
409  CC* operator->() const {
410  if (!obj) deferenceError();
411  return obj;
412  }
414 
415  private:
416  CC* obj;
417  CC& operator[](int); // [] meaningless and forbidden.
418  };
419 
420 }
421 #endif
422 
Base class for attributes.
Definition: uattr.hpp:97
Definition: uobject.hpp:115
bool isDestructing() const
[impl] this object is being destructed (one of its destructors has been called).
Definition: uobject.hpp:296
uptr< CC > & operator=(CC *_obj)
assignment: ptr = obj; (obj can be null).
Definition: uobject.hpp:384
text selection.
Definition: uselection.hpp:25
virtual UData * toData()
dynamic cast: returns this object if it derives from UData and null otherwise.
Definition: uobject.hpp:220
Definition: uobject.hpp:282
Box container.
Definition: ubox.hpp:64
virtual const UAttr * toAttr() const
dynamic cast: returns this object if it derives from UAttr and null otherwise.
Definition: uobject.hpp:211
CC & operator*() const
dereferencing: if &#39;ptr&#39; points to &#39;obj&#39; then *ptr return *obj.
Definition: uobject.hpp:403
Definition: uappliImpl.hpp:40
Base class of objects that can be added to the UBIT scene graph (SEE DETAILS!).
Definition: unode.hpp:38
virtual UMenu * toMenu()
dynamic cast: returns this object if it derives from UMenu and null otherwise.
Definition: uobject.hpp:250
virtual const UCall * toCall() const
dynamic cast: returns this object if it derives from UCall and null otherwise.
Definition: uobject.hpp:235
CC * operator->() const
dereferencing: if &#39;ptr&#39; points to &#39;obj&#39; then ptr->foo() calls obj->foo().
Definition: uobject.hpp:409
[Impl] internal object modes.
Definition: uobject.hpp:271
virtual UStr * toStr()
dynamic cast: returns this object if it derives from UStr and null otherwise.
Definition: uobject.hpp:226
uptr(CC *_obj)
constructors: uptr<ObjClass> ptr = obj;
Definition: uobject.hpp:372
virtual const UElem * toElem() const
dynamic cast: returns this object if it derives from UElem and null otherwise.
Definition: uobject.hpp:217
virtual UBox * toBox()
dynamic cast: returns this object if it derives from UBox and null otherwise.
Definition: uobject.hpp:238
virtual UStyle * newStyle() const
creates a new instance of the style of this class (if applicable).
Definition: uobject.hpp:120
static const UClass & Class()
class method that returns the metaclass of this class.
Definition: uobject.hpp:131
virtual const UData * toData() const
dynamic cast: returns this object if it derives from UData and null otherwise.
Definition: uobject.hpp:223
virtual UNode * toNode()
dynamic cast: returns this object if it derives from UNode and null otherwise.
Definition: uobject.hpp:202
Base class for Viewable Objects.
Definition: udata.hpp:31
CC * operator()() const
returns pointed object: if &#39;ptr&#39; points to &#39;obj&#39; then ptr() returns obj.
Definition: uobject.hpp:400
bool isDestructed() const
[impl] this object has been destructed (but memory has not been freed yet).
Definition: uobject.hpp:293
uptr< CC > & operator=(const uptr< CC > &p2)
assignment: ptr = ptr2;
Definition: uobject.hpp:389
virtual const UWin * toWin() const
dynamic cast: returns this object if it derives from UWin and null otherwise.
Definition: uobject.hpp:247
lightweight general purpose container.
Definition: uelem.hpp:44
virtual UWin * toWin()
dynamic cast: returns this object if it derives from UWin and null otherwise.
Definition: uobject.hpp:244
virtual const UStr * toStr() const
dynamic cast: returns this object if it derives from UStr and null otherwise.
Definition: uobject.hpp:229
Smart Pointer for UObject instances (.
Definition: uobject.hpp:365
base class of callback objects for firing functions or methods.
Definition: ucall.hpp:144
virtual UAttr * toAttr()
dynamic cast: returns this object if it derives from UAttr and null otherwise.
Definition: uobject.hpp:208
UMenu: pulldown and cascaded menux.
Definition: umenu.hpp:64
bool isConst() const
checks whether the content of this object can be modified (
Definition: uobject.hpp:170
static UConst UCONST
UCONST can be given as an argument to some constructors to make the object constant.
Definition: uobject.hpp:284
Ubit class.
Definition: uclass.hpp:74
~uptr()
destructor: deletes pointed object (if not null and not pointed elsewhere).
Definition: uobject.hpp:378
bool isAutoUpdate() const
return current update policy:
Definition: uobject.hpp:185
Ubit exception.
Definition: uobject.hpp:27
Definition: uupdatecontext.hpp:32
virtual UElem * toElem()
dynamic cast: returns this object if it derives from UElem and null otherwise.
Definition: uobject.hpp:214
uptr(const uptr< CC > &p2)
constructor: uptr<ObjClass> ptr = p2;
Definition: uobject.hpp:375
Definition: uhardfont.hpp:31
virtual const UNode * toNode() const
dynamic cast: returns this object if it derives from UNode and null otherwise.
Definition: uobject.hpp:205
virtual const UMenu * toMenu() const
dynamic cast: returns this object if it derives from UMenu and null otherwise.
Definition: uobject.hpp:253
virtual const UClass & getClass() const
instance method that returns the metaclass of this object.
Definition: uobject.hpp:134
Definition: tstclass.hpp:5
bool isIgnoringChangeCallbacks() const
return true if change callbacks are fired.
Definition: uobject.hpp:194
Base class for windows and menus.
Definition: uwin.hpp:47
void setState(State s)
changes the object state.
Definition: uobject.hpp:264
virtual UObject * newInstance() const
creates a new instance of the corresponding class (if applicable).
Definition: uobject.hpp:119
virtual UCall * toCall()
dynamic cast: returns this object if it derives from UCall and null otherwise.
Definition: uobject.hpp:232
virtual const UBox * toBox() const
dynamic cast: returns this object if it derives from UBox and null otherwise.
Definition: uobject.hpp:241
bool isDeletable() const
checks if this object can be destroyed by &#39;delete&#39;.
Definition: uobject.hpp:164
State getState() const
returns the current object state.
Definition: uobject.hpp:261
PtrCount getPtrCount() const
[impl] returns the number of uptr<> smart pointers that are pointing to this object.
Definition: uobject.hpp:267
Compiled Object Style.
Definition: ustyle.hpp:44
Smart Pointer for UObject instances.
Definition: uobject.hpp:355
Ubit String.
Definition: ustr.hpp:72
Base class of most Ubit objects (SEE DETAILS!).
Definition: uobject.hpp:113
CC * operator &() const
returns pointed object: if &#39;ptr&#39; points to &#39;obj&#39; then &ptr returns obj.
Definition: uobject.hpp:397