tesseract
3.03
|
00001 // Copyright 2012 Google Inc. All Rights Reserved. 00002 // Author: rays@google.com (Ray Smith) 00004 // File: doubleptr.h 00005 // Description: Double-ended pointer that keeps pointing correctly even 00006 // when reallocated or copied. 00007 // Author: Ray Smith 00008 // Created: Wed Mar 14 12:22:57 PDT 2012 00009 // 00010 // (C) Copyright 2012, Google Inc. 00011 // Licensed under the Apache License, Version 2.0 (the "License"); 00012 // you may not use this file except in compliance with the License. 00013 // You may obtain a copy of the License at 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 // 00022 00023 #ifndef TESSERACT_CCUTIL_DOUBLEPTR_H_ 00024 #define TESSERACT_CCUTIL_DOUBLEPTR_H_ 00025 00026 #include "errcode.h" 00027 00028 namespace tesseract { 00029 00030 // A smart pointer class that implements a double-ended pointer. Each end 00031 // points to the other end. The copy constructor and operator= have MOVE 00032 // semantics, meaning that the relationship with the other end moves to the 00033 // destination of the copy, leaving the source unattached. 00034 // For this reason both the copy constructor and the operator= take a non-const 00035 // reference argument, and the const reference versions cannot be used. 00036 // DoublePtr is useful to incorporate into structures that are part of a 00037 // collection such as GenericVector or STL containers, where reallocs can 00038 // relocate the members. DoublePtr is also useful in a GenericHeap, where it 00039 // can correctly maintain the pointer to an element of the heap despite it 00040 // getting moved around on the heap. 00041 class DoublePtr { 00042 public: 00043 DoublePtr() : other_end_(NULL) {} 00044 // Copy constructor steals the partner off src and is therefore a non 00045 // const reference arg. 00046 // Copying a const DoublePtr generates a compiler error. 00047 DoublePtr(DoublePtr& src) { 00048 other_end_ = src.other_end_; 00049 if (other_end_ != NULL) { 00050 other_end_->other_end_ = this; 00051 src.other_end_ = NULL; 00052 } 00053 } 00054 // Operator= steals the partner off src, and therefore needs src to be a non- 00055 // const reference. 00056 // Assigning from a const DoublePtr generates a compiler error. 00057 void operator=(DoublePtr& src) { 00058 Disconnect(); 00059 other_end_ = src.other_end_; 00060 if (other_end_ != NULL) { 00061 other_end_->other_end_ = this; 00062 src.other_end_ = NULL; 00063 } 00064 } 00065 00066 // Connects this and other, discarding any existing connections. 00067 void Connect(DoublePtr* other) { 00068 other->Disconnect(); 00069 Disconnect(); 00070 other->other_end_ = this; 00071 other_end_ = other; 00072 } 00073 // Disconnects this and other, making OtherEnd() return NULL for both. 00074 void Disconnect() { 00075 if (other_end_ != NULL) { 00076 other_end_->other_end_ = NULL; 00077 other_end_ = NULL; 00078 } 00079 } 00080 // Returns the pointer to the other end of the double pointer. 00081 DoublePtr* OtherEnd() const { 00082 return other_end_; 00083 } 00084 00085 private: 00086 // Pointer to the other end of the link. It is always true that either 00087 // other_end_ == NULL or other_end_->other_end_ == this. 00088 DoublePtr* other_end_; 00089 }; 00090 00091 } // namespace tesseract. 00092 00093 #endif // THIRD_PARTY_TESSERACT_CCUTIL_DOUBLEPTR_H_