Skip to content

Commit 3b9acfd

Browse files
committed
[wip] partially working implementation
1 parent 3d8557b commit 3b9acfd

File tree

9 files changed

+48
-15
lines changed

9 files changed

+48
-15
lines changed

include/openPMD/backend/Attributable.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Attributable
127127
friend class BaseRecord;
128128
template< typename T_elem >
129129
friend class BaseRecordInterface;
130-
template< typename >
130+
template< typename, typename >
131131
friend class internal::BaseRecordData;
132132
template<
133133
typename T,

include/openPMD/backend/BaseRecord.hpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ class BaseRecord :
142142
using iterator = typename T_container::iterator;
143143
using const_iterator = typename T_container::const_iterator;
144144

145+
// this avoids object slicing
146+
operator T_RecordComponent() const
147+
{
148+
return { m_baseRecordData };
149+
}
150+
145151
virtual ~BaseRecord() = default;
146152

147153
mapped_type& operator[](key_type const& key) override;
@@ -178,6 +184,8 @@ class BaseRecord :
178184
protected:
179185
void readBase();
180186

187+
void datasetDefined() override;
188+
181189
private:
182190
void flush(std::string const&) final;
183191
virtual void flush_impl(std::string const&) = 0;
@@ -238,12 +246,11 @@ BaseRecord< T_elem, T_RecordComponent >::operator[]( key_type const & key )
238246
throw std::runtime_error("A scalar component can not be contained at "
239247
"the same time as one or more regular components.");
240248

241-
mapped_type& ret = T_container::operator[](key);
242249
if( keyScalar )
243250
{
244-
get().m_containsScalar = true;
245-
ret.parent() = this->parent();
251+
datasetDefined();
246252
}
253+
mapped_type& ret = keyScalar ? *this : T_container::operator[](key);
247254
return ret;
248255
}
249256
}
@@ -263,12 +270,12 @@ BaseRecord< T_elem, T_RecordComponent >::operator[](key_type&& key)
263270
throw std::runtime_error("A scalar component can not be contained at "
264271
"the same time as one or more regular components.");
265272

266-
mapped_type& ret = T_container::operator[](std::move(key));
267273
if( keyScalar )
268274
{
269-
get().m_containsScalar = true;
270-
ret.parent() = this->parent();
275+
datasetDefined();
271276
}
277+
mapped_type& ret =
278+
keyScalar ? *this : T_container::operator[](std::move(key));
272279
return ret;
273280
}
274281
}
@@ -417,4 +424,18 @@ BaseRecord< T_elem, T_RecordComponent >::dirtyRecursive() const
417424
}
418425
return false;
419426
}
427+
428+
template< typename T_elem, typename T_RecordComponent >
429+
void BaseRecord< T_elem, T_RecordComponent >::datasetDefined()
430+
{
431+
// If the RecordComponent API of this object has been used, then the record
432+
// is a scalar one
433+
T_container::emplace(
434+
RecordComponent::SCALAR,
435+
// need to construct it in this line already, construction inside
436+
// emplace is impossible due to private constructors
437+
T_RecordComponent{ m_baseRecordData } );
438+
get().m_containsScalar = true;
439+
T_RecordComponent::datasetDefined();
440+
}
420441
} // namespace openPMD

include/openPMD/backend/BaseRecordComponent.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class BaseRecordComponent : public Attributable
133133

134134
BaseRecordComponent( std::shared_ptr< internal::BaseRecordComponentData > );
135135

136+
virtual void datasetDefined();
137+
136138
private:
137139
BaseRecordComponent();
138140
}; // BaseRecordComponent

include/openPMD/backend/MeshRecordComponent.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class MeshRecordComponent : public RecordComponent
8686
*/
8787
template< typename T >
8888
MeshRecordComponent& makeConstant(T);
89+
90+
protected:
91+
void datasetDefined() override;
8992
};
9093

9194

src/Mesh.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,8 @@ Mesh::flush_impl(std::string const& name)
237237
if( scalar() )
238238
{
239239
MeshRecordComponent& mrc = at(RecordComponent::SCALAR);
240-
mrc.parent() = parent();
241240
mrc.flush(name);
242241
IOHandler()->flush();
243-
writable().abstractFilePosition = mrc.writable().abstractFilePosition;
244-
written() = true;
245242
} else
246243
{
247244
Parameter< Operation::CREATE_PATH > pCreate;
@@ -257,8 +254,6 @@ Mesh::flush_impl(std::string const& name)
257254
for( auto& comp : *this )
258255
{
259256
comp.second.flush(name);
260-
writable().abstractFilePosition =
261-
comp.second.writable().abstractFilePosition;
262257
}
263258
}
264259
else

src/RecordComponent.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ RecordComponent::resetDataset( Dataset d )
9393
throw error::WrongAPIUsage(
9494
"[RecordComponent] Must set specific datatype." );
9595
}
96+
97+
datasetDefined();
9698
// if( d.extent.empty() )
9799
// throw std::runtime_error("Dataset extent must be at least 1D.");
98100
if( std::any_of(
@@ -186,6 +188,7 @@ RecordComponent::makeEmpty( Dataset d )
186188
if( rc.m_dataset.extent.size() == 0 )
187189
throw std::runtime_error( "Dataset extent must be at least 1D." );
188190

191+
datasetDefined();
189192
rc.m_isEmpty = true;
190193
dirty() = true;
191194
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::resetDatatype(Datatype d)
3535
if( written() )
3636
throw std::runtime_error("A Records Datatype can not (yet) be changed after it has been written.");
3737

38+
datasetDefined();
3839
get().m_dataset.dtype = d;
3940
return *this;
4041
}
@@ -79,4 +80,7 @@ BaseRecordComponent::BaseRecordComponent()
7980
{
8081
Attributable::setData( m_baseRecordComponentData );
8182
}
83+
84+
void BaseRecordComponent::datasetDefined()
85+
{}
8286
} // namespace openPMD

src/backend/MeshRecordComponent.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ namespace openPMD
2525
{
2626
MeshRecordComponent::MeshRecordComponent()
2727
: RecordComponent()
28-
{
29-
setPosition(std::vector< double >{0});
30-
}
28+
{}
3129

3230
void
3331
MeshRecordComponent::read()
@@ -62,6 +60,12 @@ MeshRecordComponent::setPosition(std::vector< T > pos)
6260
return *this;
6361
}
6462

63+
void MeshRecordComponent::datasetDefined()
64+
{
65+
setPosition(std::vector< double >{0});
66+
RecordComponent::datasetDefined();
67+
}
68+
6569
template
6670
MeshRecordComponent&
6771
MeshRecordComponent::setPosition(std::vector< float > pos);

src/backend/PatchRecordComponent.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ PatchRecordComponent::resetDataset(Dataset d)
5353
[](Extent::value_type const& i) { return i == 0u; }) )
5454
throw std::runtime_error("Dataset extent must not be zero in any dimension.");
5555

56+
datasetDefined();
5657
get().m_dataset = d;
5758
dirty() = true;
5859
return *this;

0 commit comments

Comments
 (0)