1 | /** @file scim_object.h |
2 | * @brief Reference counted base class interface. |
3 | * |
4 | * Provides a reference counted base class |
5 | * for dynamic objects handled the scim smart pointer. |
6 | * |
7 | * Most code of this file are came from Inti project. |
8 | */ |
9 | |
10 | /* |
11 | * Smart Common Input Method |
12 | * |
13 | * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn> |
14 | * Copyright (c) 2002 The Inti Development Team. |
15 | * |
16 | * |
17 | * This library is free software; you can redistribute it and/or |
18 | * modify it under the terms of the GNU Lesser General Public |
19 | * License as published by the Free Software Foundation; either |
20 | * version 2 of the License, or (at your option) any later version. |
21 | * |
22 | * This library is distributed in the hope that it will be useful, |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | * GNU Lesser General Public License for more details. |
26 | * |
27 | * You should have received a copy of the GNU Lesser General Public |
28 | * License along with this program; if not, write to the |
29 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
30 | * Boston, MA 02111-1307 USA |
31 | * |
32 | * $Id: scim_object.h,v 1.9 2005/01/10 08:30:54 suzhe Exp $ |
33 | */ |
34 | |
35 | #ifndef __SCIM_OBJECT_H |
36 | #define __SCIM_OBJECT_H |
37 | |
38 | namespace scim { |
39 | |
40 | /** |
41 | * @addtogroup Accessories |
42 | * @{ |
43 | */ |
44 | |
45 | /** |
46 | * @class ReferencedObject |
47 | * @brief Reference counted base class. |
48 | * |
49 | * ReferencedObject is a reference counting base class. |
50 | * it has an integer reference counter so that dynamic objects |
51 | * can have their memory allocation handled by the scim |
52 | * smart pointer: Pointer<>. This keeps the memory management |
53 | * in scim consistent across all classes. |
54 | * If you derive a class from ReferencedObject and allocate it |
55 | * on the heap, you free the memory and destroy the object by |
56 | * calling unref(), not delete. |
57 | */ |
58 | class ReferencedObject |
59 | { |
60 | template<typename T> friend class Pointer; |
61 | |
62 | ReferencedObject(const ReferencedObject&); |
63 | ReferencedObject& operator=(const ReferencedObject&); |
64 | |
65 | bool m_referenced; |
66 | int m_ref_count; |
67 | |
68 | protected: |
69 | ReferencedObject(); |
70 | //!< Constructor. |
71 | |
72 | virtual ~ReferencedObject() = 0; |
73 | //!< Destructor. |
74 | |
75 | void set_referenced(bool reference); |
76 | //!< Set the internal referenced flag. |
77 | //!< @param reference - <EM>true</EM> if the initial reference count must be removed by owner. |
78 | //!< |
79 | //!< <BR>Called by derived classes to set the referenced flag. A object sets this flag |
80 | //!< to true , indicating that it owns the initial reference count and unref() must be called. |
81 | public: |
82 | bool is_referenced() const; |
83 | //!< The referenced flag setting. |
84 | //!< @return <EM>true</EM> if unref() must be explicitly called on this object. |
85 | |
86 | void ref(); |
87 | //!< Increase an object's reference count by one. |
88 | |
89 | void unref(); |
90 | //!< Decrease an object's reference count by one. |
91 | //!< When the reference count becomes zero delete is called. Remember, with ReferencedObject |
92 | //!< you must call unref() on dynmaically allocated objects, not delete. |
93 | }; |
94 | |
95 | /** @} */ |
96 | |
97 | } // namespace scim |
98 | |
99 | #endif //__SCIM_OBJECT_H |
100 | |
101 | /* |
102 | vi:ts=4:nowrap:ai:expandtab |
103 | */ |
104 | |