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