src/misc/countedptr.hpp
author rubidium
Tue, 22 Jan 2008 21:00:30 +0000
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6732 ca1b466db422
permissions -rw-r--r--
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6732
diff changeset
     3
/** @file countedptr.hpp CCountedPtr - smart pointer implementation. */
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 5884
diff changeset
     4
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
#ifndef COUNTEDPTR_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
#define COUNTEDPTR_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     8
/** CCountedPtr - simple reference counting smart pointer.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
     9
 *
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    10
 *     One of the standard ways how to maintain object's lifetime.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    11
 *
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    12
 *     See http://ootips.org/yonat/4dev/smart-pointers.html for more
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    13
 *   general info about smart pointers.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    14
 *
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    15
 *     This class implements ref-counted pointer for objects/interfaces that
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    16
 *   support AddRef() and Release() methods.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4434
diff changeset
    17
 */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
template <class Tcls_>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
class CCountedPtr {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
	/** redefine the template argument to make it visible for derived classes */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    21
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    22
	typedef Tcls_ Tcls;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    23
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    24
protected:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
	/** here we hold our pointer to the target */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
	Tcls* m_pT;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    27
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    28
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    29
	/** default (NULL) construct or construct from a raw pointer */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    30
	FORCEINLINE CCountedPtr(Tcls* pObj = NULL) : m_pT(pObj) {AddRef();};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    31
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
	/** copy constructor (invoked also when initializing from another smart ptr) */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
	FORCEINLINE CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
	/** destructor releasing the reference */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    36
	FORCEINLINE ~CCountedPtr() {Release();};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    37
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    38
protected:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
	/** add one ref to the underlaying object */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
	FORCEINLINE void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    41
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    42
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    43
	/** release smart pointer (and decrement ref count) if not null */
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    44
	FORCEINLINE void Release() {if (m_pT != NULL) {Tcls* pT = m_pT; m_pT = NULL; pT->Release();}}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    45
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
	/** dereference of smart pointer - const way */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
	FORCEINLINE const Tcls* operator -> () const {assert(m_pT != NULL); return m_pT;};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
	/** dereference of smart pointer - non const way */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
	FORCEINLINE Tcls* operator -> () {assert(m_pT != NULL); return m_pT;};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    51
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
	/** raw pointer casting operator - const way */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    53
	FORCEINLINE operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
	/** raw pointer casting operator - non-const way */
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    56
	FORCEINLINE operator Tcls*() {return m_pT;}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    57
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    58
	/** operator & to support output arguments */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    59
	FORCEINLINE Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
	/** assignment operator from raw ptr */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
	FORCEINLINE CCountedPtr& operator = (Tcls* pT) {Assign(pT); return *this;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    63
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
	/** assignment operator from another smart ptr */
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    65
	FORCEINLINE CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    66
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    67
	/** assignment operator helper */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
	FORCEINLINE void Assign(Tcls* pT);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
	/** one way how to test for NULL value */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
	FORCEINLINE bool IsNull() const {return m_pT == NULL;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
	/** another way how to test for NULL value */
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    74
	//FORCEINLINE bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
	/** yet another way how to test for NULL value */
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    77
	//FORCEINLINE bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
	/** assign pointer w/o incrementing ref count */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
	FORCEINLINE void Attach(Tcls* pT) {Release(); m_pT = pT;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    81
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
	/** detach pointer w/o decrementing ref count */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
	FORCEINLINE Tcls* Detach() {Tcls* pT = m_pT; m_pT = NULL; return pT;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    85
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
template <class Tcls_>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    87
FORCEINLINE void CCountedPtr<Tcls_>::Assign(Tcls* pT)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    88
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    89
	// if they are the same, we do nothing
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    90
	if (pT != m_pT) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    91
		if (pT) pT->AddRef();        // AddRef new pointer if any
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    92
		Tcls* pTold = m_pT;          // save original ptr
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    93
		m_pT = pT;                   // update m_pT to new value
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    94
		if (pTold) pTold->Release(); // release old ptr if any
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    95
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    97
6732
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    98
/**
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
    99
 * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   100
 * collections as item type. For example CCountedPtr has overloaded operator & which
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   101
 * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   102
 */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   103
template <class T> struct AdaptT {
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   104
	T m_t;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   105
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   106
	/** construct by wrapping the given object */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   107
	AdaptT(const T &t)
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   108
		: m_t(t)
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   109
	{}
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   110
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   111
	/** assignment operator */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   112
	T& operator = (const T &t)
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   113
	{
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   114
		m_t = t;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   115
		return t;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   116
	}
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   117
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   118
	/** type-cast operator (used when AdaptT is used instead of T) */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   119
	operator T& ()
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   120
	{
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   121
		return m_t;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   122
	}
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   123
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   124
	/** const type-cast operator (used when AdaptT is used instead of const T) */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   125
	operator const T& () const
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   126
	{
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   127
		return m_t;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   128
	}
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   129
};
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   130
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   131
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   132
/** Simple counted object. Use it as base of your struct/class if you want to use
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   133
 *  basic reference counting. Your struct/class will destroy and free itself when
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   134
 *  last reference to it is released (using Relese() method). The initial reference
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   135
 *  count (when it is created) is zero (don't forget AddRef() at least one time if
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   136
 *  not using CCountedPtr<T>.
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   137
 *
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   138
 *  @see misc/countedobj.cpp for implementation.
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   139
 */
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   140
struct SimpleCountedObject {
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   141
	int32 m_ref_cnt;
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   142
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   143
	SimpleCountedObject()
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   144
		: m_ref_cnt(0)
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   145
	{}
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   146
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   147
	virtual ~SimpleCountedObject()
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   148
	{};
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   149
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   150
	virtual int32 AddRef();
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   151
	virtual int32 Release();
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   152
	virtual void FinalRelease() {};
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   153
};
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   154
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   155
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   156
ca1b466db422 (svn r10653) [NewGRF_ports] -Sync: with trunk r10602-10651
richk
parents: 6719
diff changeset
   157
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   158
#endif /* COUNTEDPTR_HPP */