From e67aa3e4fb682a5f2b534185b2af9c92be960584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 3 Dec 2021 12:55:13 +0100 Subject: [PATCH] Make RecordComponent a base class of BaseRecord --- include/openPMD/RecordComponent.hpp | 11 ++-- include/openPMD/backend/BaseRecord.hpp | 80 ++++++++++++++++---------- include/openPMD/backend/Container.hpp | 3 +- src/backend/PatchRecord.cpp | 2 +- 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/include/openPMD/RecordComponent.hpp b/include/openPMD/RecordComponent.hpp index 052e94b9ac..a08e668229 100644 --- a/include/openPMD/RecordComponent.hpp +++ b/include/openPMD/RecordComponent.hpp @@ -329,19 +329,22 @@ class RecordComponent : public BaseRecordComponent RecordComponent(); OPENPMD_protected: - RecordComponent( std::shared_ptr< internal::RecordComponentData > ); - inline internal::RecordComponentData const & get() const + using DataClass = internal::RecordComponentData; + + RecordComponent( std::shared_ptr< DataClass > ); + + inline DataClass const & get() const { return *m_recordComponentData; } - inline internal::RecordComponentData & get() + inline DataClass & get() { return *m_recordComponentData; } - inline void setData( std::shared_ptr< internal::RecordComponentData > data ) + inline void setData( std::shared_ptr< DataClass > data ) { m_recordComponentData = std::move( data ); BaseRecordComponent::setData( m_recordComponentData ); diff --git a/include/openPMD/backend/BaseRecord.hpp b/include/openPMD/backend/BaseRecord.hpp index a99bb5aa20..6ae5fd96ed 100644 --- a/include/openPMD/backend/BaseRecord.hpp +++ b/include/openPMD/backend/BaseRecord.hpp @@ -32,10 +32,28 @@ namespace openPMD { +namespace detail +{ + template< typename T, typename T_AttributableBase > + using ContainerWithBase = Container< + T, + std::string, + std::map< std::string, T >, + T_AttributableBase >; + + template< typename T, typename T_AttributableBaseData > + using ContainerDataWithBase = internal::ContainerData< + T, + std::string, + std::map< std::string, T >, + T_AttributableBaseData >; +} + namespace internal { template< typename T_elem > - class BaseRecordData : public ContainerData< T_elem > + class BaseRecordData + : public detail::ContainerDataWithBase< T_elem, RecordComponentData > { public: /** @@ -67,11 +85,16 @@ namespace internal * auxiliary::OkOr class template to treat the void type specially. */ template< typename T_elem_maybe_void > -class BaseRecord : public Container< - typename auxiliary::OkOr< T_elem_maybe_void, BaseRecord< void > >::type > +class BaseRecord : + public detail::ContainerWithBase< + typename auxiliary::OkOr< T_elem_maybe_void, BaseRecord< void > >::type, + RecordComponent > { using T_elem = typename auxiliary::OkOr< T_elem_maybe_void, BaseRecord< void > >::type; + using T_container = detail::ContainerWithBase< + typename auxiliary::OkOr< T_elem_maybe_void, BaseRecord< void > >::type, + RecordComponent >; friend class Iteration; friend class ParticleSpecies; friend class PatchRecord; @@ -100,22 +123,22 @@ class BaseRecord : public Container< inline void setData( internal::BaseRecordData< T_elem > * data ) { m_baseRecordData = std::move( data ); - Container< T_elem >::setData( m_baseRecordData ); + T_container::setData( m_baseRecordData ); } public: - using key_type = typename Container< T_elem >::key_type; - using mapped_type = typename Container< T_elem >::mapped_type; - using value_type = typename Container< T_elem >::value_type; - using size_type = typename Container< T_elem >::size_type; - using difference_type = typename Container< T_elem >::difference_type; - using allocator_type = typename Container< T_elem >::allocator_type; - using reference = typename Container< T_elem >::reference; - using const_reference = typename Container< T_elem >::const_reference; - using pointer = typename Container< T_elem >::pointer; - using const_pointer = typename Container< T_elem >::const_pointer; - using iterator = typename Container< T_elem >::iterator; - using const_iterator = typename Container< T_elem >::const_iterator; + using key_type = typename T_container::key_type; + using mapped_type = typename T_container::mapped_type; + using value_type = typename T_container::value_type; + using size_type = typename T_container::size_type; + using difference_type = typename T_container::difference_type; + using allocator_type = typename T_container::allocator_type; + using reference = typename T_container::reference; + using const_reference = typename T_container::const_reference; + using pointer = typename T_container::pointer; + using const_pointer = typename T_container::const_pointer; + using iterator = typename T_container::iterator; + using const_iterator = typename T_container::const_iterator; virtual ~BaseRecord() = default; @@ -157,7 +180,7 @@ class BaseRecord : public Container< private: void flush(std::string const&) final; virtual void flush_impl(std::string const&) = 0; - virtual void read() = 0; + virtual void read() override = 0; /** * @brief Check recursively whether this BaseRecord is dirty. @@ -171,7 +194,6 @@ class BaseRecord : public Container< dirtyRecursive() const; }; // BaseRecord - // implementation namespace internal @@ -187,15 +209,15 @@ namespace internal } // namespace internal template< typename T_elem > -BaseRecord< T_elem >::BaseRecord() : Container< T_elem >{ nullptr } +BaseRecord< T_elem >::BaseRecord() : T_container{ nullptr } { - Container< T_elem >::setData( m_baseRecordData ); + T_container::setData( m_baseRecordData ); } template< typename T_elem > BaseRecord< T_elem >::BaseRecord( std::shared_ptr< internal::BaseRecordData< T_elem > > data ) - : Container< T_elem >{ data } + : T_container{ data } , m_baseRecordData{ std::move( data ) } { } @@ -210,11 +232,11 @@ BaseRecord< T_elem >::operator[]( key_type const & key ) else { bool const keyScalar = (key == RecordComponent::SCALAR); - if( (keyScalar && !Container< T_elem >::empty() && !scalar()) || (scalar() && !keyScalar) ) + if( (keyScalar && !T_container::empty() && !scalar()) || (scalar() && !keyScalar) ) throw std::runtime_error("A scalar component can not be contained at " "the same time as one or more regular components."); - mapped_type& ret = Container< T_elem >::operator[](key); + mapped_type& ret = T_container::operator[](key); if( keyScalar ) { get().m_containsScalar = true; @@ -234,11 +256,11 @@ BaseRecord< T_elem >::operator[](key_type&& key) else { bool const keyScalar = (key == RecordComponent::SCALAR); - if( (keyScalar && !Container< T_elem >::empty() && !scalar()) || (scalar() && !keyScalar) ) + if( (keyScalar && !T_container::empty() && !scalar()) || (scalar() && !keyScalar) ) throw std::runtime_error("A scalar component can not be contained at " "the same time as one or more regular components."); - mapped_type& ret = Container< T_elem >::operator[](std::move(key)); + mapped_type& ret = T_container::operator[](std::move(key)); if( keyScalar ) { get().m_containsScalar = true; @@ -255,7 +277,7 @@ BaseRecord< T_elem >::erase(key_type const& key) bool const keyScalar = (key == RecordComponent::SCALAR); size_type res; if( !keyScalar || (keyScalar && this->at(key).constant()) ) - res = Container< T_elem >::erase(key); + res = T_container::erase(key); else { mapped_type& rc = this->find(RecordComponent::SCALAR)->second; @@ -266,7 +288,7 @@ BaseRecord< T_elem >::erase(key_type const& key) this->IOHandler()->enqueue(IOTask(&rc, dDelete)); this->IOHandler()->flush(); } - res = Container< T_elem >::erase(key); + res = T_container::erase(key); } if( keyScalar ) @@ -285,7 +307,7 @@ BaseRecord< T_elem >::erase(iterator res) bool const keyScalar = (res->first == RecordComponent::SCALAR); iterator ret; if( !keyScalar || (keyScalar && this->at(res->first).constant()) ) - ret = Container< T_elem >::erase(res); + ret = T_container::erase(res); else { mapped_type& rc = this->find(RecordComponent::SCALAR)->second; @@ -296,7 +318,7 @@ BaseRecord< T_elem >::erase(iterator res) this->IOHandler()->enqueue(IOTask(&rc, dDelete)); this->IOHandler()->flush(); } - ret = Container< T_elem >::erase(res); + ret = T_container::erase(res); } if( keyScalar ) diff --git a/include/openPMD/backend/Container.hpp b/include/openPMD/backend/Container.hpp index fec80ebf5b..d74b1ca842 100644 --- a/include/openPMD/backend/Container.hpp +++ b/include/openPMD/backend/Container.hpp @@ -348,7 +348,7 @@ class Container : public T_AttributableBase OPENPMD_protected: Container( std::shared_ptr< DataClass > containerData ) - : T_AttributableBase{ containerData } + : T_AttributableBase( containerData ) , m_containerData{ std::move( containerData ) } { } @@ -376,6 +376,7 @@ class Container : public T_AttributableBase OPENPMD_private: Container() : T_AttributableBase{ nullptr } { + // @todo write Attributable::setData here and in other places? T_AttributableBase::setData( m_containerData ); } }; diff --git a/src/backend/PatchRecord.cpp b/src/backend/PatchRecord.cpp index 3926677a30..036932f9cc 100644 --- a/src/backend/PatchRecord.cpp +++ b/src/backend/PatchRecord.cpp @@ -43,7 +43,7 @@ PatchRecord::flush_impl(std::string const& path) if( this->find(RecordComponent::SCALAR) == this->end() ) { if(IOHandler()->m_frontendAccess != Access::READ_ONLY ) - Container< PatchRecordComponent >::flush(path); // warning (clang-tidy-10): bugprone-parent-virtual-call + T_container::flush(path); // warning (clang-tidy-10): bugprone-parent-virtual-call for( auto& comp : *this ) comp.second.flush(comp.first); } else