Skip to content

Commit

Permalink
Make RecordComponent a base class of BaseRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Feb 2, 2022
1 parent d000c57 commit e67aa3e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
11 changes: 7 additions & 4 deletions include/openPMD/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
80 changes: 51 additions & 29 deletions include/openPMD/backend/BaseRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -171,7 +194,6 @@ class BaseRecord : public Container<
dirtyRecursive() const;
}; // BaseRecord


// implementation

namespace internal
Expand All @@ -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 ) }
{
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 )
Expand All @@ -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;
Expand All @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion include/openPMD/backend/Container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) }
{
}
Expand Down Expand Up @@ -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 );
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/backend/PatchRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e67aa3e

Please sign in to comment.