Skip to content

Commit

Permalink
Avoid object slicing when using records as scalar components
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Aug 11, 2023
1 parent 6e559a1 commit 05bd135
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/openPMD/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ namespace internal
class BaseRecordData;
} // namespace internal

template <typename>
class BaseRecord;

class RecordComponent : public BaseRecordComponent
{
template <typename T, typename T_key, typename T_container>
Expand All @@ -134,6 +137,15 @@ class RecordComponent : public BaseRecordComponent
AUTO
}; // Allocation

/**
* @brief Avoid object slicing when using a Record as a scalar Record
* Component.
*
* It's still preferred to directly use the Record, or alternatively a
* Record-Component-type reference to a Record.
*/
RecordComponent(BaseRecord<RecordComponent> const &);

RecordComponent &setUnitSI(double);

/**
Expand Down
3 changes: 3 additions & 0 deletions include/openPMD/backend/BaseRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ class BaseRecord
: public Container<T_elem>
, public T_elem // T_RecordComponent
{
public:
using T_RecordComponent = T_elem;
using T_Container = Container<T_elem>;

private:
using T_Self = BaseRecord<T_elem>;
friend class Iteration;
friend class ParticleSpecies;
Expand Down
3 changes: 3 additions & 0 deletions include/openPMD/backend/BaseRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ namespace internal
};
} // namespace internal

template <typename>
class BaseRecord;

class BaseRecordComponent : virtual public Attributable
{
template <typename T, typename T_key, typename T_container>
Expand Down
9 changes: 9 additions & 0 deletions include/openPMD/backend/MeshRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class MeshRecordComponent : public RecordComponent
public:
~MeshRecordComponent() override = default;

/**
* @brief Avoid object slicing when using a Record as a scalar Record
* Component.
*
* It's still preferred to directly use the Record, or alternatively a
* Record-Component-type reference to a Record.
*/
MeshRecordComponent(BaseRecord<MeshRecordComponent> const &);

/** Position on an element
*
* Relative on an element (node/cell/voxel) of the mesh
Expand Down
9 changes: 9 additions & 0 deletions include/openPMD/backend/PatchRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ class PatchRecordComponent : public BaseRecordComponent
friend class internal::PatchRecordComponentData;

public:
/**
* @brief Avoid object slicing when using a Record as a scalar Record
* Component.
*
* It's still preferred to directly use the Record, or alternatively a
* Record-Component-type reference to a Record.
*/
PatchRecordComponent(BaseRecord<PatchRecordComponent> const &);

PatchRecordComponent &setUnitSI(double);

virtual PatchRecordComponent &resetDataset(Dataset);
Expand Down
7 changes: 7 additions & 0 deletions src/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "openPMD/IO/Format.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/Memory.hpp"
#include "openPMD/backend/BaseRecord.hpp"

#include <algorithm>
#include <climits>
Expand All @@ -48,6 +49,12 @@ RecordComponent::RecordComponent() : BaseRecordComponent(NoInit())
RecordComponent::RecordComponent(NoInit) : BaseRecordComponent(NoInit())
{}

RecordComponent::RecordComponent(BaseRecord<RecordComponent> const &baseRecord)
: BaseRecordComponent(NoInit())
{
setData(baseRecord.m_recordComponentData);
}

// We need to instantiate this somewhere otherwise there might be linker issues
// despite this thing actually being constepxr
constexpr char const *const RecordComponent::SCALAR;
Expand Down
8 changes: 8 additions & 0 deletions src/backend/MeshRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/backend/MeshRecordComponent.hpp"
#include "openPMD/backend/BaseRecord.hpp"

namespace openPMD
{
Expand All @@ -28,6 +29,13 @@ MeshRecordComponent::MeshRecordComponent() : RecordComponent()
MeshRecordComponent::MeshRecordComponent(NoInit) : RecordComponent(NoInit())
{}

MeshRecordComponent::MeshRecordComponent(
BaseRecord<MeshRecordComponent> const &baseRecord)
: RecordComponent(NoInit())
{
setData(baseRecord.m_recordComponentData);
}

void MeshRecordComponent::read()
{
using DT = Datatype;
Expand Down
8 changes: 8 additions & 0 deletions src/backend/PatchRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
#include "openPMD/backend/PatchRecordComponent.hpp"
#include "openPMD/auxiliary/Memory.hpp"
#include "openPMD/backend/BaseRecord.hpp"

#include <algorithm>

Expand Down Expand Up @@ -74,6 +75,13 @@ Extent PatchRecordComponent::getExtent() const
}
}

PatchRecordComponent::PatchRecordComponent(
BaseRecord<PatchRecordComponent> const &baseRecord)
: BaseRecordComponent(NoInit())
{
setData(baseRecord.m_patchRecordComponentData);
}

PatchRecordComponent::PatchRecordComponent() : BaseRecordComponent(NoInit())
{
setData(std::make_shared<Data_t>());
Expand Down

0 comments on commit 05bd135

Please sign in to comment.