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 } // namespace internal
55 namespace detail
56 {
57  class ADIOS2File;
58 }
59 
60 namespace debug
61 {
62  void printDirty(Series const &);
63 }
64 
75 class Writable final
76 {
78  friend class internal::AttributableData;
79  friend class internal::SeriesData;
80  friend class Attributable;
81  template <typename T_elem>
82  friend class BaseRecord;
83  template <typename T_elem>
84  friend class BaseRecordInterface;
85  template <typename T, typename T_key, typename T_container>
86  friend class Container;
87  friend class Iteration;
88  friend class Mesh;
89  friend class ParticleSpecies;
90  friend class Series;
91  friend class Record;
92  friend class RecordComponent;
93  friend class AbstractIOHandlerImpl;
94  friend class ADIOS2IOHandlerImpl;
95  friend class detail::ADIOS2File;
96  friend class HDF5IOHandlerImpl;
97  friend class ParallelHDF5IOHandlerImpl;
98  template <typename>
99  friend class AbstractIOHandlerImplCommon;
100  friend class JSONIOHandlerImpl;
101  friend struct test::TestHelper;
102  friend std::string concrete_h5_file_position(Writable *);
103  friend std::string concrete_bp1_file_position(Writable *);
104  template <typename>
105  friend class Span;
106  friend void debug::printDirty(Series const &);
107  friend struct Parameter<Operation::CREATE_DATASET>;
108  friend struct Parameter<Operation::OPEN_DATASET>;
109 
110 private:
111  Writable(internal::AttributableData *);
112 
113 public:
114  ~Writable();
115 
116  Writable(Writable const &other) = delete;
117  Writable(Writable &&other) = delete;
118  Writable &operator=(Writable const &other) = delete;
119  Writable &operator=(Writable &&other) = delete;
120 
128  template <bool flush_entire_series>
129  void seriesFlush(std::string backendConfig = "{}");
130 
131  // clang-format off
132 OPENPMD_private
133  // clang-format on
134 
135  template <bool flush_entire_series>
136  void seriesFlush(internal::FlushParams const &);
137  /*
138  * These members need to be shared pointers since distinct instances of
139  * Writable may share them.
140  */
141  std::shared_ptr<AbstractFilePosition> abstractFilePosition = nullptr;
142  /*
143  * shared_ptr since the IOHandler is shared by multiple Writable instances.
144  * optional to make it possible to release the IOHandler, without first
145  * having to destroy every single Writable.
146  * unique_ptr since AbstractIOHandler is an abstract class.
147  */
148  std::shared_ptr<std::optional<std::unique_ptr<AbstractIOHandler>>>
149  IOHandler = nullptr;
150  /*
151  * Link to the containing Attributable.
152  * If multiple Attributables share the same Writable, then the creating one.
153  * (See SharedAttributableData)
154  */
155  internal::AttributableData *attributable = nullptr;
156  Writable *parent = nullptr;
157 
162  bool dirtySelf = true;
175  bool dirtyRecursive = true;
180  std::string ownKeyWithinParent;
196  bool written = false;
197 };
198 } // 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:222
Base class for any type of record (e.g.
Definition: BaseRecord.hpp:182
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:104
Definition: HDF5IOHandlerImpl.hpp:41
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:146
Definition: JSONIOHandlerImpl.hpp:159
Container for N-dimensional, homogeneous Records.
Definition: Mesh.hpp:41
Definition: ParallelHDF5IOHandlerImpl.hpp:38
Definition: ParticleSpecies.hpp:34
Definition: RecordComponent.hpp:114
Definition: Record.hpp:33
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:76
bool written
Whether a Writable has been written to the backend.
Definition: Writable.hpp:196
std::string ownKeyWithinParent
If parent is not null, then this is a key such that: &(*parent)[key] == this.
Definition: Writable.hpp:180
bool dirtyRecursive
Tracks if there are unwritten changes anywhere in the tree whose ancestor this Writable is.
Definition: Writable.hpp:175
bool dirtySelf
Tracks if there are unwritten changes for this specific Writable.
Definition: Writable.hpp:162
void seriesFlush(std::string backendConfig="{}")
Flush the corresponding Series object.
Definition: Writable.cpp:55
Definition: ADIOS2File.hpp:143
Definition: Attributable.hpp:107
Data members for Series.
Definition: Series.hpp:90
Definition: Attributable.hpp:62
Public definitions of openPMD-api.
Definition: Date.cpp:29
Typesafe description of all required arguments for a specified Operation.
Definition: IOTask.hpp:148