@@ -145,16 +145,15 @@ namespace internal
145145 {
146146 /*
147147 * m_embeddeddatasets and its friends should point to the same instance
148- * of Attributable Can only use a non-owning pointer in here in order to
149- * avoid shared pointer cycles. When handing this object out to users,
150- * we create a copy that has a proper owning pointer (see
151- * CustomHierarchy::datasets()).
148+ * of Attributable.
152149 */
153150 for (auto p : std::initializer_list<Attributable *>{
154151 &m_embeddedDatasets, &m_embeddedMeshes, &m_embeddedParticles})
155152 {
156- (*p).Attributable ::setData (
157- std::shared_ptr<AttributableData>(this , [](auto const *) {}));
153+ static_cast <std::shared_ptr<internal::SharedAttributableData> &>(
154+ *p->m_attri ) =
155+ static_cast <
156+ std::shared_ptr<internal::SharedAttributableData> &>(*this );
158157 }
159158 }
160159} // namespace internal
@@ -425,12 +424,12 @@ void CustomHierarchy::synchronizeContainers(
425424 target_container.emplace (std::move (pair));
426425 }
427426 source.container ().clear ();
428- auto &target_attributes = target.m_attri -> m_attributes ;
429- for (auto &pair : source.m_attri -> m_attributes )
427+ auto &target_attributes = target.get (). m_attributes ;
428+ for (auto &pair : source.get (). m_attributes )
430429 {
431430 target_attributes.emplace (std::move (pair));
432431 }
433- source.m_attri -> m_attributes .clear ();
432+ source.get (). m_attributes .clear ();
434433 source.setData (target.m_containerData );
435434 // We need to do this since we redirect the Attributable pointers for some
436435 // members:
@@ -454,14 +453,14 @@ void CustomHierarchy::flush_internal(
454453 {
455454 if (!meshes.empty ())
456455 {
457- auto defaultMeshes =
456+ auto & defaultMeshes =
458457 (*this )[defaultMeshesPath].asContainerOf <Mesh>();
459458 synchronizeContainers (defaultMeshes, meshes);
460459 }
461460
462461 if (!particles.empty ())
463462 {
464- auto defaultParticles =
463+ auto & defaultParticles =
465464 (*this )[defaultParticlesPath].asContainerOf <ParticleSpecies>();
466465 synchronizeContainers (defaultParticles, particles);
467466 }
@@ -594,54 +593,45 @@ bool CustomHierarchy::dirtyRecursive() const
594593 return false ;
595594}
596595
597- Container<RecordComponent> CustomHierarchy::datasets ()
596+ Container<RecordComponent> & CustomHierarchy::datasets ()
598597{
599- Container<RecordComponent> res = get ().m_embeddedDatasets ;
600- res.Attributable ::setData (m_customHierarchyData);
601- return res;
598+ return get ().m_embeddedDatasets ;
602599}
603600
604601template <typename ContainedType>
605- auto CustomHierarchy::asContainerOf () -> Container<ContainedType>
602+ auto CustomHierarchy::asContainerOf () -> Container<ContainedType> &
606603{
607604 if constexpr (std::is_same_v<ContainedType, CustomHierarchy>)
608605 {
609606 return *static_cast <Container<CustomHierarchy> *>(this );
610607 }
608+ else if constexpr (std::is_same_v<ContainedType, Mesh>)
609+ {
610+ return get ().m_embeddedMeshes ;
611+ }
612+ else if constexpr (std::is_same_v<ContainedType, ParticleSpecies>)
613+ {
614+ return get ().m_embeddedParticles ;
615+ }
616+ else if constexpr (std::is_same_v<ContainedType, RecordComponent>)
617+ {
618+ return get ().m_embeddedDatasets ;
619+ }
611620 else
612621 {
613- Container<ContainedType> res = [&data = get ()]() {
614- if constexpr (std::is_same_v<ContainedType, Mesh>)
615- {
616- return data.m_embeddedMeshes ;
617- }
618- else if constexpr (std::is_same_v<ContainedType, ParticleSpecies>)
619- {
620- return data.m_embeddedParticles ;
621- }
622- else if constexpr (std::is_same_v<ContainedType, RecordComponent>)
623- {
624- return data.m_embeddedDatasets ;
625- }
626- else
627- {
628- static_assert (
629- auxiliary::dependent_false_v<ContainedType>,
630- " [CustomHierarchy::asContainerOf] Type parameter must be "
631- " one of: CustomHierarchy, RecordComponent, Mesh, "
632- " ParticleSpecies." );
633- }
634- }();
635- res.Attributable ::setData (m_customHierarchyData);
636- return res;
622+ static_assert (
623+ auxiliary::dependent_false_v<ContainedType>,
624+ " [CustomHierarchy::asContainerOf] Type parameter must be "
625+ " one of: CustomHierarchy, RecordComponent, Mesh, "
626+ " ParticleSpecies." );
637627 }
638628}
639629
640630template auto CustomHierarchy::asContainerOf<CustomHierarchy>()
641- -> Container<CustomHierarchy>;
631+ -> Container<CustomHierarchy> & ;
642632template auto CustomHierarchy::asContainerOf<RecordComponent>()
643- -> Container<RecordComponent>;
644- template auto CustomHierarchy::asContainerOf<Mesh>() -> Container<Mesh>;
633+ -> Container<RecordComponent> & ;
634+ template auto CustomHierarchy::asContainerOf<Mesh>() -> Container<Mesh> & ;
645635template auto CustomHierarchy::asContainerOf<ParticleSpecies>()
646- -> Container<ParticleSpecies>;
636+ -> Container<ParticleSpecies> & ;
647637} // namespace openPMD
0 commit comments