00001 /* -*- C++ -*- */ 00002 00003 /* COPYRIGHT 00004 * 00005 * This file is part of the Visual Effects Engine - VEE 00006 * 00007 * Read the "VEE-LICENSE" file for the license. 00008 * 00009 * Authors & Copyright: 00010 * 00011 * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 00012 * Janne Kontkanen 2001-2004 00013 * 00014 * Additional copyrights: Tekes 2003-2004 00015 * 00016 * firstname.lastname@hut.fi 00017 * 00018 */ 00019 00020 #ifndef VEE_REFERENCE_OBJECT_H 00021 #define VEE_REFERENCE_OBJECT_H 00022 00024 template <typename T> 00025 class VEE_ReferenceOjb 00026 { 00027 public: 00028 VEE_ReferenceOjb() : m_count(1) {} 00029 00030 T m_object; 00031 unsigned m_count; 00032 }; 00033 00035 template <typename T> 00036 class VEE_ReferenceObject 00037 { 00038 public: 00039 VEE_ReferenceObject() : m_holder(new VEE_ReferenceOjb<T>) {} 00040 VEE_ReferenceObject(const T &obj) 00041 : m_holder(new VEE_ReferenceOjb<T>) { m_holder->m_object = obj; } 00042 00043 VEE_ReferenceObject(const VEE_ReferenceObject &that) 00044 { 00045 m_holder = ((VEE_ReferenceObject *) & that)->m_holder; 00046 m_holder->m_count++; 00047 } 00048 00049 ~VEE_ReferenceObject() { breakLink(); } 00050 00051 T & ref() { return m_holder->m_object; } 00052 const T & ref() const { return m_holder->m_object; } 00053 00055 void deepCopy(const VEE_ReferenceObject &that) 00056 { 00057 if(m_holder->count > 1) { 00058 --m_holder->m_count; 00059 m_holder = new VEE_ReferenceOjb<T>; 00060 } 00061 m_holder->m_object = that.m_holder->m_object; 00062 } 00063 00064 T & operator *() { return m_holder->m_object; } 00065 const T & operator *() const { return m_holder->m_object; } 00066 00068 VEE_ReferenceObject & operator = (const VEE_ReferenceObject &that) 00069 { 00070 if(m_holder == that.m_holder) return *this; 00071 00072 breakLink(); 00073 m_holder = ((VEE_ReferenceObject *) & that)->m_holder; 00074 m_holder->m_count++; 00075 00076 return *this; 00077 } 00078 00079 protected: 00080 00081 void breakLink() 00082 { if(! --m_holder->m_count) delete m_holder; } 00083 00084 VEE_ReferenceOjb<T> *m_holder; 00085 }; 00086 00087 #endif 00088