00001 /** @file scim_object.h 00002 * @brief Reference counted base class interface. 00003 * 00004 * Provides a reference counted base class 00005 * for dynamic objects handled the scim smart pointer. 00006 * 00007 * Most code of this file are came from Inti project. 00008 */ 00009 00010 /* 00011 * Smart Common Input Method 00012 * 00013 * Copyright (c) 2004 James Su <suzhe@turbolinux.com.cn> 00014 * Copyright (c) 2003 James Su <suzhe@turbolinux.com.cn> 00015 * Copyright (c) 2002 James Su <suzhe@turbolinux.com.cn> 00016 * Copyright (c) 2002 The Inti Development Team. 00017 * 00018 * 00019 * This library is free software; you can redistribute it and/or 00020 * modify it under the terms of the GNU Lesser General Public 00021 * License as published by the Free Software Foundation; either 00022 * version 2 of the License, or (at your option) any later version. 00023 * 00024 * This library is distributed in the hope that it will be useful, 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00027 * GNU Lesser General Public License for more details. 00028 * 00029 * You should have received a copy of the GNU Lesser General Public 00030 * License along with this program; if not, write to the 00031 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 00032 * Boston, MA 02111-1307 USA 00033 * 00034 * $Id: scim_object.h,v 1.6 2004/02/06 07:53:15 suzhe Exp $ 00035 */ 00036 00037 #ifndef __SCIM_OBJECT_H 00038 #define __SCIM_OBJECT_H 00039 00040 namespace scim { 00041 00042 /** 00043 * @addtogroup Helper 00044 * @{ 00045 */ 00046 00047 /** 00048 * @class ReferencedObject 00049 * @brief Reference counted base class. 00050 * 00051 * ReferencedObject is a reference counting base class. 00052 * it has an integer reference counter so that dynamic objects 00053 * can have their memory allocation handled by the scim 00054 * smart pointer: Pointer<>. This keeps the memory management 00055 * in scim consistent across all classes. 00056 * If you derive a class from ReferencedObject and allocate it 00057 * on the heap, you free the memory and destroy the object by 00058 * calling unref(), not delete. 00059 */ 00060 class ReferencedObject 00061 { 00062 template<typename T> friend class Pointer; 00063 00064 ReferencedObject(const ReferencedObject&); 00065 ReferencedObject& operator=(const ReferencedObject&); 00066 00067 bool m_referenced; 00068 int m_ref_count; 00069 00070 protected: 00071 ReferencedObject(); 00072 //!< Constructor. 00073 00074 virtual ~ReferencedObject() = 0; 00075 //!< Destructor. 00076 00077 void set_referenced(bool reference); 00078 //!< Set the internal referenced flag. 00079 //!< @param reference - <EM>true</EM> if the initial reference count must be removed by owner. 00080 //!< 00081 //!< <BR>Called by derived classes to set the referenced flag. A object sets this flag 00082 //!< to true , indicating that it owns the initial reference count and unref() must be called. 00083 public: 00084 bool is_referenced() const; 00085 //!< The referenced flag setting. 00086 //!< @return <EM>true</EM> if unref() must be explicitly called on this object. 00087 00088 void ref(); 00089 //!< Increase an object's reference count by one. 00090 00091 void unref(); 00092 //!< Decrease an object's reference count by one. 00093 //!< When the reference count becomes zero delete is called. Remember, with ReferencedObject 00094 //!< you must call unref() on dynmaically allocated objects, not delete. 00095 }; 00096 00097 /** @} */ 00098 00099 } // namespace scim 00100 00101 #endif //__SCIM_OBJECT_H 00102 00103 /* 00104 vi:ts=4:nowrap:ai:expandtab 00105 */