aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/engines/direct3d/ref.h
blob: 0853f2e1ad051152da1ca74b6e2a4b3bb81373a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef __REF_H__
#define __REF_H__

//////////////////////////////////////////////////////////////////////////////
// class Referenc
// Desc: Base class enabling reference interface
// Note: Class should derive as virtual
//
class Referenc
{
public:
   Referenc()
      : refs_count(0) {};

   inline int AddRef()
   {
      return ++refs_count;
   }
   inline int RemRef()
   {
      return --refs_count;
   }
   inline int RefCount()
   {
      return refs_count;
   }

private:
   int refs_count;
};


//////////////////////////////////////////////////////////////////////////////
// template Ref
// Desc: Holder in smart-pointers system.
// Important: Only Referenc subclasses may be used as template param.
//

template <class T>
class Ref
{
public:
   // Constructors & destructor
   Ref();
   //Ref(Ref<T> &ref);
   Ref(const Ref<T> &ref);
   Ref(T *ptr);
   Ref(const T *ptr);
   ~Ref();

   Ref<T> &operator =(Ref<T> &ref);
   Ref<T> &operator =(T *ptr);

   inline T *Addr();
   inline T *Addr() const;
   inline int RefCount();
   inline bool IsNull();

   inline T *operator ->();
   inline operator const T *() const;
   inline operator T *();

//private:
   void RemRef();

private:
   T *m_ptr;
};

//////////////////////////////////////////////////////////////////////////////
// Constructors & destructor
template <class T> Ref<T>::Ref()
: m_ptr(NULL)
{
}

//template <class T> Ref<T>::Ref(Ref<T> &ref)
//: m_ptr(NULL)
//{
//   if (ref.Addr() != NULL)
//   {
//      m_ptr = ref.Addr();
//      ((Referenc *)m_ptr)->AddRef();
//   }
//}

template <class T> Ref<T>::Ref(const Ref<T> &ref)
: m_ptr(NULL)
{
   if (ref.Addr() != NULL)
   {
      m_ptr = ref.Addr();
      ((Referenc *)m_ptr)->AddRef();
   }
}

template <class T> Ref<T>::Ref(T *ptr)
: m_ptr(NULL)
{
   if (ptr != NULL)
   {
      m_ptr = ptr;
      ((Referenc *)m_ptr)->AddRef();
   }
}

template <class T> Ref<T>::Ref(const T *ptr)
: m_ptr(NULL)
{
   if (ptr != NULL)
   {
      m_ptr = ptr;
      ((Referenc *)m_ptr)->AddRef();
   }
}

template <class T> Ref<T>::~Ref()
{
   if (m_ptr == NULL)
      return;
   RemRef();
}

// Check pointer on correctness
template <class T> bool Ref<T>::IsNull()
{
   return (m_ptr == NULL);
}

//////////////////////////////////////////////////////////////////////////////
// Operators

template <class T> Ref<T> &Ref<T>::operator =(T *ptr)
{
   if (ptr != NULL)
   {
      if (m_ptr != ptr)
      {
         RemRef();
         m_ptr = ptr;
         ((Referenc *)m_ptr)->AddRef();
      }
   }
   else if (m_ptr != NULL)
      RemRef();
   return *this;
}

template <class T> Ref<T> &Ref<T>::operator =(Ref<T> &ref)
{
   if (ref.Addr() != NULL)
   {
      if (m_ptr != ref.Addr())
      {
         RemRef();
         m_ptr = ref.Addr();
         ((Referenc *)m_ptr)->AddRef();
      }
   }
   else if (m_ptr != NULL)
      RemRef();
   return *this;
}

// Get pointer
template <class T> T *Ref<T>::Addr()
{
   return m_ptr;
}

template <class T> T *Ref<T>::Addr() const
{
   return m_ptr;
}

// Get refs count
template <class T> int Ref<T>::RefCount()
{
   if (m_ptr == NULL)
      return 0;
   return ((Referenc *)m_ptr)->RefCount();
}

// Remove ref to the object and delete it if necessary
// WARNING: arrays cannot be deleted
template <class T> void Ref<T>::RemRef()
{
   if (m_ptr == NULL)
      return;
   if (((Referenc *)m_ptr)->RemRef() == 0)
      delete m_ptr;
   m_ptr = NULL;
}

template <class T> T *Ref<T>::operator ->()
{
   return m_ptr;
}

template <class T> Ref<T>::operator const T *() const
{
   return m_ptr;
}

template <class T> Ref<T>::operator T *()
{
   return m_ptr;
}

#endif  // __REF_H__