Skip to content

Commit d5f68d0

Browse files
committed
Tentatively working version
1 parent 434c7a1 commit d5f68d0

File tree

11 files changed

+79
-24
lines changed

11 files changed

+79
-24
lines changed

include/openPMD/RecordComponent.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ OPENPMD_protected
329329

330330
inline internal::RecordComponentData &get()
331331
{
332+
datasetDefined();
332333
return *m_recordComponentData;
333334
}
334335

include/openPMD/backend/Attributable.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Attributable
141141
friend class BaseRecord;
142142
template <typename T_elem>
143143
friend class BaseRecordInterface;
144-
template <typename>
144+
template <typename, typename>
145145
friend class internal::BaseRecordData;
146146
template <typename T, typename T_key, typename T_container>
147147
friend class Container;

include/openPMD/backend/BaseRecord.hpp

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace internal
7171
*/
7272
template <
7373
typename T_elem_maybe_void,
74-
typename T_RecordComponent = RecordComponent>
74+
typename T_RecordComponent = T_elem_maybe_void>
7575
class BaseRecord
7676
: public Container<
7777
typename auxiliary::OkOr<T_elem_maybe_void, BaseRecord<void> >::type>
@@ -91,18 +91,25 @@ class BaseRecord
9191
std::shared_ptr<internal::BaseRecordData<T_elem, T_RecordComponentData> >
9292
m_baseRecordData{
9393
new internal::BaseRecordData<T_elem, T_RecordComponentData>()};
94+
using Data_t = internal::BaseRecordData<T_elem, T_RecordComponentData>;
9495

95-
inline internal::BaseRecordData<T_elem, T_RecordComponentData> &get()
96+
inline Data_t &get()
9697
{
9798
return *m_baseRecordData;
9899
}
99100

100-
inline internal::BaseRecordData<T_elem, T_RecordComponentData> const &
101-
get() const
101+
inline Data_t const &get() const
102102
{
103103
return *m_baseRecordData;
104104
}
105105

106+
inline void setData(std::shared_ptr<Data_t> data)
107+
{
108+
m_baseRecordData = std::move(data);
109+
T_Container::setData(m_baseRecordData);
110+
T_RecordComponent::setData(m_baseRecordData);
111+
}
112+
106113
BaseRecord();
107114

108115
public:
@@ -119,6 +126,14 @@ class BaseRecord
119126
using iterator = typename Container<T_elem>::iterator;
120127
using const_iterator = typename Container<T_elem>::const_iterator;
121128

129+
// this avoids object slicing
130+
operator T_RecordComponent() const
131+
{
132+
T_RecordComponent res;
133+
res.setData(m_baseRecordData);
134+
return res;
135+
}
136+
122137
virtual ~BaseRecord() = default;
123138

124139
mapped_type &operator[](key_type const &key) override;
@@ -160,11 +175,13 @@ class BaseRecord
160175
// BaseRecord(internal::BaseRecordData<T_elem> *);
161176
void readBase();
162177

178+
void datasetDefined() override;
179+
163180
private:
164181
void flush(std::string const &, internal::FlushParams const &) final;
165182
virtual void
166183
flush_impl(std::string const &, internal::FlushParams const &) = 0;
167-
virtual void read() = 0;
184+
// virtual void read() = 0;
168185

169186
/**
170187
* @brief Check recursively whether this BaseRecord is dirty.
@@ -195,7 +212,8 @@ namespace internal
195212
template <typename T_elem, typename T_RecordComponent>
196213
BaseRecord<T_elem, T_RecordComponent>::BaseRecord()
197214
{
198-
Container<T_elem>::setData(m_baseRecordData);
215+
T_Container::setData(m_baseRecordData);
216+
T_RecordComponent::setData(m_baseRecordData);
199217
}
200218

201219
template <typename T_elem, typename T_RecordComponent>
@@ -214,12 +232,12 @@ BaseRecord<T_elem, T_RecordComponent>::operator[](key_type const &key)
214232
"A scalar component can not be contained at "
215233
"the same time as one or more regular components.");
216234

217-
mapped_type &ret = Container<T_elem>::operator[](key);
218235
if (keyScalar)
219236
{
220-
get().m_containsScalar = true;
221-
ret.parent() = this->parent();
237+
datasetDefined();
222238
}
239+
mapped_type &ret = keyScalar ? static_cast<mapped_type &>(*this)
240+
: T_Container::operator[](key);
223241
return ret;
224242
}
225243
}
@@ -240,12 +258,15 @@ BaseRecord<T_elem, T_RecordComponent>::operator[](key_type &&key)
240258
"A scalar component can not be contained at "
241259
"the same time as one or more regular components.");
242260

243-
mapped_type &ret = Container<T_elem>::operator[](std::move(key));
244261
if (keyScalar)
245262
{
246-
get().m_containsScalar = true;
247-
ret.parent() = this->parent();
263+
datasetDefined();
248264
}
265+
/*
266+
* datasetDefined() inits the container entry
267+
*/
268+
mapped_type &ret = keyScalar ? T_Container::at(std::move(key))
269+
: T_Container::operator[](std::move(key));
249270
return ret;
250271
}
251272
}
@@ -406,4 +427,16 @@ inline bool BaseRecord<T_elem, T_RecordComponent>::dirtyRecursive() const
406427
}
407428
return false;
408429
}
430+
431+
template <typename T_elem, typename T_RecordComponent>
432+
void BaseRecord<T_elem, T_RecordComponent>::datasetDefined()
433+
{
434+
// If the RecordComponent API of this object has been used, then the record
435+
// is a scalar one
436+
T_RecordComponent copy;
437+
copy.setData(m_baseRecordData);
438+
T_Container::emplace(RecordComponent::SCALAR, std::move(copy));
439+
get().m_containsScalar = true;
440+
T_RecordComponent::datasetDefined();
441+
}
409442
} // namespace openPMD

include/openPMD/backend/BaseRecordComponent.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class BaseRecordComponent : virtual public Attributable
119119
Attributable::setData(m_baseRecordComponentData);
120120
}
121121

122+
virtual void datasetDefined();
123+
122124
BaseRecordComponent();
123125
}; // BaseRecordComponent
124126

include/openPMD/backend/MeshRecordComponent.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MeshRecordComponent : public RecordComponent
3030
{
3131
template <typename T, typename T_key, typename T_container>
3232
friend class Container;
33+
template <typename, typename>
34+
friend class BaseRecord;
3335

3436
friend class Mesh;
3537

@@ -70,6 +72,9 @@ class MeshRecordComponent : public RecordComponent
7072
*/
7173
template <typename T>
7274
MeshRecordComponent &makeConstant(T);
75+
76+
protected:
77+
void datasetDefined() override;
7378
};
7479

7580
template <typename T>

include/openPMD/backend/PatchRecordComponent.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ OPENPMD_private
9292
// clang-format on
9393

9494
void flush(std::string const &, internal::FlushParams const &);
95-
void read();
95+
virtual void read();
9696

9797
/**
9898
* @brief Check recursively whether this RecordComponent is dirty.
@@ -104,8 +104,8 @@ OPENPMD_private
104104
*/
105105
bool dirtyRecursive() const;
106106

107-
std::shared_ptr<internal::PatchRecordComponentData>
108-
m_patchRecordComponentData{new internal::PatchRecordComponentData()};
107+
std::shared_ptr<internal::PatchRecordComponentData> m_recordComponentData{
108+
new internal::PatchRecordComponentData()};
109109

110110
PatchRecordComponent();
111111

@@ -115,19 +115,19 @@ OPENPMD_protected
115115

116116
inline internal::PatchRecordComponentData const &get() const
117117
{
118-
return *m_patchRecordComponentData;
118+
return *m_recordComponentData;
119119
}
120120

121121
inline internal::PatchRecordComponentData &get()
122122
{
123-
return *m_patchRecordComponentData;
123+
return *m_recordComponentData;
124124
}
125125

126126
inline void
127127
setData(std::shared_ptr<internal::PatchRecordComponentData> data)
128128
{
129-
m_patchRecordComponentData = std::move(data);
130-
BaseRecordComponent::setData(m_patchRecordComponentData);
129+
m_recordComponentData = std::move(data);
130+
BaseRecordComponent::setData(m_recordComponentData);
131131
}
132132
}; // PatchRecordComponent
133133

src/RecordComponent.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ RecordComponent &RecordComponent::resetDataset(Dataset d)
7979
throw error::WrongAPIUsage(
8080
"[RecordComponent] Must set specific datatype.");
8181
}
82+
83+
datasetDefined();
8284
// if( d.extent.empty() )
8385
// throw std::runtime_error("Dataset extent must be at least 1D.");
8486
if (std::any_of(
@@ -167,6 +169,7 @@ RecordComponent &RecordComponent::makeEmpty(Dataset d)
167169
if (rc.m_dataset.extent.size() == 0)
168170
throw std::runtime_error("Dataset extent must be at least 1D.");
169171

172+
datasetDefined();
170173
rc.m_isEmpty = true;
171174
dirty() = true;
172175
if (!written())

src/backend/BaseRecordComponent.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ BaseRecordComponent &BaseRecordComponent::resetDatatype(Datatype d)
3535
"A Records Datatype can not (yet) be changed after it has been "
3636
"written.");
3737

38+
datasetDefined();
3839
get().m_dataset.dtype = d;
3940
return *this;
4041
}
@@ -69,4 +70,7 @@ BaseRecordComponent::BaseRecordComponent()
6970
{
7071
Attributable::setData(m_baseRecordComponentData);
7172
}
73+
74+
void BaseRecordComponent::datasetDefined()
75+
{}
7276
} // namespace openPMD

src/backend/MeshRecordComponent.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
namespace openPMD
2424
{
2525
MeshRecordComponent::MeshRecordComponent() : RecordComponent()
26-
{
27-
setPosition(std::vector<double>{0});
28-
}
26+
{}
2927

3028
void MeshRecordComponent::read()
3129
{
@@ -64,6 +62,12 @@ MeshRecordComponent &MeshRecordComponent::setPosition(std::vector<T> pos)
6462
return *this;
6563
}
6664

65+
void MeshRecordComponent::datasetDefined()
66+
{
67+
setPosition(std::vector<double>{0});
68+
RecordComponent::datasetDefined();
69+
}
70+
6771
template MeshRecordComponent &
6872
MeshRecordComponent::setPosition(std::vector<float> pos);
6973
template MeshRecordComponent &

src/backend/PatchRecordComponent.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ PatchRecordComponent &PatchRecordComponent::resetDataset(Dataset d)
5151
throw std::runtime_error(
5252
"Dataset extent must not be zero in any dimension.");
5353

54+
datasetDefined();
5455
get().m_dataset = d;
5556
dirty() = true;
5657
return *this;
@@ -68,7 +69,7 @@ Extent PatchRecordComponent::getExtent() const
6869

6970
PatchRecordComponent::PatchRecordComponent()
7071
{
71-
BaseRecordComponent::setData(m_patchRecordComponentData);
72+
BaseRecordComponent::setData(m_recordComponentData);
7273
setUnitSI(1);
7374
}
7475

0 commit comments

Comments
 (0)