EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RefPtr.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24  Update: Allan
25 */
26 
27 #ifndef __UTILS__REF_PTR_H__INCL__
28 #define __UTILS__REF_PTR_H__INCL__
29 
30 
31 #include <cassert>
32 #include <cstdio>
33 #include <iostream>
34 
35 #include "utils/misc.h"
36 
48 class RefObject
49 {
50  template<typename X>
51  friend class RefPtr;
52 
53 public:
59  RefObject( size_t initRefCount )
60  : mRefCount( initRefCount )
61  {
62  mDeleted = false;
63  }
64 
71  virtual ~RefObject()
72  {
73  // this isnt completely accurate yet. disable to avoid crashes i cant trace
74  //assert( mRefCount == 0);
75  mDeleted = true;
76  }
77 
78  size_t GetCount() { return mRefCount; }
79 
80 protected:
84  void IncRef() const
85  {
86  if (mDeleted) {
87  _log(REFPTR__ERROR, "IncRef() - mDeleted = true. Count is %u", mRefCount);
89  return;
90  }
91  assert( mDeleted == false );
92  ++mRefCount;
93  //assert( mRefCount > 0 ); // RefPtr objects are created with a ref count of 0, then incremented during RefPtr c'tor
94  //_log(REFPTR__INC, "IncRef() is %u.", mRefCount);
95  }
101  void DecRef() const
102  {
103  if (mDeleted) {
104  _log(REFPTR__ERROR, "DecRef() - mDeleted = true. Count is %u", mRefCount);
105  EvE::traceStack();
106  return;
107  }
108 
109  assert( mDeleted == false );
110  assert( mRefCount > 0 );
111  --mRefCount;
112  //_log(REFPTR__DEC, "DecRef() is %u.", mRefCount);
113 
114  if (mRefCount < 1)
115  delete this;
116  }
117 
119  mutable size_t mRefCount;
120  mutable bool mDeleted;
121 };
122 
132 template<typename X>
133 class RefPtr
134 {
135 public:
141  explicit RefPtr( X* p = nullptr)
142  : mPtr( p )
143  {
144  if (*this)
145  (*this)->IncRef();
146  }
152  RefPtr( const RefPtr& oth )
153  : mPtr( oth.get() )
154  {
155  if (*this)
156  (*this)->IncRef();
157  }
163  template<typename Y>
164  RefPtr( const RefPtr<Y>& oth )
165  : mPtr( oth.get() )
166  {
167  if (*this)
168  (*this)->IncRef();
169  }
170 
174  virtual ~RefPtr()
175  {
176  if (*this)
177  (*this)->DecRef();
178  }
179 
185  RefPtr& operator=( const RefPtr& oth )
186  {
187  if (*this)
188  (*this)->DecRef();
189  mPtr = oth.get();
190  if (*this)
191  (*this)->IncRef();
192  return *this;
193  }
199  template<typename Y>
200  RefPtr& operator=( const RefPtr<Y>& oth )
201  {
202  if (*this)
203  (*this)->DecRef();
204  mPtr = oth.get();
205  if (*this)
206  (*this)->IncRef();
207  return *this;
208  }
209 
213  X* get() const { return mPtr; }
214 
218  operator bool() const { return ( mPtr != nullptr ); }
219 
220  X& operator*() const { assert( *this ); return *mPtr; }
221  X* operator->() const { assert( *this ); return mPtr; }
222 
228  template<typename Y>
229  bool operator==( const RefPtr<Y>& oth ) const
230  {
231  return ( mPtr == oth.get() );
232  }
233 
237  template<typename Y>
238  static RefPtr StaticCast( const RefPtr<Y>& oth )
239  {
240  return RefPtr( static_cast<X*>( oth.get() ) );
241  }
242 
243 protected:
245  X* mPtr;
246 };
247 
248 #endif /* !__UTILS__REF_PTR_H__INCL__ */
RefPtr & operator=(const RefPtr< Y > &oth)
Casting copy operator.
Definition: RefPtr.h:200
#define _log(type, fmt,...)
Definition: logsys.h:124
void DecRef() const
Decrements reference count of object by one.
Definition: RefPtr.h:101
size_t GetCount()
Definition: RefPtr.h:78
RefPtr & operator=(const RefPtr &oth)
Copy operator.
Definition: RefPtr.h:185
virtual ~RefPtr()
Destructor, releases reference.
Definition: RefPtr.h:174
bool operator==(const RefPtr< Y > &oth) const
Compares two references.
Definition: RefPtr.h:229
A reference-counted object.
Definition: RefPtr.h:48
virtual ~RefObject()
Destructor; must be virtual.
Definition: RefPtr.h:71
void IncRef() const
Increments reference count of object by one.
Definition: RefPtr.h:84
X & operator*() const
Definition: RefPtr.h:220
RefPtr(X *p=nullptr)
Primary constructor.
Definition: RefPtr.h:141
X * operator->() const
Definition: RefPtr.h:221
X * get() const
Definition: RefPtr.h:213
bool mDeleted
Definition: RefPtr.h:120
RefPtr(const RefPtr< Y > &oth)
Casting copy constructor.
Definition: RefPtr.h:164
size_t mRefCount
Reference count of instance.
Definition: RefPtr.h:119
static RefPtr StaticCast(const RefPtr< Y > &oth)
Acts as static_cast from one RefPtr to another.
Definition: RefPtr.h:238
RefObject(size_t initRefCount)
Initializes reference count.
Definition: RefPtr.h:59
void traceStack(void)
Definition: misc.cpp:169
RefPtr(const RefPtr &oth)
Copy constructor.
Definition: RefPtr.h:152
X * mPtr
The pointer to the reference-counted object.
Definition: RefPtr.h:245
Reference-counting-based smart pointer.
Definition: RefPtr.h:133