Spear Parser (1): smart pointer class

Source: Internet
Author: User

Introduction to Spear Parser

Spear Parser (hereinafter referred to as Spear) contains the training part of Collins Model 1. It is a good entry code for understanding and implementing the Collins Model. Because the Code provided by thesis of M Collins only contains the part of Parsing and does not have the part of Training, it may be a bit difficult to understand the code of Parsing. Dan Bikel seems to be a little huge, and it is not necessary for beginners to look so complicated. So, based on the principle of laziness, I found a fairly good Spear for Google Collins Model implementation.

The Spear Parser is open-source.

To better understand Spear and record learning progress, make a series of study notes. You can see from the top down. When you remember, you can start from the bottom to the top. First, you can talk about some peripheral classes, and finally you can talk about the overall implementation ideas.

This section describes some auxiliary classes of Spear. The first class is the Smart Pointer class ).

Introduction to smart pointers

Smart pointers are a common technology in C ++. They are mainly used to prevent a pointer from pointing to a non-existent object. The core is to control the deletion time of an object. For details, refer to the introduction in C ++ Primer. The Chinese version of the fourth edition is in section 13.5.1.

Implementation of the smart pointer class in Spear 

The intelligent pointer class in Spear has two main classes: RCObject and RCIPtr. RCIPtr is an implementation class of a specific smart pointer. RCObject is the parent class of all objects it can point. RCObject encapsulates the management behavior of count so that RCIPtr can use the use-count technology to implement Smart Pointer.

The code for RCObject is as follows:

 
 
  1. class RCObject  
  2.  public: 
  3.   void addReference(); 
  4.   void removeReference(); 
  5.  protected: 
  6.   RCObject(); 
  7.   RCObject(const RCObject& rhs); 
  8.   RCObject& operator=(const RCObject& rhs); 
  9.   virtual ~RCObject() = 0; 
  10.  public: 
  11.   unsigned short refCount; 
  12. }; 
  13. inline RCObject::RCObject() 
  14.   : refCount(0){} // std::cout << "RCObject constr\n"; } 
  15. inline RCObject::RCObject(const RCObject&) 
  16.   : refCount(0){} // std::cout << "RCObject copy constr\n"; } 
  17. inline RCObject& RCObject::operator=(const RCObject&) 
  18.   return *this; 
  19. }   
  20. inline RCObject::~RCObject() {} 
  21. inline void RCObject::addReference()  
  22.   ++refCount; 
  23. inline void RCObject::removeReference() 
  24.   if (--refCount == 0) delete this; 

This code is quite simple, that is, common construction, copy construction, assignment, structure analysis, and refCount operations. Note that when refCount is 0 in removeReference, the current object is deleted. This is actually a Smart Pointer implementation idea. In the future, we can see that many classes inherit RCObject, so that you can use smart pointer technology to manage pointers to their objects.

RCIPtr is the implementation of Smart Pointer. It mainly involves the implementation of copy construction, assignment operators, and destructor. At the same time, it also loads various = ,! =, But these loads are not the focus.

The RCIPrt code is as follows:

 
 
  1. template<class T> 
  2. class RCIPtr  
  3.  public: 
  4.   explicit RCIPtr(T* realPtr = 0); 
  5.   RCIPtr(const RCIPtr& rhs); 
  6.   ~RCIPtr(); 
  7.   RCIPtr& operator=(const RCIPtr& rhs); 
  8.   T* operator->() const; 
  9.   T& operator*() const; 
  10.   void clear() { 
  11.     *this = RCIPtr<T>(0); 
  12.   }; 
  13. private: 
  14.   T *pointee; 
  15.   void init() { 
  16.     if(pointee != 0) pointee->addReference(); 
  17.   } 
  18. }; 

Core code implementation:

 
 
  1. template<class T> 
  2. RCIPtr<T>::RCIPtr(T* realPtr) 
  3.   : pointee(realPtr) 
  4. {  
  5.   init(); 
  6. template<class T> 
  7. RCIPtr<T>::RCIPtr(const RCIPtr& rhs) 
  8.   : pointee(rhs.pointee) 
  9. {  
  10.   init();  
  11. template<class T> 
  12. RCIPtr<T>::~RCIPtr() 
  13. {  
  14.   if(pointee != 0) pointee->removeReference();  
  15. template<class T> 
  16. RCIPtr<T>& RCIPtr<T>::operator=(const RCIPtr& rhs) 
  17.   if (pointee != rhs.pointee) {          
  18.     if(pointee != 0) pointee->removeReference();      
  19.     pointee = rhs.pointee; 
  20.     init(); 
  21.   } 
  22.   return *this; 

Use of the Spear smart pointer class

RCIPtr <BankEdge> _ head; this can be considered as: BankEdge * _ head. The usage is basically the same as _ head-& gt;, and * _ head can be used as the original pointer, because all of them have been reloaded.

This article is from the "Qinghe" blog, please be sure to keep this source http://snowteng17.blog.51cto.com/1532294/541344

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.