SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGReferencedData.cpp
Go to the documentation of this file.
1 #include <shogun/lib/common.h>
3 #include <shogun/lib/RefCount.h>
4 #include <shogun/io/SGIO.h>
5 
6 
7 using namespace shogun;
8 
9 namespace shogun {
10 
11 SGReferencedData::SGReferencedData(bool ref_counting) : m_refcount(NULL)
12 {
13  if (ref_counting)
14  {
15  m_refcount = new RefCount(0);
16  }
17 
18  ref();
19 }
20 
22 {
23  copy_refcount(orig);
24  ref();
25 }
26 
28 {
29  if (this == &orig)
30  return *this;
31 
32  unref();
33  copy_data(orig);
34  copy_refcount(orig);
35  ref();
36  return *this;
37 }
38 
40 {
41  delete m_refcount;
42 }
43 
45 {
46  if (m_refcount == NULL)
47  return -1;
48 
49  int32_t c = m_refcount->ref_count();
50 
51 #ifdef DEBUG_SGVECTOR
52  SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", c, this)
53 #endif
54  return c;
55 }
56 
59 {
60  m_refcount = orig.m_refcount;
61 }
62 
68 {
69  if (m_refcount == NULL)
70  {
71  return -1;
72  }
73 
74  int32_t c = m_refcount->ref();
75 
76 #ifdef DEBUG_SGVECTOR
77  SG_SGCDEBUG("ref() refcount %ld data %p increased\n", c, this)
78 #endif
79  return c;
80 }
81 
88 {
89  if (m_refcount == NULL)
90  {
91  init_data();
92  m_refcount=NULL;
93  return -1;
94  }
95 
96  int32_t c = m_refcount->unref();
97 
98  if (c<=0)
99  {
100 #ifdef DEBUG_SGVECTOR
101  SG_SGCDEBUG("unref() refcount %d data %p destroying\n", c, this)
102 #endif
103  free_data();
104  delete m_refcount;
105  m_refcount=NULL;
106  return 0;
107  }
108  else
109  {
110 #ifdef DEBUG_SGVECTOR
111  SG_SGCDEBUG("unref() refcount %d data %p decreased\n", c, this)
112 #endif
113  init_data();
114  m_refcount=NULL;
115  return c;
116  }
117 }
118 }

SHOGUN Machine Learning Toolbox - Documentation