tesseract  5.0.0
list.h
Go to the documentation of this file.
1 /**********************************************************************
2  ** Licensed under the Apache License, Version 2.0 (the "License");
3  ** you may not use this file except in compliance with the License.
4  ** You may obtain a copy of the License at
5  ** http://www.apache.org/licenses/LICENSE-2.0
6  ** Unless required by applicable law or agreed to in writing, software
7  ** distributed under the License is distributed on an "AS IS" BASIS,
8  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  ** See the License for the specific language governing permissions and
10  ** limitations under the License.
11  *
12  **********************************************************************/
13 
14 #ifndef LIST_ITERATOR_H
15 #define LIST_ITERATOR_H
16 
17 #include <stdint.h>
18 
19 namespace tesseract {
20 
21 template <typename ITERATOR, typename CLASSNAME>
22 class X_ITER : public ITERATOR {
23 public:
24  X_ITER() = default;
25  template <typename U>
26  X_ITER(U *list) : ITERATOR(list) {}
27 
28  CLASSNAME *data() {
29  return static_cast<CLASSNAME *>(ITERATOR::data());
30  }
31  CLASSNAME *data_relative(int8_t offset) {
32  return static_cast<CLASSNAME *>(ITERATOR::data_relative(offset));
33  }
34  CLASSNAME *forward() {
35  return static_cast<CLASSNAME *>(ITERATOR::forward());
36  }
37  CLASSNAME *extract() {
38  return static_cast<CLASSNAME *>(ITERATOR::extract());
39  }
40 };
41 
42 template <typename CONTAINER, typename ITERATOR, typename CLASSNAME>
43 class X_LIST : public CONTAINER {
44 public:
45  X_LIST() = default;
46  X_LIST(const X_LIST &) = delete;
47  X_LIST &operator=(const X_LIST &) = delete;
48  ~X_LIST() {
49  clear();
50  }
51 
52  /* delete elements */
53  void clear() {
54  CONTAINER::internal_clear([](void *link) {delete reinterpret_cast<CLASSNAME *>(link);});
55  }
56 
57  /* Become a deep copy of src_list */
58  template <typename U>
59  void deep_copy(const U *src_list, CLASSNAME *(*copier)(const CLASSNAME *)) {
60  X_ITER<ITERATOR, CLASSNAME> from_it(const_cast<U *>(src_list));
61  X_ITER<ITERATOR, CLASSNAME> to_it(this);
62 
63  for (from_it.mark_cycle_pt(); !from_it.cycled_list(); from_it.forward())
64  to_it.add_after_then_move((*copier)(from_it.data()));
65  }
66 };
67 
68 } // namespace tesseract
69 
70 #endif
CLASSNAME * data_relative(int8_t offset)
Definition: list.h:31
X_ITER(U *list)
Definition: list.h:26
CLASSNAME * extract()
Definition: list.h:37
CLASSNAME * data()
Definition: list.h:28
CLASSNAME * forward()
Definition: list.h:34
X_LIST & operator=(const X_LIST &)=delete
void deep_copy(const U *src_list, CLASSNAME *(*copier)(const CLASSNAME *))
Definition: list.h:59
X_LIST(const X_LIST &)=delete
void clear()
Definition: list.h:53