SpectMorph

lib/smobject.hh

00001 /*
00002  * Copyright (C) 2011 Stefan Westerfeld
00003  *
00004  * This library is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published by the
00006  * Free Software Foundation; either version 3 of the License, or (at your
00007  * option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
00012  * for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef SPECTMORPH_OBJECT_HH
00019 #define SPECTMORPH_OBJECT_HH
00020 
00021 #include <birnet/birnet.hh>
00022 
00023 namespace SpectMorph
00024 {
00025 
00026 class Object
00027 {
00028   Birnet::Mutex object_mutex;
00029   unsigned int  object_ref_count;
00030 
00031 public:
00032   Object();
00033   virtual ~Object();
00034 
00035   void ref();
00036   void unref();
00037 };
00038 
00039 template<class T>
00040 class RefPtr
00041 {
00042   T *ptr;
00043 
00044 public:
00045   RefPtr (T* t = NULL)
00046   {
00047     ptr = t;
00048   }
00049   RefPtr (const RefPtr& other)
00050   {
00051     T *new_ptr = other.ptr;
00052 
00053     if (new_ptr)
00054       new_ptr->ref();
00055 
00056     ptr = new_ptr;
00057   }
00058   RefPtr&
00059   operator= (const RefPtr& other)
00060   {
00061     T *new_ptr = other.ptr;
00062     T *old_ptr = ptr;
00063 
00064     if (new_ptr)
00065       new_ptr->ref();
00066 
00067     ptr = new_ptr;
00068 
00069     if (old_ptr)
00070       old_ptr->unref();
00071 
00072     return *this;
00073   }
00074   T*
00075   operator->()
00076   {
00077     return ptr;
00078   }
00079   T*
00080   c_ptr()
00081   {
00082     return ptr;
00083   }
00084   ~RefPtr()
00085   {
00086     if (ptr)
00087       ptr->unref();
00088   }
00089   operator bool() const
00090   {
00091     return (ptr != 0);
00092   }
00093 };
00094 
00095 }
00096 
00097 #endif
 All Classes Functions Variables Enumerations Enumerator