12 #ifndef SST_CORE_SERIALIZATION_IMPL_SERIALIZE_UNIQUE_PTR_H 13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_UNIQUE_PTR_H 15 #ifndef SST_INCLUDING_SERIALIZE_H 17 "The header file sst/core/serialization/impl/serialize_unique_ptr.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serialize.h" 20 #include "sst/core/serialization/serializer.h" 25 #include <type_traits> 34 template <
class PTR_TYPE,
class DELETER = std::default_delete<PTR_TYPE>,
class SIZE_T =
void>
37 std::unique_ptr<PTR_TYPE, DELETER>& ptr;
42 template <
class ELEM_TYPE,
class DELETER,
class SIZE_T>
45 std::unique_ptr<ELEM_TYPE[], DELETER>& ptr;
53 template <
class PTR_TYPE>
54 class serialize_impl<std::unique_ptr<PTR_TYPE>, std::enable_if_t<!is_unbounded_array_v<PTR_TYPE>>>
56 void operator()(std::unique_ptr<PTR_TYPE>& ptr,
serializer& ser, ser_opt_t opt)
62 SST_FRIEND_SERIALIZE();
66 template <
class PTR_TYPE,
class DELETER,
class SIZE_T>
70 using OWNER_TYPE = std::remove_cv_t<PTR_TYPE>;
74 using ELEM_TYPE = std::remove_cv_t<std::remove_pointer_t<typename std::unique_ptr<PTR_TYPE, DELETER>::pointer>>;
78 const auto opts = SerOption::is_set(opt, SerOption::as_ptr_elem) ? SerOption::as_ptr : SerOption::none;
79 const auto mode = ser.mode();
81 if ( mode == serializer::MAP ) {
87 if ( mode == serializer::UNPACK ) ptr.ptr.~unique_ptr();
90 if constexpr ( is_unbounded_array_v<PTR_TYPE> ) {
94 if ( mode != serializer::UNPACK )
96 size = get_array_size(ptr.size,
97 "Serialization Error: Array size in SST::Core::Serialization::unique_ptr() cannot fit " 98 "inside size_t. size_t should be used for array sizes.\n");
102 if ( mode == serializer::UNPACK ) ptr.size =
static_cast<SIZE_T
>(size);
106 uintptr_t ptr_stored =
reinterpret_cast<uintptr_t
>(ptr.ptr.get());
107 ser.primitive(ptr_stored);
109 uintptr_t real_ptr = ptr_stored;
110 bool serialize_obj =
false;
113 case serializer::SIZER:
114 serialize_obj = ptr_stored && !ser.sizer().check_pointer_sizer(ptr_stored);
117 case serializer::PACK:
118 serialize_obj = ptr_stored && !ser.packer().check_pointer_pack(ptr_stored);
121 case serializer::UNPACK:
124 real_ptr = ser.unpacker().check_pointer_unpack(ptr_stored);
128 if constexpr ( is_unbounded_array_v<PTR_TYPE> )
129 real_ptr =
reinterpret_cast<uintptr_t
>(
new ELEM_TYPE[size]());
130 else if constexpr ( std::is_array_v<PTR_TYPE> )
131 real_ptr =
reinterpret_cast<uintptr_t
>(
new ELEM_TYPE[std::extent_v<PTR_TYPE>]());
133 real_ptr =
reinterpret_cast<uintptr_t
>(
new OWNER_TYPE());
134 ser.unpacker().report_real_pointer(ptr_stored, real_ptr);
135 serialize_obj =
true;
139 new (&ptr.ptr) std::unique_ptr<PTR_TYPE, DELETER>(
140 reinterpret_cast<ELEM_TYPE*>(real_ptr), std::forward<decltype(ptr.del)>(ptr.del));
148 if ( serialize_obj ) {
149 if constexpr ( !is_unbounded_array_v<PTR_TYPE> )
150 SST_SER(*reinterpret_cast<OWNER_TYPE*>(real_ptr));
151 else if constexpr ( is_trivially_serializable_v<ELEM_TYPE> )
152 ser.raw(reinterpret_cast<ELEM_TYPE*>(real_ptr), size *
sizeof(ELEM_TYPE));
154 pvt::serialize_array(
155 ser, reinterpret_cast<ELEM_TYPE*>(real_ptr), opts, size, pvt::serialize_array_element<ELEM_TYPE>);
162 SST_FRIEND_SERIALIZE();
166 template <
class ELEM_TYPE,
class DELETER,
class SIZE_T>
168 unique_ptr(std::unique_ptr<ELEM_TYPE[], DELETER>& ptr, SIZE_T& size)
170 return { ptr, size };
174 template <
class PTR_TYPE,
class DELETER,
class DEL>
175 std::enable_if_t<!is_unbounded_array_v<PTR_TYPE> && std::is_constructible_v<DELETER, DEL&&>,
176 pvt::unique_ptr_wrapper<PTR_TYPE, DELETER>>
177 unique_ptr(std::unique_ptr<PTR_TYPE, DELETER>& ptr, DEL&& del)
179 return { ptr, std::forward<DEL>(del) };
183 template <
class ELEM_TYPE,
class DELETER,
class SIZE_T,
class DEL>
184 std::enable_if_t<std::is_integral_v<SIZE_T> && std::is_constructible_v<DELETER, DEL&&>,
185 pvt::unique_ptr_wrapper<ELEM_TYPE[], DELETER, SIZE_T>>
186 unique_ptr(std::unique_ptr<ELEM_TYPE[], DELETER>& ptr, SIZE_T& size, DEL&& del)
188 return { ptr, size, std::forward<DEL>(del) };
192 template <
class PTR_TYPE,
class DELETER>
193 std::unique_ptr<PTR_TYPE, DELETER>&
194 unique_ptr(std::unique_ptr<PTR_TYPE, DELETER>& ptr)
201 #endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_UNIQUE_PTR_H This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
Base serialize class.
Definition: serialize.h:113
Definition: serialize_unique_ptr.h:35
Definition: serialize_unique_ptr.h:43