Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

ubrick.hpp

00001 /* ==================================================== ======== ======= *
00002  *
00003  *  ubrick.hpp
00004  *  Ubit Project  [Elc][2003]
00005  *  Author: Eric Lecolinet
00006  *
00007  *  Part of the Ubit Toolkit: A Brick Construction Game Model for Creating GUIs
00008  *
00009  *  (C) 1999-2003 Eric Lecolinet @ ENST Paris
00010  *  WWW: http://www.enst.fr/~elc/ubit   Email: elc@enst.fr (subject: ubit)
00011  *
00012  * ***********************************************************************
00013  * COPYRIGHT NOTICE : 
00014  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE 
00015  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 
00016  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU 
00017  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
00018  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00019  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
00020  * ***********************************************************************
00021  *
00022  * ==================================================== [Elc:03] ======= *
00023  * ==================================================== ======== ======= */
00024 
00025 #ifndef _ubrick_hpp_
00026 #define _ubrick_hpp_
00027 //pragma ident  "@(#)ubrick.hpp         ubit:03.06.04"
00028 #include <typeinfo>
00029 #include <stdlib.h>  // for size_t, new(), delete()
00030 #include <ubit/udefs.hpp>
00031 #include <ubit/ubrickImpl.hpp>
00032 #include <ubit/uerror.hpp>
00033 
00034 /* ==================================================== [Elc:03] ======= */
00035 /* ==================================================== ======== ======= */
00036 
00088 class UBrick {  
00089   UBrick(const UBrick&);
00090   UBrick& operator=(const UBrick&);
00092 
00093 public:
00094 
00095   UBrick(u_modes b_modes = 0);
00101   virtual ~UBrick() {destructs();}
00127   static const char* getUbitVersion();
00129 
00130   virtual const char* getClassName() const {return typeid(*this).name();}
00132 
00133   void setAutoUpdate(bool);
00135 
00136   bool isAutoUpdate() const;
00137 
00138   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00139   // access to parents
00140 
00141   virtual int getParentCount() const;
00144   virtual UGroup** getParents() const;
00145   virtual UGroup** getParents(int &parent_count) const;
00156   virtual int getParents(std::vector<UGroup*>& parent_vect) const;
00168   virtual UGroup* getParent(int pos) const;
00180   virtual bool isChildOf(class UGroup *possible_parent, bool indirect) const;
00188   template<class CC>
00189     static bool isInstance(const UBrick*_obj) {return dynamic_cast<const CC*>(_obj);}
00194   virtual void removeFromParents(bool update_parents = true);
00203   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00204   // conditionnal objects
00205 
00206   friend class ULink& operator/(const UCond&, UBrick&);
00207   friend class ULink& operator/(const UCond&, UBrick*);
00214   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00215   // errors and warnings
00216 
00217   static void error(const char* id, const char* msg);
00218   static void error(const char* id, const char* msg, long arg);
00219   static void error(const char* id, const char* msg, const char* arg);
00220   static void error(const char* id, const char* msg, const UStr& arg);
00221   static void error(const char* id, const char* msg, const std::string& arg);
00230   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00231   // implementation
00232 
00233   bool isBmode(u_modes some_Bmodes) const 
00234   {return ((bmodes & some_Bmodes) != 0);}
00236 
00237   u_modes getBmodes() const {return bmodes;}
00239 
00240   void setBmodes(u_modes bmodes, bool on_off);
00242 
00243   void* operator new(size_t);
00244   void  operator delete(void*);
00245   void  addRef() {refcount++;}
00246   void  removeRef();
00247   u_count getRefCount() const {return refcount;}
00249 
00250   virtual void fire(class UEvent&, const class UOn&) const;
00252 
00253   virtual class UProp*  propCast()   {return null;}
00254   virtual class UElem*  elemCast()   {return null;}
00255   virtual class UStr*   strCast()    {return null;}
00256   virtual class UGroup* groupCast()  {return null;}
00257   virtual class UBox*   boxCast()    {return null;}
00258   virtual class UWin*   winCast()    {return null;}
00259   virtual class UGroup* getSubGroup(){return null;}
00261 
00262   virtual void addingTo(class ULink *selflink, class UGroup *parent);
00263   virtual void removingFrom(class ULink *selflink, class UGroup *parent);
00264   virtual void destructs();
00273 #ifndef NO_DOC // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00274 
00275   virtual const char* cname() const {return getClassName();}
00277 
00278   virtual class ULink* makeLink() {return new ULink(this);}
00279   const class ULLChain& getParentList() const {return parents;}
00280   bool hasValidParent() const;
00281   bool hasValidParent(bool& fake_or_deleted_par) const;
00282 
00283   void addToCache(UBrick&);
00284   void addChangeCall(UBrick&);
00285   void addCall(ULink&);
00286 
00287 protected:
00288   friend class ULink;
00289   friend class UArgs;
00290   class ULLChain parents;
00291   class UChain*  cache;  // attributes and callbacks
00292   u_modes  bmodes;
00293   u_count refcount;
00294 #endif
00295 };
00296 
00297 /* ==================================================== [Elc:03] ======= */
00298 /* ==================================================== ======== ======= */
00299 
00300 /* uptr template : Ubit smart pointers.
00301  *
00302  * Ubit smart pointers make it possible to handle objects created in the
00303  * heap (= in dynamic memory) in a safe way. uptr(s) should be used for 
00304  * pointing objects created by 'new' or by 'creator shortcuts' (such as
00305  * ubutton(), ugroup(), ustr(), etc.) 
00306  *
00307  * Ubit smart pointers are somewhat similar to Java references. 
00308  * Objects pointed by uptr(s) are <b>automatically destroyed</b> when:
00309  * - they are not pointed by any uptr any longer, AND
00310  * - they do not have parents in the GUI graph, AND
00311  * - they have been allocated in the heap.
00312  *
00313  * !CAUTION: 'delete' can not be used to destroy object pointed by uptr(s)
00314  * (this would produce an error and the object would not be destroyed).
00315  * To destroy an object : set all uptrs that point to it to null (and 
00316  * remove this object from the GUI graph).
00317  *
00318  *  Examples:
00319  *  <pre><tt>
00320  *    uptr<UStr> p1 = new UStr("111");   // p1 points to a new string
00321  *    uptr<UStr> p2 = ustr("222");       // ustr(..) == *new UStr(...)
00322  *
00323  *    *p1 = *p2;        // the CONTENT of string "222" is copied into
00324  *                      // string "111" (but p1 and p2 still point to
00325  *                      // different objects)
00326  *
00327  *    p1 = p2;          // p1 now points to the same string as p2
00328  *                      // the string that was pointed by p1 is deleted
00329  *                      // as there is no other refernce to it
00330  *
00331  *    p1->append("xyz") // the content of the string pointed by p1 is changed
00332  *
00333  *    cerr << *p1;      // writes the content of the pointed UStr: 222xyz
00334  *    cerr << &p1;      // writes the address of the pointed UStr
00335  *
00336  *    p1 = null;        // p1 points to null. the UStr is deleted
00337  */
00338 template <class CC>
00339 class uptr {
00340   CC* obj;
00341   CC& operator[](int);  
00342 
00343 public:
00344   uptr()         {obj = null;}
00345   uptr(CC& _obj) {obj = &_obj; obj->addRef();}
00346   uptr(CC* _obj) {obj = _obj; if(obj) obj->addRef();}
00348 
00349   uptr(const uptr<CC>& ref2) {obj = ref2.obj; if(obj) obj->addRef();}
00351 
00352   ~uptr() {if (obj) obj->removeRef();}
00354 
00355   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00356 
00357   uptr<CC>& operator=(CC& _obj) {
00358     if(obj) obj->removeRef(); obj = &_obj; if(obj) obj->addRef(); return *this;
00359   }
00360   uptr<CC>& operator=(CC* _obj) {
00361     if(obj) obj->removeRef(); obj = _obj; if(obj) obj->addRef(); return *this;
00362   }
00364 
00365   uptr<CC>& operator=(const uptr<CC>& p2) {
00366     if(obj) obj->removeRef(); obj = p2.obj; if(obj) obj->addRef(); return *this;
00367   }
00369 
00370   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
00371     
00372   operator CC* () const {return obj;}
00374 
00375   CC* operator&() const {return obj;}
00377 
00378   CC& operator*() const {
00379     if (!obj) UError::error("uptr::operator*", "attempt to derefence a uptr (Ubit smart pointer) that points to null; ref:", (long)this);
00380     return *obj;
00381   }
00383 
00384   CC* operator->() const {
00385     if (!obj) UError::error("uptr::operator->", "attempt to derefence a uptr (Ubit  smart pointer) that points to null; ref:", (long)this);
00386     return obj;
00387   }
00389 };
00390 
00391 /* ==================================================== [TheEnd] ======= */
00392 /* ==================================================== [Elc:03] ======= */
00393 #endif

Generated on Thu Mar 3 09:57:27 2005 for Ubit[Eric.Lecolinet@enst.fr] by  doxygen 1.4.1