SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SGReferencedData.cpp
Go to the documentation of this file.
2 #include <shogun/lib/RefCount.h>
3 
4 using namespace shogun;
5 
6 namespace shogun {
7 
8 SGReferencedData::SGReferencedData(bool ref_counting) : m_refcount(NULL)
9 {
10  if (ref_counting)
11  {
12  m_refcount = new RefCount(0);
13  }
14 
15  ref();
16 }
17 
19 {
20  copy_refcount(orig);
21  ref();
22 }
23 
25 {
26  if (this == &orig)
27  return *this;
28 
29  unref();
30  copy_data(orig);
31  copy_refcount(orig);
32  ref();
33  return *this;
34 }
35 
37 {
38  delete m_refcount;
39 }
40 
42 {
43  if (m_refcount == NULL)
44  return -1;
45 
46  int32_t c = m_refcount->ref_count();
47 
48 #ifdef DEBUG_SGVECTOR
49  SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", c, this)
50 #endif
51  return c;
52 }
53 
56 {
57  m_refcount = orig.m_refcount;
58 }
59 
65 {
66  if (m_refcount == NULL)
67  {
68  return -1;
69  }
70 
71  int32_t c = m_refcount->ref();
72 
73 #ifdef DEBUG_SGVECTOR
74  SG_SGCDEBUG("ref() refcount %ld data %p increased\n", c, this)
75 #endif
76  return c;
77 }
78 
85 {
86  if (m_refcount == NULL)
87  {
88  init_data();
89  m_refcount=NULL;
90  return -1;
91  }
92 
93  int32_t c = m_refcount->unref();
94 
95  if (c<=0)
96  {
97 #ifdef DEBUG_SGVECTOR
98  SG_SGCDEBUG("unref() refcount %d data %p destroying\n", c, this)
99 #endif
100  free_data();
101  delete m_refcount;
102  m_refcount=NULL;
103  return 0;
104  }
105  else
106  {
107 #ifdef DEBUG_SGVECTOR
108  SG_SGCDEBUG("unref() refcount %d data %p decreased\n", c, this)
109 #endif
110  init_data();
111  m_refcount=NULL;
112  return c;
113  }
114 }
115 }
virtual void init_data()=0
virtual void copy_data(const SGReferencedData &orig)=0
int32_t ref_count()
Definition: RefCount.cpp:31
SGReferencedData(bool ref_counting=true)
void copy_refcount(const SGReferencedData &orig)
int32_t unref()
Definition: RefCount.cpp:18
virtual void free_data()=0
shogun reference count managed data
int32_t ref()
Definition: RefCount.cpp:5
SGReferencedData & operator=(const SGReferencedData &orig)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SGCDEBUG(...)
Definition: SGIO.h:163

SHOGUN Machine Learning Toolbox - Documentation