1 /** @file scim_pointer.h
2 * @brief Smart pointer class interface.
4 * Provides a reference-counted-object aware smart pointer class.
6 * Most code of this file are came from Inti project.
9 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
12 * Smart Common Input Method
14 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
15 * Copyright (c) 2002 The Inti Development Team.
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2 of the License, or (at your option) any later version.
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with this program; if not, write to the
30 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
31 * Boston, MA 02111-1307 USA
33 * $Id: scim_pointer.h,v 1.11 2005/01/10 08:30:54 suzhe Exp $
36 #ifndef __SCIM_POINTER_H
37 #define __SCIM_POINTER_H
42 * @addtogroup Accessories
43 * @ingroup InputServiceFramework
49 * @brief Smart pointer template class.
51 * Pointer is a standard auto_ptr-like smart pointer for managing heap
52 * allocated reference counted objects. T must be a class derived from
53 * scim::ReferencedObject.
65 if (!object->is_referenced())
67 object->set_referenced(false);
74 template<typename T1, typename T2>
75 friend bool operator == (const Pointer<T1>& t1, const Pointer<T2>& t2);
78 //! @name Constructors
81 Pointer(T *object = 0) : t(0)
85 //!< Construct a new smart pointer.
86 //!< @param object - a pointer to an object allocated on the heap.
88 //!< <BR>Initialize a new Pointer with any dumb pointer.
90 Pointer(Pointer& src) : t(0)
94 //!< Copy constructor.
95 //!< @param src - a reference to a smart pointer.
97 //!< <BR>Initialize a new Pointer with any compatible Pointer.
99 template <typename T1>
100 Pointer(const Pointer<T1>& src) : t(0)
104 //!< Copy constructor.
105 //!< @param src - a Pointer to type T1 where T1 is derived from T.
107 //!< <BR>Initialize a new Pointer of type T from a Pointer of type T1,
108 //!< only if T1 is derived from T.
115 //!< Decreases the object reference count.
117 Pointer& operator=(T *object)
122 //!< Assignment operator.
123 //!< @param object - a pointer to an object allocated on the heap.
125 //!< <BR>Releases the current dumb pointer, if any and assigns <EM>object</EM>
126 //!< to this Pointer, incrementing its reference count.
128 Pointer& operator=(const Pointer& src)
133 //!< Assignment operator.
134 //!< @param src - a reference to a smart pointer.
136 //!< <BR>Releases the current dumb pointer, if any and assigns the dumb pointer
137 //!< managed by <EM>src</EM> to this Pointer, incrementing its reference count.
139 template <typename T1>
140 Pointer& operator=(const Pointer<T1>& src)
145 //!< Assignment operator.
146 //!< @param src - a Pointer to type T1 where T1 is derived from T.
148 //!< <BR>Releases the current dumb pointer, if any and assigns the dumb pointer
149 //!< of type T1 managed by <EM>src</EM> to this Pointer as a dumb pointer of type T,
150 //!< only if T1 is derived from T. The reference count is incremented.
160 //!< Dereference operator.
161 //!< @return a reference to the object pointed to by the dumb pointer.
163 T* operator->() const
167 //!< Member selection operator.
168 //!< @return the dumb pointer.
174 //!< Conversion operator.
175 //!< Converts a Pointer into its dumb pointer: the C pointer it manages.
176 //!< Normally it is considered pretty evil to mix smart and regular pointers.
177 //!< In scim you can safely if you just follow the reference counting rules
178 //!< for each of them. You can never call delete on Pointer either because
179 //!< you don't call delete on scim objects; you call unref().
185 //!< Returns the dumb pointer; the regular C pointer managed by the Pointer.
186 //!< @return the dumb pointer.
192 //!< Returns true if the Pointer has no dumb pointer.
206 //!< Releases the dumb pointer.
207 //!< @return the regular C pointer previously managed by the Pointer.
209 //!< <BR>Before releasing the dumb pointer its reference count is incremented
210 //!< to prevent it being destroyed. You must call unref() on the pointer to
211 //!< prevent a memory leak.
213 void reset(T *object = 0)
217 //!< Sets a new dumb pointer for the Pointer to manage.
218 //!< @param object - the new dumb pointer.
220 //!< <BR>Releases the current dumb pointer, if any, and assigns <EM>object</EM>
221 //!< to the Pointer, incrementing its reference count.
226 //! @name Equality operators
229 template<typename T1, typename T2>
230 inline bool operator == (const Pointer<T1>& t1, const Pointer<T2>& t2)
234 //!< Compares two Pointers.
235 //!< @return <EM>true</EM> if both Pointers manage to same dumb pointer.
237 template<typename T1, typename T2>
238 inline bool operator != (const Pointer<T1>& t1, const Pointer<T2>& t2)
242 //!< Compares two Pointers.
243 //!< @return <EM>true</EM> if both Pointers manage a different dumb pointer.
246 //! @name C++-style casting functions
249 template <typename To, typename From>
251 cast_const(const Pointer<From>& from)
253 return Pointer<To>(from ? const_cast<To*>(from.get()) : 0);
255 //!< Removes the <EM>const</EM> qualifier from a managed const dumb pointer.
256 //!< @param from - a Pointer that manages a const dumb pointer.
257 //!< @return a new Pointer that manages the non-const dumb pointer.
259 //!< <BR>Calls <EM>const_cast</EM> on the dumb pointer and returns the non-const
260 //!< pointer as a new Pointer.
262 template <typename To, typename From>
264 cast_dynamic(const Pointer<From>& from)
266 return Pointer<To>(dynamic_cast<To*>(from.get()));
268 //!< Casts a managed polymophic dumb pointer down or across its inheritance heirarchy.
269 //!< @param from - a Pointer managing a polymophic dumb pointer of type From.
270 //!< @return a new Pointer managing the dumb pointer as a base or sibling pointer of type <EM>To</EM>.
272 //!< <BR>Calls <EM>dynmaic_cast</EM> to safely cast a managed polymophic dumb pointer
273 //!< of type <EM>From</EM> to a base, derived or sibling class pointer of type <EM>To</EM>.
275 template <typename To, typename From>
277 cast_static(const Pointer<From>& from)
279 return Pointer<To>(from ? static_cast<To*>(from.get()) : 0);
281 //!< Casts a managed dumb pointer to a pointer to a related type.
282 //!< @param from - a Pointer managing a dumb pointer of type From.
283 //!< @return a new Pointer managing the dumb pointer as a pointer of type <EM>To</EM>.
285 //!< <BR>Calls <EM>static_cast</EM> to cast a dumb pointer of type <EM>From</EM> to a
286 //!< pointer of type <EM>To</EM>.
294 #endif //__SCIM_POINTER_H
297 vi:ts=4:nowrap:ai:expandtab