X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ETL%2Ftrunk%2FETL%2F_handle.h;h=3f0750381096b766c0fd9f5708d491b66ab98308;hb=d15c4522466bedfbe61620c401becae0931854f5;hp=0cf5af2854d39121098831818d7e5001ec919431;hpb=b3016b249333ac0ab0008d8c6c4d9029b2ff30c9;p=synfig.git diff --git a/ETL/trunk/ETL/_handle.h b/ETL/trunk/ETL/_handle.h index 0cf5af2..3f07503 100644 --- a/ETL/trunk/ETL/_handle.h +++ b/ETL/trunk/ETL/_handle.h @@ -1,11 +1,12 @@ /* === E T L =============================================================== */ /*! \file _handle.h -** $Id: _handle.h,v 1.1.1.1 2005/01/04 01:31:47 darco Exp $ +** $Id$ ** \brief Template Object Handle Implementation ** \internal ** ** \legal ** Copyright (c) 2002 Robert B. Quattlebaum Jr. +** Copyright (c) 2007 Chris Moore ** ** This package is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License as @@ -66,7 +67,7 @@ class shared_object { private: mutable int refcount; - + protected: shared_object():refcount(0) { } @@ -75,11 +76,11 @@ protected: #else ~shared_object() { } #endif - -public: + +public: void ref()const { assert(refcount>=0); refcount++; } - + //! Returns \c false if object needs to be deleted bool unref()const { @@ -97,7 +98,7 @@ public: return true; } - + int count()const { return refcount; } }; // END of class shared_object @@ -140,6 +141,9 @@ public: typedef int size_type; protected: +#ifdef _DEBUG +public: +#endif value_type *obj; //!< Pointer to object public: @@ -182,7 +186,7 @@ public: return *this; } */ - + //! Assignment operator handle & operator=(const handle &x) @@ -229,7 +233,7 @@ public: void reset() { detach(); } bool empty()const { return obj==0; } - + //! Creates a new instance of a T object and puts it in the handle. /*! Uses the default constructor */ void spawn() { operator=(handle(new T())); } @@ -257,59 +261,48 @@ public: operator->()const { assert(obj); return obj; } - //! static_cast<> overload -- Useful for implicit casts - template - operator handle()const - { return handle(static_cast(obj)); } + //! More explicit bool cast + operator bool()const + { return obj!=NULL; } operator handle()const { return handle(static_cast(obj)); } //! static_cast\<\> wrapper - template static - handle cast_static(const handle &x) - { return handle(static_cast(x.get())); } - + template static handle cast_static (const handle &x) { return handle(static_cast (x.get())); } //! dynamic_cast\<\> wrapper - template static - handle cast_dynamic(const handle &x) - { return handle(dynamic_cast(x.get())); } - + template static handle cast_dynamic (const handle &x) { return handle(dynamic_cast (x.get())); } //! const_cast\<\> wrapper - template static - handle cast_const(const handle &x) - { return handle(const_cast(x.get())); } - + template static handle cast_const (const handle &x) { return handle(const_cast (x.get())); } //! reinterpret_cast\<\> wrapper - template static - handle cast_reinterpret(const handle &x) - { return handle(reinterpret_cast(x.get())); } + template static handle cast_reinterpret(const handle &x) { return handle(reinterpret_cast(x.get())); } - template static handle cast_static(const loose_handle &x); - template static handle cast_dynamic(const loose_handle &x); - template static handle cast_const(const loose_handle &x); - template static handle cast_reinterpret(const loose_handle &x); + template static handle cast_static (const loose_handle &x); + template static handle cast_dynamic (const loose_handle &x); + template static handle cast_const (const loose_handle &x); + template static handle cast_reinterpret(const loose_handle &x); - template static handle cast_static(const rhandle &x); - template static handle cast_dynamic(const rhandle &x); - template static handle cast_const(const rhandle &x); - template static handle cast_reinterpret(const rhandle &x); + template static handle cast_static (const rhandle &x); + template static handle cast_dynamic (const rhandle &x); + template static handle cast_const (const rhandle &x); + template static handle cast_reinterpret(const rhandle &x); - template static handle cast_static(U* x); - template static handle cast_dynamic(U* x); - template static handle cast_const(U* x); - template static handle cast_reinterpret(U* x); + template static handle cast_static (U* x); + template static handle cast_dynamic (U* x); + template static handle cast_const (U* x); + template static handle cast_reinterpret(U* x); //! Returns pointer to the object that is being wrapped pointer get()const { return obj; } - //! More explicit bool cast - operator bool()const - { return obj!=NULL; } - bool operator!()const { return !obj; } + + //! static_cast<> overload -- Useful for implicit casts + template + operator handle()const + { return handle(static_cast(obj)); } }; // END of template class handle // ======================================================================== @@ -326,20 +319,20 @@ private: public: void *front_; void *back_; - + protected: rshared_object():rrefcount(0),front_(0),back_(0) { } - -public: + +public: void rref()const { rrefcount++; } - + void runref()const { assert(rrefcount>0); rrefcount--; } - + int rcount()const { return rrefcount; } }; // END of class rshared_object @@ -369,9 +362,9 @@ public: using handle::unique; using handle::operator bool; using handle::get; - using handle::operator *; - using handle::operator ->; - + using handle::operator*; + using handle::operator->; + /* operator const handle&()const { return *this; } @@ -379,25 +372,25 @@ public: private: using handle::obj; - + rhandle *prev_; rhandle *next_; void add_to_rlist() { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing assert(obj); obj->rref(); - // If this is the first reversable handle + // If this is the first reversible handle if(!obj->front_) { obj->front_=obj->back_=this; prev_=next_=0; return; } - + prev_=reinterpret_cast*>(obj->back_); next_=0; prev_->next_=this; @@ -406,11 +399,11 @@ private: void del_from_rlist() { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing assert(obj); obj->runref(); - // If this is the last reversable handle + // If this is the last reversible handle if(obj->front_==obj->back_) { obj->front_=obj->back_=0; @@ -428,7 +421,7 @@ private: else next_->prev_=prev_; } - + public: //! Default constructor - empty handle @@ -437,26 +430,26 @@ public: //! Constructor that constructs from a pointer to new object rhandle(pointer x):handle(x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(obj)add_to_rlist(); } rhandle(const handle &x):handle(x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(obj)add_to_rlist(); } //! Default copy constructor rhandle(const rhandle &x):handle(x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(obj)add_to_rlist(); } //! Handle is released on deletion ~rhandle() { detach(); } - + //! Template Assignment operator /*! \note This class may not be necessary, and may be removed ** at some point in the future. @@ -479,12 +472,12 @@ public: return *this; } */ - + //! Assignment operator rhandle & operator=(const rhandle &x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(x.get()==obj) return *this; @@ -502,7 +495,7 @@ public: rhandle& operator=(const handle &x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(x.get()==obj) return *this; @@ -520,7 +513,7 @@ public: rhandle& operator=(value_type* x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(x==obj) return *this; @@ -540,7 +533,7 @@ public: void detach() { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing if(obj)del_from_rlist(); handle::detach(); obj=0; @@ -555,11 +548,11 @@ public: /*! Uses the default constructor */ void spawn() { operator=(handle(new T())); } - //! Returns number of reversable instances + //! Returns number of reversible instances count_type rcount()const { -// value_type*const& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*const& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing return obj?obj->rcount():0; } @@ -567,42 +560,42 @@ public: bool runique()const { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing assert(obj); return obj->front_==obj->back_; } //! \writeme int replace(const handle &x) { -// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 form barfing +// value_type*& obj(handle::obj); // Required to keep gcc 3.4.2 from barfing assert(obj); assert(x.get()!=obj); - + if(x.get()==obj) return 0; - + rhandle *iter; rhandle *next; - + iter=reinterpret_cast*>(obj->front_); - assert(iter); + assert(iter); next=iter->next_; - + int i=0; #ifndef NDEBUG pointer obj_=obj; #endif - + for(;iter;iter=next,next=iter?iter->next_:0,i++) { - assert(iter->get()==obj_); + assert(iter->get()==obj_); (*iter)=x; } assert(obj==x.get()); - + return i; } @@ -622,11 +615,6 @@ public: }; // END of template class rhandle - - - - - // ======================================================================== /*! \class loose_handle _handle.h ETL/handle ** \brief Loose Object Handle @@ -647,6 +635,9 @@ public: typedef int size_type; protected: +#ifdef _DEBUG +public: +#endif value_type *obj; //!< Pointer to object public: @@ -710,7 +701,7 @@ public: //void release() { detach(); } void reset() { detach(); } - + bool empty()const { return obj==0; } handle clone()const { assert(obj); return obj->clone(); } @@ -762,141 +753,53 @@ public: bool unref() { if(obj && !obj->unref()){ obj=0; return false; } return true; } }; // END of template class loose_handle - - - -template template handle -handle::cast_static(const loose_handle &x) - { return handle(static_cast(x.get())); } - -template template handle -handle::cast_dynamic(const loose_handle &x) - { return handle(dynamic_cast(x.get())); } - -template template handle -handle::cast_const(const loose_handle &x) - { return handle(const_cast(x.get())); } - -template template handle -handle::cast_reinterpret(const loose_handle &x) - { return handle(reinterpret_cast(x.get())); } - - - -template template handle -handle::cast_static(const rhandle &x) - { return handle(static_cast(x.get())); } - -template template handle -handle::cast_dynamic(const rhandle &x) - { return handle(dynamic_cast(x.get())); } - -template template handle -handle::cast_const(const rhandle &x) - { return handle(const_cast(x.get())); } - -template template handle -handle::cast_reinterpret(const rhandle &x) - { return handle(reinterpret_cast(x.get())); } - - - -template template handle -handle::cast_static(U* x) - { return handle(static_cast(x)); } - -template template handle -handle::cast_dynamic(U* x) - { return handle(dynamic_cast(x)); } - -template template handle -handle::cast_const(U* x) - { return handle(const_cast(x)); } - -template template handle -handle::cast_reinterpret(U* x) - { return handle(reinterpret_cast(x)); } - - - - - -template bool -operator==(const handle &lhs,const handle &rhs) - { return (lhs.get()==rhs.get()); } -template bool -operator==(const loose_handle &lhs,const loose_handle &rhs) - { return (lhs.get()==rhs.get()); } -template bool -operator==(const handle &lhs,const loose_handle &rhs) - { return (lhs.get()==rhs.get()); } -template bool -operator==(const loose_handle &lhs,const handle &rhs) - { return (lhs.get()==rhs.get()); } -template bool -operator==(const handle &lhs,const T *rhs) - { return (lhs.get()==rhs); } -template bool -operator==(const loose_handle &lhs,const T *rhs) - { return (lhs.get()==rhs); } -template bool -operator==(const T *lhs,const handle &rhs) - { return (lhs==rhs.get()); } -template bool -operator==(const T *lhs,const loose_handle &rhs) - { return (lhs==rhs.get()); } - - -template bool -operator!=(const handle &lhs,const handle &rhs) - { return (lhs.get()!=rhs.get()); } -template bool -operator!=(const loose_handle &lhs,const loose_handle &rhs) - { return (lhs.get()!=rhs.get()); } -template bool -operator!=(const handle &lhs,const loose_handle &rhs) - { return (lhs.get()!=rhs.get()); } -template bool -operator!=(const loose_handle &lhs,const handle &rhs) - { return (lhs.get()!=rhs.get()); } -template bool -operator!=(const handle &lhs,const T *rhs) - { return (lhs.get()!=rhs); } -template bool -operator!=(const loose_handle &lhs,const T *rhs) - { return (lhs.get()!=rhs); } -template bool -operator!=(const T *lhs,const handle &rhs) - { return (lhs!=rhs.get()); } -template bool -operator!=(const T *lhs,const loose_handle &rhs) - { return (lhs!=rhs.get()); } - - -template bool -operator<(const handle &lhs,const handle &rhs) - { return (lhs.get() bool -operator<(const loose_handle &lhs,const loose_handle &rhs) - { return (lhs.get() bool -operator<(const handle &lhs,const loose_handle &rhs) - { return (lhs.get() bool -operator<(const loose_handle &lhs,const handle &rhs) - { return (lhs.get() bool -operator<(const handle &lhs,const T *rhs) - { return (lhs.get() bool -operator<(const loose_handle &lhs,const T *rhs) - { return (lhs.get() bool -operator<(const T *lhs,const handle &rhs) - { return (lhs bool -operator<(const T *lhs,const loose_handle &rhs) - { return (lhs -> handle<> +template template handle handle::cast_static (const loose_handle& x) { return handle(static_cast (x.get())); } +template template handle handle::cast_dynamic (const loose_handle& x) { return handle(dynamic_cast (x.get())); } +template template handle handle::cast_const (const loose_handle& x) { return handle(const_cast (x.get())); } +template template handle handle::cast_reinterpret(const loose_handle& x) { return handle(reinterpret_cast(x.get())); } + +// cast rhandle_handle<> -> handle<> +template template handle handle::cast_static (const rhandle& x) { return handle(static_cast (x.get())); } +template template handle handle::cast_dynamic (const rhandle& x) { return handle(dynamic_cast (x.get())); } +template template handle handle::cast_const (const rhandle& x) { return handle(const_cast (x.get())); } +template template handle handle::cast_reinterpret(const rhandle& x) { return handle(reinterpret_cast(x.get())); } + +// cast U* -> handle<> +template template handle handle::cast_static (U* x) { return handle(static_cast (x)); } +template template handle handle::cast_dynamic (U* x) { return handle(dynamic_cast (x)); } +template template handle handle::cast_const (U* x) { return handle(const_cast (x)); } +template template handle handle::cast_reinterpret(U* x) { return handle(reinterpret_cast(x)); } + +// operator== for handle<>, loose_handle<> and T* +template bool operator==(const handle & lhs,const handle & rhs) { return (lhs.get()==rhs.get()); } +template bool operator==(const loose_handle& lhs,const loose_handle& rhs) { return (lhs.get()==rhs.get()); } +template bool operator==(const handle & lhs,const loose_handle& rhs) { return (lhs.get()==rhs.get()); } +template bool operator==(const loose_handle& lhs,const handle & rhs) { return (lhs.get()==rhs.get()); } +template bool operator==(const handle& lhs,const T* rhs) { return (lhs.get()==rhs); } +template bool operator==(const loose_handle& lhs,const T* rhs) { return (lhs.get()==rhs); } +template bool operator==(const T* lhs,const handle& rhs) { return (lhs ==rhs.get()); } +template bool operator==(const T* lhs,const loose_handle& rhs) { return (lhs ==rhs.get()); } + +// operator!= for handle<>, loose_handle<> and T* +template bool operator!=(const handle & lhs,const handle & rhs) { return (lhs.get()!=rhs.get()); } +template bool operator!=(const loose_handle& lhs,const loose_handle& rhs) { return (lhs.get()!=rhs.get()); } +template bool operator!=(const handle & lhs,const loose_handle& rhs) { return (lhs.get()!=rhs.get()); } +template bool operator!=(const loose_handle& lhs,const handle & rhs) { return (lhs.get()!=rhs.get()); } +template bool operator!=(const handle& lhs,const T* rhs) { return (lhs.get()!=rhs); } +template bool operator!=(const loose_handle& lhs,const T* rhs) { return (lhs.get()!=rhs); } +template bool operator!=(const T* lhs,const handle& rhs) { return (lhs !=rhs.get()); } +template bool operator!=(const T* lhs,const loose_handle& rhs) { return (lhs !=rhs.get()); } + +// operator< for handle<>, loose_handle<> and T* +template bool operator<(const handle& lhs,const handle& rhs) { return (lhs.get() bool operator<(const loose_handle& lhs,const loose_handle& rhs) { return (lhs.get() bool operator<(const handle& lhs,const loose_handle& rhs) { return (lhs.get() bool operator<(const loose_handle& lhs,const handle& rhs) { return (lhs.get() bool operator<(const handle& lhs,const T* rhs) { return (lhs.get() bool operator<(const loose_handle& lhs,const T* rhs) { return (lhs.get() bool operator<(const T* lhs,const handle& rhs) { return (lhs bool operator<(const T* lhs,const loose_handle& rhs) { return (lhs