/** @file scim_pointer.h * @brief Smart pointer class interface. * * Provides a reference-counted-object aware smart pointer class. * * Most code of this file are came from Inti project. */ /* * Smart Common Input Method * * Copyright (c) 2002-2005 James Su * Copyright (c) 2002 The Inti Development Team. * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * * $Id: scim_pointer.h,v 1.11 2005/01/10 08:30:54 suzhe Exp $ */ #ifndef __SCIM_POINTER_H #define __SCIM_POINTER_H namespace scim { /** * @addtogroup Accessories * @{ */ /** * @class Pointer * @brief Smart pointer template class. * * Pointer is a standard auto_ptr-like smart pointer for managing heap * allocated reference counted objects. T must be a class derived from * scim::ReferencedObject. */ template class Pointer { T *t; void set(T *object) { if (object) { if (!object->is_referenced()) object->ref(); object->set_referenced(false); } if (t) t->unref(); t = object; } template friend bool operator == (const Pointer& t1, const Pointer& t2); public: //! @name Constructors //! @{ Pointer(T *object = 0) : t(0) { set(object); } //!< Construct a new smart pointer. //!< @param object - a pointer to an object allocated on the heap. //!< //!<
Initialize a new Pointer with any dumb pointer. Pointer(Pointer& src) : t(0) { set(src.get()); } //!< Copy constructor. //!< @param src - a reference to a smart pointer. //!< //!<
Initialize a new Pointer with any compatible Pointer. template Pointer(const Pointer& src) : t(0) { set(src.get()); } //!< Copy constructor. //!< @param src - a Pointer to type T1 where T1 is derived from T. //!< //!<
Initialize a new Pointer of type T from a Pointer of type T1, //!< only if T1 is derived from T. ~Pointer() { set(0); } //!< Destructor. //!< Decreases the object reference count. Pointer& operator=(T *object) { set(object); return *this; } //!< Assignment operator. //!< @param object - a pointer to an object allocated on the heap. //!< //!<
Releases the current dumb pointer, if any and assigns object //!< to this Pointer, incrementing its reference count. Pointer& operator=(const Pointer& src) { set(src.get()); return *this; } //!< Assignment operator. //!< @param src - a reference to a smart pointer. //!< //!<
Releases the current dumb pointer, if any and assigns the dumb pointer //!< managed by src to this Pointer, incrementing its reference count. template Pointer& operator=(const Pointer& src) { set(src.get()); return *this; } //!< Assignment operator. //!< @param src - a Pointer to type T1 where T1 is derived from T. //!< //!<
Releases the current dumb pointer, if any and assigns the dumb pointer //!< of type T1 managed by src to this Pointer as a dumb pointer of type T, //!< only if T1 is derived from T. The reference count is incremented. //! @} //! @name Accessors //! @{ T& operator*() const { return *get(); } //!< Dereference operator. //!< @return a reference to the object pointed to by the dumb pointer. T* operator->() const { return get(); } //!< Member selection operator. //!< @return the dumb pointer. operator T*() const { return get(); } //!< Conversion operator. //!< Converts a Pointer into its dumb pointer: the C pointer it manages. //!< Normally it is considered pretty evil to mix smart and regular pointers. //!< In scim you can safely if you just follow the reference counting rules //!< for each of them. You can never call delete on Pointer either because //!< you don't call delete on scim objects; you call unref(). T* get() const { return t; } //!< Returns the dumb pointer; the regular C pointer managed by the Pointer. //!< @return the dumb pointer. bool null() const { return t == 0; } //!< Returns true if the Pointer has no dumb pointer. //! @} //! @name Methods //! @{ T* release() { T *tmp = t; if (tmp) tmp->ref(); set(0); return tmp; } //!< Releases the dumb pointer. //!< @return the regular C pointer previously managed by the Pointer. //!< //!<
Before releasing the dumb pointer its reference count is incremented //!< to prevent it being destroyed. You must call unref() on the pointer to //!< prevent a memory leak. void reset(T *object = 0) { set(object); } //!< Sets a new dumb pointer for the Pointer to manage. //!< @param object - the new dumb pointer. //!< //!<
Releases the current dumb pointer, if any, and assigns object //!< to the Pointer, incrementing its reference count. //! @} }; //! @name Equality operators //! @{ template inline bool operator == (const Pointer& t1, const Pointer& t2) { return t1.t == t2.t; } //!< Compares two Pointers. //!< @return true if both Pointers manage to same dumb pointer. template inline bool operator != (const Pointer& t1, const Pointer& t2) { return !(t1 == t2); } //!< Compares two Pointers. //!< @return true if both Pointers manage a different dumb pointer. //! @} //! @name C++-style casting functions //! @{ template inline Pointer cast_const(const Pointer& from) { return Pointer(from ? const_cast(from.get()) : 0); } //!< Removes the const qualifier from a managed const dumb pointer. //!< @param from - a Pointer that manages a const dumb pointer. //!< @return a new Pointer that manages the non-const dumb pointer. //!< //!<
Calls const_cast on the dumb pointer and returns the non-const //!< pointer as a new Pointer. template inline Pointer cast_dynamic(const Pointer& from) { return Pointer(dynamic_cast(from.get())); } //!< Casts a managed polymophic dumb pointer down or across its inheritance heirarchy. //!< @param from - a Pointer managing a polymophic dumb pointer of type From. //!< @return a new Pointer managing the dumb pointer as a base or sibling pointer of type To. //!< //!<
Calls dynmaic_cast to safely cast a managed polymophic dumb pointer //!< of type From to a base, derived or sibling class pointer of type To. template inline Pointer cast_static(const Pointer& from) { return Pointer(from ? static_cast(from.get()) : 0); } //!< Casts a managed dumb pointer to a pointer to a related type. //!< @param from - a Pointer managing a dumb pointer of type From. //!< @return a new Pointer managing the dumb pointer as a pointer of type To. //!< //!<
Calls static_cast to cast a dumb pointer of type From to a //!< pointer of type To. //! @} /** @} */ } // namespace scim #endif //__SCIM_POINTER_H /* vi:ts=4:nowrap:ai:expandtab */