src/misc/countedptr.hpp
author maedhros
Mon, 06 Aug 2007 12:54:03 +0000
changeset 7417 c0ddfb49c50b
parent 7296 84069e62f29d
child 8348 4d7c1c5055b3
permissions -rw-r--r--
(svn r10810) -Fix (r10097): Add a comma between different cargo types if an articulated vehicle carries more than one cargo.
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     1
/* $Id$ */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     2
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 5633
diff changeset
     3
/** @file countedptr.hpp */
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 5633
diff changeset
     4
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     5
#ifndef COUNTEDPTR_HPP
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     6
#define COUNTEDPTR_HPP
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     7
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     8
/** @file CCountedPtr - smart pointer implementation */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
     9
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    10
/** CCountedPtr - simple reference counting smart pointer.
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    11
 *
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    12
 *     One of the standard ways how to maintain object's lifetime.
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    13
 *
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    14
 *     See http://ootips.org/yonat/4dev/smart-pointers.html for more
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    15
 *   general info about smart pointers.
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    16
 *
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    17
 *     This class implements ref-counted pointer for objects/interfaces that
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    18
 *   support AddRef() and Release() methods.
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    19
 */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    20
template <class Tcls_>
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    21
class CCountedPtr {
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    22
	/** redefine the template argument to make it visible for derived classes */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    23
public:
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    24
	typedef Tcls_ Tcls;
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    25
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    26
protected:
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    27
	/** here we hold our pointer to the target */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    28
	Tcls* m_pT;
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    29
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    30
public:
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    31
	/** default (NULL) construct or construct from a raw pointer */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    32
	FORCEINLINE CCountedPtr(Tcls* pObj = NULL) : m_pT(pObj) {AddRef();};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    33
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    34
	/** copy constructor (invoked also when initializing from another smart ptr) */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    35
	FORCEINLINE CCountedPtr(const CCountedPtr& src) : m_pT(src.m_pT) {AddRef();};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    36
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    37
	/** destructor releasing the reference */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    38
	FORCEINLINE ~CCountedPtr() {Release();};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    39
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    40
protected:
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    41
	/** add one ref to the underlaying object */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    42
	FORCEINLINE void AddRef() {if (m_pT != NULL) m_pT->AddRef();}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    43
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    44
public:
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    45
	/** release smart pointer (and decrement ref count) if not null */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    46
	FORCEINLINE void Release() {if (m_pT != NULL) {Tcls* pT = m_pT; m_pT = NULL; pT->Release();}}
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    47
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    48
	/** dereference of smart pointer - const way */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    49
	FORCEINLINE const Tcls* operator -> () const {assert(m_pT != NULL); return m_pT;};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    50
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    51
	/** dereference of smart pointer - non const way */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    52
	FORCEINLINE Tcls* operator -> () {assert(m_pT != NULL); return m_pT;};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    53
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    54
	/** raw pointer casting operator - const way */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    55
	FORCEINLINE operator const Tcls*() const {assert(m_pT == NULL); return m_pT;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    56
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    57
	/** raw pointer casting operator - non-const way */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    58
	FORCEINLINE operator Tcls*() {return m_pT;}
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    59
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    60
	/** operator & to support output arguments */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    61
	FORCEINLINE Tcls** operator &() {assert(m_pT == NULL); return &m_pT;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    62
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    63
	/** assignment operator from raw ptr */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    64
	FORCEINLINE CCountedPtr& operator = (Tcls* pT) {Assign(pT); return *this;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    65
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    66
	/** assignment operator from another smart ptr */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    67
	FORCEINLINE CCountedPtr& operator = (const CCountedPtr& src) {Assign(src.m_pT); return *this;}
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    68
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    69
	/** assignment operator helper */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    70
	FORCEINLINE void Assign(Tcls* pT);
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    71
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    72
	/** one way how to test for NULL value */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    73
	FORCEINLINE bool IsNull() const {return m_pT == NULL;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    74
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    75
	/** another way how to test for NULL value */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    76
	//FORCEINLINE bool operator == (const CCountedPtr& sp) const {return m_pT == sp.m_pT;}
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    77
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    78
	/** yet another way how to test for NULL value */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    79
	//FORCEINLINE bool operator != (const CCountedPtr& sp) const {return m_pT != sp.m_pT;}
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    80
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    81
	/** assign pointer w/o incrementing ref count */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    82
	FORCEINLINE void Attach(Tcls* pT) {Release(); m_pT = pT;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    83
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    84
	/** detach pointer w/o decrementing ref count */
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    85
	FORCEINLINE Tcls* Detach() {Tcls* pT = m_pT; m_pT = NULL; return pT;}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    86
};
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    87
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    88
template <class Tcls_>
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    89
FORCEINLINE void CCountedPtr<Tcls_>::Assign(Tcls* pT)
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    90
{
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    91
	// if they are the same, we do nothing
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    92
	if (pT != m_pT) {
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    93
		if (pT) pT->AddRef();        // AddRef new pointer if any
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    94
		Tcls* pTold = m_pT;          // save original ptr
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    95
		m_pT = pT;                   // update m_pT to new value
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    96
		if (pTold) pTold->Release(); // release old ptr if any
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    97
	}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    98
}
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
    99
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   100
/**
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   101
 * Adapter wrapper for CCountedPtr like classes that can't be used directly by stl
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   102
 * collections as item type. For example CCountedPtr has overloaded operator & which
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   103
 * prevents using CCountedPtr in stl collections (i.e. std::list<CCountedPtr<MyType> >)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   104
 */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   105
template <class T> struct AdaptT {
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   106
	T m_t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   107
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   108
	/** construct by wrapping the given object */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   109
	AdaptT(const T &t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   110
		: m_t(t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   111
	{}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   112
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   113
	/** assignment operator */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   114
	T& operator = (const T &t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   115
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   116
		m_t = t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   117
		return t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   118
	}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   119
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   120
	/** type-cast operator (used when AdaptT is used instead of T) */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   121
	operator T& ()
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   122
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   123
		return m_t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   124
	}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   125
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   126
	/** const type-cast operator (used when AdaptT is used instead of const T) */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   127
	operator const T& () const
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   128
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   129
		return m_t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   130
	}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   131
};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   132
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   133
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   134
/** Simple counted object. Use it as base of your struct/class if you want to use
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   135
 *  basic reference counting. Your struct/class will destroy and free itself when
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   136
 *  last reference to it is released (using Relese() method). The initial reference
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   137
 *  count (when it is created) is zero (don't forget AddRef() at least one time if
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   138
 *  not using CCountedPtr<T>.
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   139
 *
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   140
 *  @see misc/countedobj.cpp for implementation.
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   141
 */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   142
struct SimpleCountedObject {
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   143
	int32 m_ref_cnt;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   144
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   145
	SimpleCountedObject()
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   146
		: m_ref_cnt(0)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   147
	{}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   148
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   149
	virtual ~SimpleCountedObject()
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   150
	{};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   151
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   152
	virtual int32 AddRef();
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   153
	virtual int32 Release();
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   154
	virtual void FinalRelease() {};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   155
};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   156
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   157
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   158
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   159
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents:
diff changeset
   160
#endif /* COUNTEDPTR_HPP */