@@ -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 :
178184protected:
179185 void readBase ();
180186
187+ void datasetDefined () override ;
188+
181189private:
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
0 commit comments