ANTLR Support Libraries 2.7.1+
antlr/TokenRefCount.hpp
Go to the documentation of this file.
00001 #ifndef INC_TokenRefCount_hpp__
00002 # define INC_TokenRefCount_hpp__
00003 
00004 /* ANTLR Translator Generator
00005  * Project led by Terence Parr at http://www.jGuru.com
00006  * Software rights: http://www.antlr.org/license.html
00007  *
00008  * $Id:$
00009  */
00010 
00011 # include <antlr/config.hpp>
00012 
00013 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00014 namespace antlr {
00015 #endif
00016 
00017 class Token;
00018 
00019 struct ANTLR_API TokenRef
00020 {
00021    Token* const ptr;
00022    unsigned int count;
00023 
00024    TokenRef(Token* p);
00025    ~TokenRef();
00026    TokenRef* increment()
00027    {
00028       ++count;
00029       return this;
00030    }
00031    bool decrement()
00032    {
00033       return (--count==0);
00034    }
00035 
00036    static TokenRef* getRef(const Token* p);
00037 private:
00038    TokenRef( const TokenRef& );
00039    TokenRef& operator=( const TokenRef& );
00040 };
00041 
00042 template<class T>
00043    class ANTLR_API TokenRefCount
00044 {
00045 private:
00046    TokenRef* ref;
00047 
00048 public:
00049    TokenRefCount(const Token* p=0)
00050    : ref(p ? TokenRef::getRef(p) : 0)
00051    {
00052    }
00053    TokenRefCount(const TokenRefCount<T>& other)
00054    : ref(other.ref ? other.ref->increment() : 0)
00055    {
00056    }
00057    ~TokenRefCount()
00058    {
00059       if (ref && ref->decrement())
00060          delete ref;
00061    }
00062    TokenRefCount<T>& operator=(Token* other)
00063    {
00064       TokenRef* tmp = TokenRef::getRef(other);
00065 
00066       if (ref && ref->decrement())
00067          delete ref;
00068 
00069       ref=tmp;
00070 
00071       return *this;
00072    }
00073    TokenRefCount<T>& operator=(const TokenRefCount<T>& other)
00074    {
00075       if( other.ref != ref )
00076       {
00077          TokenRef* tmp = other.ref ? other.ref->increment() : 0;
00078 
00079          if (ref && ref->decrement())
00080             delete ref;
00081 
00082          ref=tmp;
00083       }
00084       return *this;
00085    }
00086 
00087    operator T* ()  const { return ref ? static_cast<T*>(ref->ptr) : 0; }
00088    T* operator->() const { return ref ? static_cast<T*>(ref->ptr) : 0; }
00089    T* get()        const { return ref ? static_cast<T*>(ref->ptr) : 0; }
00090 };
00091 
00092 typedef TokenRefCount<Token> RefToken;
00093 
00094 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
00095 }
00096 #endif
00097 
00098 #endif //INC_TokenRefCount_hpp__
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines