openPMD-api
 
Loading...
Searching...
No Matches
BaseRecordComponent.hpp
1/* Copyright 2017-2025 Fabian Koller, Axel Huebl, 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/ChunkInfo.hpp"
24#include "openPMD/Dataset.hpp"
25#include "openPMD/Error.hpp"
26#include "openPMD/backend/Attributable.hpp"
27
28#include <optional>
29
30// expose private and protected members for invasive testing
31#ifndef OPENPMD_protected
32#define OPENPMD_protected protected:
33#endif
34
35namespace openPMD
36{
37namespace internal
38{
39 class BaseRecordComponentData : virtual public AttributableData
40 {
41 public:
45 std::optional<Dataset> m_dataset;
52 bool m_isConstant = false;
59 bool m_datasetDefined = false;
60
61 BaseRecordComponentData(BaseRecordComponentData const &) = delete;
62 BaseRecordComponentData(BaseRecordComponentData &&) = delete;
63 BaseRecordComponentData &
64 operator=(BaseRecordComponentData const &) = delete;
65 BaseRecordComponentData &operator=(BaseRecordComponentData &&) = delete;
66
67 BaseRecordComponentData() = default;
68
69 virtual void reset()
70 {
71 m_dataset = std::nullopt;
72 m_isConstant = false;
73 m_datasetDefined = false;
74 }
75 };
76} // namespace internal
77
78template <typename>
79class BaseRecord;
80
81class BaseRecordComponent : virtual public Attributable
82{
83 template <typename T, typename T_key, typename T_container>
84 friend class Container;
85
86public:
87 /*
88 * Need to define these manually due to the virtual inheritance from
89 * Attributable.
90 * Otherwise, they would only run from the most derived class
91 * if explicitly called.
92 * If not defining these, a user could destroy copy/move constructors/
93 * assignment operators by deriving from any class that has a virtual
94 * Attributable somewhere.
95 * Care must be taken in move constructors/assignment operators to not move
96 * multiple times (which could happen in diamond inheritance situations).
97 */
98
99 BaseRecordComponent(BaseRecordComponent const &other)
100 : Attributable(NoInit())
101 {
102 m_attri = other.m_attri;
103 m_baseRecordComponentData = other.m_baseRecordComponentData;
104 }
105
106 BaseRecordComponent(BaseRecordComponent &&other) : Attributable(NoInit())
107 {
108 if (other.m_attri)
109 {
110 m_attri = std::move(other.m_attri);
111 }
112 m_baseRecordComponentData = std::move(other.m_baseRecordComponentData);
113 }
114
115 BaseRecordComponent &operator=(BaseRecordComponent const &other)
116 {
117 m_attri = other.m_attri;
118 m_baseRecordComponentData = other.m_baseRecordComponentData;
119 return *this;
120 }
121
122 BaseRecordComponent &operator=(BaseRecordComponent &&other)
123 {
124 if (other.m_attri)
125 {
126 m_attri = std::move(other.m_attri);
127 }
128 m_baseRecordComponentData = std::move(other.m_baseRecordComponentData);
129 return *this;
130 }
131
132 double unitSI() const;
133
134 BaseRecordComponent &resetDatatype(Datatype);
135
136 Datatype getDatatype() const;
137
145 bool constant() const;
146
147 std::optional<size_t> joinedDimension() const;
148
166 ChunkTable availableChunks();
167
168protected:
170 std::shared_ptr<Data_t> m_baseRecordComponentData;
171
172 inline Data_t const &get() const
173 {
174 return *m_baseRecordComponentData;
175 }
176
177 inline Data_t &get()
178 {
179 return *m_baseRecordComponentData;
180 }
181
182 inline void setData(std::shared_ptr<Data_t> data)
183 {
184 m_baseRecordComponentData = std::move(data);
185 Attributable::setData(m_baseRecordComponentData);
186 }
187
188 virtual void setDatasetDefined(Data_t &);
189
190 bool datasetDefined() const;
191
192 BaseRecordComponent();
193 BaseRecordComponent(NoInit);
194}; // BaseRecordComponent
195
196namespace detail
197{
206 template <typename T_RecordComponent>
208 {
209 template <typename T>
210 static void call(T_RecordComponent &rc)
211 {
212 rc.makeConstant(T());
213 }
214
215 template <unsigned n, typename... Args>
216 static void call(Args &&...)
217 {
218 throw std::runtime_error(
219 "makeEmpty: Datatype not supported by openPMD.");
220 }
221 };
222} // namespace detail
223} // namespace openPMD
bool constant() const
Returns true if this is a constant record component.
Definition BaseRecordComponent.cpp:65
ChunkTable availableChunks()
Get data chunks that are available to be loaded from the backend.
Definition BaseRecordComponent.cpp:83
Definition BaseRecordComponent.hpp:40
std::optional< Dataset > m_dataset
The type and extent of the dataset defined by this component.
Definition BaseRecordComponent.hpp:45
bool m_datasetDefined
Tracks if there was any write access to the record component.
Definition BaseRecordComponent.hpp:59
bool m_isConstant
True if this is defined as a constant record component as specified in the openPMD standard.
Definition BaseRecordComponent.hpp:52
Public definitions of openPMD-api.
Definition Date.cpp:29
@ T
time
Definition UnitDimension.hpp:41
Datatype
Concrete datatype of an object available at runtime.
Definition Datatype.hpp:51
Definition Attributable.hpp:251
Functor template to be used in combination with switchType::operator() to set a default value for con...
Definition BaseRecordComponent.hpp:208