openPMD-api
ContainerTraits.hpp
1 /* Copyright 2025 Franz Poeschel
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/Iteration.hpp"
24 #include "openPMD/snapshots/IteratorTraits.hpp"
25 #include <optional>
26 #include <utility>
27 
28 /* Public header due to use of AbstractSnapshotsContainer and its iterator type
29  * OpaqueSeriesIterator in Snapshots class header. No direct user interaction
30  * required with this header.
31  */
32 
33 namespace openPMD
34 {
38 template <typename value_type_in>
40  : public AbstractSeriesIterator<
41  OpaqueSeriesIterator<value_type_in>,
42  value_type_in>
43 {
44 private:
45  friend class Series;
46  friend class StatefulSnapshotsContainer;
47  friend class RandomAccessIteratorContainer;
48  using parent_t =
50  // no shared_ptr since copied iterators should not share state
51  std::unique_ptr<DynamicSeriesIterator<value_type_in>> m_internal_iterator;
52 
53 protected:
55 
56  template <typename ChildClass>
57  auto to_concrete_iterator() -> std::optional<ChildClass>
58  {
59  auto bare_iterator =
60  dynamic_cast<ChildClass *>(this->m_internal_iterator.get());
61  if (!bare_iterator)
62  {
63  return std::nullopt;
64  }
65  return *bare_iterator;
66  }
67 
68 public:
71  internal_iterator);
72 
75  OpaqueSeriesIterator &operator=(OpaqueSeriesIterator const &other);
76  OpaqueSeriesIterator &operator=(OpaqueSeriesIterator &&other) noexcept;
77 
79 
80  // dereference
81  using value_type = value_type_in;
82 
83  auto operator*() -> value_type &;
84  auto operator*() const -> value_type const &;
85 
86  // increment/decrement
87  OpaqueSeriesIterator &operator++();
95  OpaqueSeriesIterator operator++(int);
100 
101  // comparison
102  bool operator==(OpaqueSeriesIterator const &other) const;
103 };
104 
109 {
110  RandomAccess,
111  Synchronous
112 };
113 
114 // Internal interface used by Snapshots class for interacting with containers.
115 // This needs to be in a public header since the type definition is used in
116 // private members of the Snapshots class which itself is a public class.
118 {
119 public:
120  using key_type = Iteration::IterationIndex_t;
122  using mapped_type = Iteration;
125  // since AbstractSnapshotsContainer abstracts away the specific mode of
126  // iteration, these are the same type
129  using size_type = size_t;
130 
131  virtual ~AbstractSnapshotsContainer() = 0;
132 
133  virtual auto currentIteration() -> std::optional<value_type *>;
134  virtual auto currentIteration() const
135  -> std::optional<value_type const *> = 0;
136 
137  virtual auto begin() -> iterator = 0;
138  virtual auto begin() const -> const_iterator = 0;
139  virtual auto end() -> iterator = 0;
140  virtual auto end() const -> const_iterator = 0;
141  virtual auto rbegin() -> reverse_iterator = 0;
142  virtual auto rbegin() const -> const_reverse_iterator = 0;
143  virtual auto rend() -> reverse_iterator = 0;
144  virtual auto rend() const -> const_reverse_iterator = 0;
145 
146  virtual auto empty() const -> bool = 0;
147  virtual auto size() const -> size_t = 0;
148 
149  virtual auto at(key_type const &key) const -> mapped_type const & = 0;
150  virtual auto at(key_type const &key) -> mapped_type &;
151 
152  virtual auto operator[](key_type const &key) -> mapped_type & = 0;
153 
154  virtual auto clear() -> void = 0;
155 
156  virtual auto find(key_type const &key) -> iterator = 0;
157  virtual auto find(key_type const &key) const -> const_iterator = 0;
158 
159  virtual auto contains(key_type const &key) const -> bool = 0;
160 
161  virtual auto erase(key_type const &key) -> size_type = 0;
162  virtual auto erase(iterator) -> iterator = 0;
163 
164  virtual auto emplace(value_type &&) -> std::pair<iterator, bool> = 0;
165 
166  virtual auto snapshotWorkflow() const -> SnapshotWorkflow = 0;
167 };
168 } // namespace openPMD
Definition: IteratorTraits.hpp:93
Definition: ContainerTraits.hpp:118
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:104
Definition: IteratorTraits.hpp:56
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:146
Counterpart to Snapshots class: Iterator type that can wrap different implementations internally.
Definition: ContainerTraits.hpp:43
OpaqueSeriesIterator & operator--()
Not implemented for synchronous workflow: Reverse iteration not possible.
Definition: ContainerTraits.cpp:80
Definition: ContainerImpls.hpp:131
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:288
Definition: ContainerImpls.hpp:36
Public definitions of openPMD-api.
Definition: Date.cpp:29
SnapshotWorkflow
Enum used as a label for distinguishing the different Snapshots implementations.
Definition: ContainerTraits.hpp:109