src/misc/countedptr.hpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:13:39 +0200
changeset 10440 0a91ea45b0e8
parent 8348 4d7c1c5055b3
permissions -rw-r--r--
adjust the random land gen a bit to work with mini-maps
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
8348
4d7c1c5055b3 (svn r11914) -Documentation: fix some @file statement
glx
parents: 7296
diff changeset
     3
/** @file countedptr.hpp CCountedPtr - smart pointer implementation. */
6481
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
/** 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
     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
 *     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
    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
 *     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
    13
 *   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
    14
 *
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
 *     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
    16
 *   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
    17
 */
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
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
    19
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
    20
	/** 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
    21
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
    22
	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
    23
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
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
    25
	/** 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
    26
	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
    27
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
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
    29
	/** 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
    30
	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
    31
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
	/** 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
    33
	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
    34
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
	/** 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
    36
	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
    37
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
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
    39
	/** 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
    40
	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
    41
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
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
    43
	/** 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
    44
	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
    45
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
    46
	/** 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
    47
	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
    48
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
	/** 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
    50
	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
    51
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
	/** 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
    53
	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
    54
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
	/** 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
    56
	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
    57
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
    58
	/** 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
    59
	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
    60
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
	/** 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
    62
	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
    63
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
	/** assignment operator from another smart ptr */
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    65
	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
    66
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
    67
	/** 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
    68
	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
    69
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
	/** 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
    71
	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
    72
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
	/** 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
    74
	//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
    75
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
    76
	/** 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
    77
	//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
    78
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
    79
	/** 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
    80
	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
    81
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
	/** 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
    83
	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
    84
};
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
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
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
    87
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
    88
{
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
	// 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
    90
	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
    91
		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
    92
		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
    93
		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
    94
		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
    95
	}
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
}
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
7296
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    98
/**
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
    99
 * 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
   100
 * 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
   101
 * 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
   102
 */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   103
template <class T> struct AdaptT {
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   104
	T m_t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   105
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   106
	/** construct by wrapping the given object */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   107
	AdaptT(const T &t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   108
		: m_t(t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   109
	{}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   110
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   111
	/** assignment operator */
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   112
	T& operator = (const T &t)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   113
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   114
		m_t = t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   115
		return t;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   116
	}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   117
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   118
	/** 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
   119
	operator T& ()
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   120
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   121
		return m_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
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   124
	/** 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
   125
	operator const T& () const
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   126
	{
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   127
		return m_t;
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
};
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
/** 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
   133
 *  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
   134
 *  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
   135
 *  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
   136
 *  not using CCountedPtr<T>.
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   137
 *
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   138
 *  @see misc/countedobj.cpp for implementation.
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
struct SimpleCountedObject {
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   141
	int32 m_ref_cnt;
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   142
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   143
	SimpleCountedObject()
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   144
		: m_ref_cnt(0)
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   145
	{}
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   146
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   147
	virtual ~SimpleCountedObject()
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
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   150
	virtual int32 AddRef();
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   151
	virtual int32 Release();
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   152
	virtual void FinalRelease() {};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   153
};
84069e62f29d (svn r10644) -Merge (from NoAI): framework for reference counted objects (pointers).
rubidium
parents: 6481
diff changeset
   154
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
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
   158
#endif /* COUNTEDPTR_HPP */