tesseract 4.1.1
Loading...
Searching...
No Matches
doubleptr.h
Go to the documentation of this file.
1// Copyright 2012 Google Inc. All Rights Reserved.
2// Author: rays@google.com (Ray Smith)
4// File: doubleptr.h
5// Description: Double-ended pointer that keeps pointing correctly even
6// when reallocated or copied.
7// Author: Ray Smith
8// Created: Wed Mar 14 12:22:57 PDT 2012
9//
10// (C) Copyright 2012, Google Inc.
11// Licensed under the Apache License, Version 2.0 (the "License");
12// you may not use this file except in compliance with the License.
13// You may obtain a copy of the License at
14// http://www.apache.org/licenses/LICENSE-2.0
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
22
23#ifndef TESSERACT_CCUTIL_DOUBLEPTR_H_
24#define TESSERACT_CCUTIL_DOUBLEPTR_H_
25
26#include "errcode.h"
27
28namespace tesseract {
29
30// A smart pointer class that implements a double-ended pointer. Each end
31// points to the other end. The copy constructor and operator= have MOVE
32// semantics, meaning that the relationship with the other end moves to the
33// destination of the copy, leaving the source unattached.
34// For this reason both the copy constructor and the operator= take a non-const
35// reference argument, and the const reference versions cannot be used.
36// DoublePtr is useful to incorporate into structures that are part of a
37// collection such as GenericVector or STL containers, where reallocs can
38// relocate the members. DoublePtr is also useful in a GenericHeap, where it
39// can correctly maintain the pointer to an element of the heap despite it
40// getting moved around on the heap.
41class DoublePtr {
42 public:
43 DoublePtr() : other_end_(nullptr) {}
44 // Copy constructor steals the partner off src and is therefore a non
45 // const reference arg.
46 // Copying a const DoublePtr generates a compiler error.
48 other_end_ = src.other_end_;
49 if (other_end_ != nullptr) {
50 other_end_->other_end_ = this;
51 src.other_end_ = nullptr;
52 }
53 }
54 // Operator= steals the partner off src, and therefore needs src to be a non-
55 // const reference.
56 // Assigning from a const DoublePtr generates a compiler error.
57 void operator=(DoublePtr& src) {
58 Disconnect();
59 other_end_ = src.other_end_;
60 if (other_end_ != nullptr) {
61 other_end_->other_end_ = this;
62 src.other_end_ = nullptr;
63 }
64 }
65
66 // Connects this and other, discarding any existing connections.
67 void Connect(DoublePtr* other) {
68 other->Disconnect();
69 Disconnect();
70 other->other_end_ = this;
71 other_end_ = other;
72 }
73 // Disconnects this and other, making OtherEnd() return nullptr for both.
74 void Disconnect() {
75 if (other_end_ != nullptr) {
76 other_end_->other_end_ = nullptr;
77 other_end_ = nullptr;
78 }
79 }
80 // Returns the pointer to the other end of the double pointer.
82 return other_end_;
83 }
84
85 private:
86 // Pointer to the other end of the link. It is always true that either
87 // other_end_ == nullptr or other_end_->other_end_ == this.
88 DoublePtr* other_end_;
89};
90
91} // namespace tesseract.
92
93#endif // THIRD_PARTY_TESSERACT_CCUTIL_DOUBLEPTR_H_
DoublePtr * OtherEnd() const
Definition: doubleptr.h:81
void Connect(DoublePtr *other)
Definition: doubleptr.h:67
void operator=(DoublePtr &src)
Definition: doubleptr.h:57
DoublePtr(DoublePtr &src)
Definition: doubleptr.h:47