openPMD-api
Writable.hpp
1 /* Copyright 2017-2025 Fabian Koller, Axel Huebl, Franz Poeschel, Luca Fedeli
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/IO/AbstractIOHandler.hpp"
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 // expose private and protected members for invasive testing
30 #ifndef OPENPMD_private
31 #define OPENPMD_private private:
32 #endif
33 
34 namespace openPMD
35 {
36 namespace test
37 {
38  struct TestHelper;
39 } // namespace test
40 class AbstractFilePosition;
41 class AbstractIOHandler;
42 struct ADIOS2FilePosition;
43 template <typename FilePositionType>
44 class AbstractIOHandlerImplCommon;
45 template <typename>
46 class Span;
47 class Series;
48 
49 namespace internal
50 {
51  class SharedAttributableData;
52  class AttributableData;
53  class SeriesData;
54  class ScientificDefaults;
55 } // namespace internal
56 namespace detail
57 {
58  class ADIOS2File;
59 }
60 
61 namespace debug
62 {
63  void printDirty(Series const &);
64 }
65 
76 class Writable final
77 {
79  friend class internal::AttributableData;
80  friend class internal::SeriesData;
81  friend class Attributable;
82  template <typename T_elem>
83  friend class BaseRecord;
84  template <typename T_elem>
85  friend class BaseRecordInterface;
86  template <typename T, typename T_key, typename T_container>
87  friend class Container;
88  friend class Iteration;
89  friend class Mesh;
90  friend class ParticleSpecies;
91  friend class Series;
92  friend class Record;
93  friend class RecordComponent;
94  friend class AbstractIOHandlerImpl;
95  friend class ADIOS2IOHandlerImpl;
96  friend class detail::ADIOS2File;
97  friend class HDF5IOHandlerImpl;
98  friend class ParallelHDF5IOHandlerImpl;
99  template <typename>
100  friend class AbstractIOHandlerImplCommon;
101  friend class JSONIOHandlerImpl;
102  friend struct test::TestHelper;
103  friend std::string concrete_h5_file_position(Writable *);
104  friend std::string concrete_bp1_file_position(Writable *);
105  template <typename>
106  friend class Span;
107  friend void debug::printDirty(Series const &);
108  friend struct Parameter<Operation::CREATE_DATASET>;
109  friend struct Parameter<Operation::OPEN_DATASET>;
110  friend class internal::ScientificDefaults;
111 
112 private:
113  Writable(internal::AttributableData *);
114 
115 public:
116  ~Writable();
117 
118  Writable(Writable const &other) = delete;
119  Writable(Writable &&other) = delete;
120  Writable &operator=(Writable const &other) = delete;
121  Writable &operator=(Writable &&other) = delete;
122 
130  template <bool flush_entire_series>
131  void seriesFlush(std::string backendConfig = "{}");
132 
133  // clang-format off
134 OPENPMD_private
135  // clang-format on
136 
137  template <bool flush_entire_series>
138  void seriesFlush(internal::FlushParams const &);
139  /*
140  * These members need to be shared pointers since distinct instances of
141  * Writable may share them.
142  */
143  std::shared_ptr<AbstractFilePosition> abstractFilePosition = nullptr;
144  /*
145  * shared_ptr since the IOHandler is shared by multiple Writable instances.
146  * optional to make it possible to release the IOHandler, without first
147  * having to destroy every single Writable.
148  * unique_ptr since AbstractIOHandler is an abstract class.
149  */
150  std::shared_ptr<std::optional<std::unique_ptr<AbstractIOHandler>>>
151  IOHandler = nullptr;
152  /*
153  * Link to the containing Attributable.
154  * If multiple Attributables share the same Writable, then the creating one.
155  * (See SharedAttributableData)
156  */
157  internal::AttributableData *attributable = nullptr;
158  Writable *parent = nullptr;
159 
164  bool dirtySelf = true;
177  bool dirtyRecursive = true;
182  std::string ownKeyWithinParent;
198  bool written = false;
199 };
200 } // namespace openPMD
Definition: ADIOS2IOHandler.hpp:100
Definition: AbstractIOHandlerImplCommon.hpp:40
Definition: AbstractIOHandlerImpl.hpp:36
Layer to manage storage of attributes associated with file objects.
Definition: Attributable.hpp:225
Base class for any type of record (e.g.
Definition: BaseRecord.hpp:186
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:106
Definition: HDF5IOHandlerImpl.hpp:41
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:172
Definition: JSONIOHandlerImpl.hpp:159
Container for N-dimensional, homogeneous Records.
Definition: Mesh.hpp:44
Definition: ParallelHDF5IOHandlerImpl.hpp:38
Definition: ParticleSpecies.hpp:39
Definition: RecordComponent.hpp:118
Definition: Record.hpp:34
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:288
Subset of C++20 std::span class template.
Definition: Span.hpp:38
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:77
bool written
Whether a Writable has been written to the backend.
Definition: Writable.hpp:198
std::string ownKeyWithinParent
If parent is not null, then this is a key such that: &(*parent)[key] == this.
Definition: Writable.hpp:182
bool dirtyRecursive
Tracks if there are unwritten changes anywhere in the tree whose ancestor this Writable is.
Definition: Writable.hpp:177
bool dirtySelf
Tracks if there are unwritten changes for this specific Writable.
Definition: Writable.hpp:164
void seriesFlush(std::string backendConfig="{}")
Flush the corresponding Series object.
Definition: Writable.cpp:55
Definition: ADIOS2File.hpp:143
Definition: Attributable.hpp:110
Definition: ScientificDefaults.hpp:23
Data members for Series.
Definition: Series.hpp:90
Definition: Attributable.hpp:65
Public definitions of openPMD-api.
Definition: Date.cpp:29
Typesafe description of all required arguments for a specified Operation.
Definition: IOTask.hpp:148