Skip to content

Commit

Permalink
Avoid object slicing when deriving from Series class (openPMD#1107)
Browse files Browse the repository at this point in the history
* Make Series class final

* Use private constructor to avoid object slicing
  • Loading branch information
franzpoeschel authored and ax3l committed Nov 3, 2021
1 parent a52966b commit bea0c0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ class Series : public SeriesInterface
private:
std::shared_ptr< internal::SeriesInternal > m_series;

// constructor from private parts
Series( std::shared_ptr< internal::SeriesInternal > );

public:
explicit Series();

Expand Down
13 changes: 12 additions & 1 deletion src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,15 @@ SeriesInternal::~SeriesInternal()
}
} // namespace internal

Series::Series( std::shared_ptr< internal::SeriesInternal > series_in )
: SeriesInterface{
series_in.get(),
static_cast< internal::AttributableData * >( series_in.get() ) }
, m_series{ std::move( series_in ) }
, iterations{ m_series->iterations }
{
}

Series::Series() : SeriesInterface{ nullptr, nullptr }, iterations{}
{
}
Expand Down Expand Up @@ -1517,7 +1526,9 @@ Series::operator bool() const

ReadIterations Series::readIterations()
{
return { *this };
// Use private constructor instead of copy constructor to avoid
// object slicing
return { this->m_series };
}

WriteIterations
Expand Down

0 comments on commit bea0c0c

Please sign in to comment.