| 1 |
tim |
770 |
#ifndef INC_ASTRefCount_hpp__ |
| 2 |
|
|
# define INC_ASTRefCount_hpp__ |
| 3 |
|
|
|
| 4 |
|
|
/* ANTLR Translator Generator |
| 5 |
|
|
* Project led by Terence Parr at http://www.jGuru.com |
| 6 |
|
|
* Software rights: http://www.antlr.org/license.html |
| 7 |
|
|
* |
| 8 |
|
|
* $Id: ASTRefCount.hpp,v 1.1 2005-12-02 15:38:02 tim Exp $ |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
# include <antlr/config.hpp> |
| 12 |
|
|
|
| 13 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
| 14 |
|
|
namespace antlr { |
| 15 |
|
|
#endif |
| 16 |
|
|
|
| 17 |
|
|
class AST; |
| 18 |
|
|
|
| 19 |
|
|
struct ANTLR_API ASTRef |
| 20 |
|
|
{ |
| 21 |
|
|
AST* const ptr; |
| 22 |
|
|
unsigned int count; |
| 23 |
|
|
|
| 24 |
|
|
ASTRef(AST* p); |
| 25 |
|
|
~ASTRef(); |
| 26 |
|
|
ASTRef* increment() |
| 27 |
|
|
{ |
| 28 |
|
|
++count; |
| 29 |
|
|
return this; |
| 30 |
|
|
} |
| 31 |
|
|
bool decrement() |
| 32 |
|
|
{ |
| 33 |
|
|
return (--count==0); |
| 34 |
|
|
} |
| 35 |
|
|
|
| 36 |
|
|
static ASTRef* getRef(const AST* p); |
| 37 |
|
|
private: |
| 38 |
|
|
ASTRef( const ASTRef& ); |
| 39 |
|
|
ASTRef& operator=( const ASTRef& ); |
| 40 |
|
|
}; |
| 41 |
|
|
|
| 42 |
|
|
template<class T> |
| 43 |
|
|
class ANTLR_API ASTRefCount |
| 44 |
|
|
{ |
| 45 |
|
|
private: |
| 46 |
|
|
ASTRef* ref; |
| 47 |
|
|
|
| 48 |
|
|
public: |
| 49 |
|
|
ASTRefCount(const AST* p=0) |
| 50 |
|
|
: ref(p ? ASTRef::getRef(p) : 0) |
| 51 |
|
|
{ |
| 52 |
|
|
} |
| 53 |
|
|
ASTRefCount(const ASTRefCount<T>& other) |
| 54 |
|
|
: ref(other.ref ? other.ref->increment() : 0) |
| 55 |
|
|
{ |
| 56 |
|
|
} |
| 57 |
|
|
~ASTRefCount() |
| 58 |
|
|
{ |
| 59 |
|
|
if (ref && ref->decrement()) |
| 60 |
|
|
delete ref; |
| 61 |
|
|
} |
| 62 |
|
|
ASTRefCount<T>& operator=(AST* other) |
| 63 |
|
|
{ |
| 64 |
|
|
ASTRef* tmp = ASTRef::getRef(other); |
| 65 |
|
|
|
| 66 |
|
|
if (ref && ref->decrement()) |
| 67 |
|
|
delete ref; |
| 68 |
|
|
|
| 69 |
|
|
ref=tmp; |
| 70 |
|
|
|
| 71 |
|
|
return *this; |
| 72 |
|
|
} |
| 73 |
|
|
ASTRefCount<T>& operator=(const ASTRefCount<T>& other) |
| 74 |
|
|
{ |
| 75 |
|
|
if( other.ref != ref ) |
| 76 |
|
|
{ |
| 77 |
|
|
ASTRef* tmp = other.ref ? other.ref->increment() : 0; |
| 78 |
|
|
|
| 79 |
|
|
if (ref && ref->decrement()) |
| 80 |
|
|
delete ref; |
| 81 |
|
|
|
| 82 |
|
|
ref=tmp; |
| 83 |
|
|
} |
| 84 |
|
|
return *this; |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
operator T* () const { return ref ? static_cast<T*>(ref->ptr) : 0; } |
| 88 |
|
|
T* operator->() const { return ref ? static_cast<T*>(ref->ptr) : 0; } |
| 89 |
|
|
T* get() const { return ref ? static_cast<T*>(ref->ptr) : 0; } |
| 90 |
|
|
}; |
| 91 |
|
|
|
| 92 |
|
|
typedef ASTRefCount<AST> RefAST; |
| 93 |
|
|
|
| 94 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
| 95 |
|
|
} |
| 96 |
|
|
#endif |
| 97 |
|
|
|
| 98 |
|
|
#endif //INC_ASTRefCount_hpp__ |