Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid object slicing when deriving from Series class #1107

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,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 @@ -1528,6 +1528,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 @@ -1570,7 +1579,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