@@ -71,7 +71,7 @@ namespace internal
7171 */
7272template <
7373 typename T_elem_maybe_void,
74- typename T_RecordComponent = RecordComponent >
74+ typename T_RecordComponent = T_elem_maybe_void >
7575class 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
108115public:
@@ -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+
163180private:
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
195212template <typename T_elem, typename T_RecordComponent>
196213BaseRecord<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
201219template <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
0 commit comments