SHOGUN
v3.2.0
|
00001 #include <shogun/lib/common.h> 00002 #include <shogun/lib/SGReferencedData.h> 00003 #include <shogun/lib/RefCount.h> 00004 #include <shogun/io/SGIO.h> 00005 00006 00007 using namespace shogun; 00008 00009 namespace shogun { 00010 00011 SGReferencedData::SGReferencedData(bool ref_counting) : m_refcount(NULL) 00012 { 00013 if (ref_counting) 00014 { 00015 m_refcount = new RefCount(0); 00016 } 00017 00018 ref(); 00019 } 00020 00021 SGReferencedData::SGReferencedData(const SGReferencedData &orig) 00022 { 00023 copy_refcount(orig); 00024 ref(); 00025 } 00026 00027 SGReferencedData& SGReferencedData::operator= (const SGReferencedData &orig) 00028 { 00029 if (this == &orig) 00030 return *this; 00031 00032 unref(); 00033 copy_data(orig); 00034 copy_refcount(orig); 00035 ref(); 00036 return *this; 00037 } 00038 00039 SGReferencedData::~SGReferencedData() 00040 { 00041 delete m_refcount; 00042 } 00043 00044 int32_t SGReferencedData::ref_count() 00045 { 00046 if (m_refcount == NULL) 00047 return -1; 00048 00049 int32_t c = m_refcount->ref_count(); 00050 00051 #ifdef DEBUG_SGVECTOR 00052 SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", c, this) 00053 #endif 00054 return c; 00055 } 00056 00058 void SGReferencedData::copy_refcount(const SGReferencedData &orig) 00059 { 00060 m_refcount = orig.m_refcount; 00061 } 00062 00067 int32_t SGReferencedData::ref() 00068 { 00069 if (m_refcount == NULL) 00070 { 00071 return -1; 00072 } 00073 00074 int32_t c = m_refcount->ref(); 00075 00076 #ifdef DEBUG_SGVECTOR 00077 SG_SGCDEBUG("ref() refcount %ld data %p increased\n", c, this) 00078 #endif 00079 return c; 00080 } 00081 00087 int32_t SGReferencedData::unref() 00088 { 00089 if (m_refcount == NULL) 00090 { 00091 init_data(); 00092 m_refcount=NULL; 00093 return -1; 00094 } 00095 00096 int32_t c = m_refcount->unref(); 00097 00098 if (c<=0) 00099 { 00100 #ifdef DEBUG_SGVECTOR 00101 SG_SGCDEBUG("unref() refcount %d data %p destroying\n", c, this) 00102 #endif 00103 free_data(); 00104 delete m_refcount; 00105 m_refcount=NULL; 00106 return 0; 00107 } 00108 else 00109 { 00110 #ifdef DEBUG_SGVECTOR 00111 SG_SGCDEBUG("unref() refcount %d data %p decreased\n", c, this) 00112 #endif 00113 init_data(); 00114 m_refcount=NULL; 00115 return c; 00116 } 00117 } 00118 }