Skip to content

Commit e3d818a

Browse files
math::Vector<...> fix const storage handling
- add functor CopyElementWise<bool> - use CopyElementWise in implicit math::Vector copy constructor
1 parent 7f19215 commit e3d818a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/libPMacc/include/math/ConstVector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace PMACC_JOIN(pmacc_static_const_storage,id) \
5858
template<typename T_Type, int T_Dim> \
5959
struct ConstArrayStorage \
6060
{ \
61+
static const bool isConst = true; \
6162
typedef T_Type type; \
6263
static const int dim=T_Dim; \
6364
HDINLINE type& operator[](const int idx) \

src/libPMacc/include/math/vector/Vector.hpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace detail
4141
template<typename T_Type, int T_Dim>
4242
struct Vector_components
4343
{
44+
static const bool isConst = false;
4445
static const int dim = T_Dim;
4546
typedef T_Type type;
4647

@@ -60,6 +61,42 @@ struct Vector_components
6061
}
6162
};
6263

64+
65+
/** functor to copy a object element wise
66+
*
67+
* @tparam isDestConst define if destination is const (not copyable) object
68+
*/
69+
template<bool isDestConst>
70+
struct CopyElementWise
71+
{
72+
/** copy object element wise
73+
*
74+
* @tparam T_Dest destination object type
75+
* @tparam T_Src source object type
76+
*/
77+
template<typename T_Dest,typename T_Src>
78+
HDINLINE void operator()(T_Dest& dest,const T_Src& src) const
79+
{
80+
PMACC_CASSERT_MSG(CopyElementWise_destination_and_source_had_different_dimension,
81+
T_Dest::dim == T_Src::dim);
82+
for (int d = 0; d < T_Dest::dim; d++)
83+
dest[d] = src[d];
84+
}
85+
};
86+
87+
/** specialization for constant destination
88+
*
89+
* the constant storage is already available and set in the destination
90+
*/
91+
template<>
92+
struct CopyElementWise<true>
93+
{
94+
template<typename T_Dest,typename T_Src>
95+
HDINLINE void operator()(T_Dest& dest,const T_Src& src) const
96+
{
97+
}
98+
};
99+
63100
} //namespace detail
64101

65102
namespace tag
@@ -128,8 +165,7 @@ struct Vector : private T_Storage<T_Type, T_dim>, protected T_Accessor, protecte
128165

129166
HDINLINE Vector(const This& other)
130167
{
131-
for (int i = 0; i < dim; i++)
132-
(*this)[i] = other[i];
168+
detail::CopyElementWise<Storage::isConst>()(*this,other);
133169
}
134170

135171
template<

0 commit comments

Comments
 (0)