ANTLR Support Libraries 2.7.1+
TokenRefCount.hpp
Go to the documentation of this file.
1 #ifndef INC_TokenRefCount_hpp__
2 # define INC_TokenRefCount_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:$
9  */
10 
11 # include <antlr/config.hpp>
12 
13 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
14 namespace antlr {
15 #endif
16 
17 class Token;
18 
20 {
21  Token* const ptr;
22  unsigned int count;
23 
24  TokenRef(Token* p);
25  ~TokenRef();
27  {
28  ++count;
29  return this;
30  }
31  bool decrement()
32  {
33  return (--count==0);
34  }
35 
36  static TokenRef* getRef(const Token* p);
37 private:
38  TokenRef( const TokenRef& );
39  TokenRef& operator=( const TokenRef& );
40 };
41 
42 template<class T>
44 {
45 private:
47 
48 public:
49  TokenRefCount(const Token* p=0)
50  : ref(p ? TokenRef::getRef(p) : 0)
51  {
52  }
54  : ref(other.ref ? other.ref->increment() : 0)
55  {
56  }
58  {
59  if (ref && ref->decrement())
60  delete ref;
61  }
63  {
64  TokenRef* tmp = TokenRef::getRef(other);
65 
66  if (ref && ref->decrement())
67  delete ref;
68 
69  ref=tmp;
70 
71  return *this;
72  }
74  {
75  if( other.ref != ref )
76  {
77  TokenRef* 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 
93 
94 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
95 }
96 #endif
97 
98 #endif //INC_TokenRefCount_hpp__
Definition: ANTLRException.hpp:15
Definition: TokenRefCount.hpp:43
TokenRefCount(const Token *p=0)
Definition: TokenRefCount.hpp:49
TokenRef * ref
Definition: TokenRefCount.hpp:46
Token *const ptr
Definition: TokenRefCount.hpp:21
#define ANTLR_API
Definition: config.hpp:22
unsigned int count
Definition: TokenRefCount.hpp:22
bool decrement()
Definition: TokenRefCount.hpp:31
Definition: TokenRefCount.hpp:19
TokenRefCount(const TokenRefCount< T > &other)
Definition: TokenRefCount.hpp:53
T * operator->() const
Definition: TokenRefCount.hpp:88
TokenRefCount< Token > RefToken
Definition: TokenRefCount.hpp:92
TokenRef * increment()
Definition: TokenRefCount.hpp:26
TokenRefCount< T > & operator=(const TokenRefCount< T > &other)
Definition: TokenRefCount.hpp:73
TokenRefCount< T > & operator=(Token *other)
Definition: TokenRefCount.hpp:62
~TokenRefCount()
Definition: TokenRefCount.hpp:57
Definition: Token.hpp:24
static TokenRef * getRef(const Token *p)
Definition: TokenRefCount.cpp:26