SST  15.1.0
StructuralSimulationToolkit
serialize_valarray.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VALARRAY_H
13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VALARRAY_H
14 
15 #ifndef SST_INCLUDING_SERIALIZE_H
16 #warning \
17  "The header file sst/core/serialization/impl/serialize_valarray.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"
18 #endif
19 
20 #include "sst/core/serialization/serializer.h"
21 
22 #include <string>
23 #include <type_traits>
24 #include <valarray>
25 
26 namespace SST::Core::Serialization {
27 
28 // Whether a type is a std::valarray type
29 template <typename>
30 constexpr bool is_valarray_v = false;
31 
32 // We must make sure that T is trivially copyable and standard layout type, although this is almost always true
33 template <typename T>
34 constexpr bool is_valarray_v<std::valarray<T>> = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
35 
36 // Serialize std::valarray and pointers to std::valarray
37 template <typename T>
38 class serialize_impl<T, std::enable_if_t<is_valarray_v<std::remove_pointer_t<T>>>>
39 {
40  void operator()(T& obj, serializer& ser, ser_opt_t UNUSED(options))
41  {
42  const auto& objPtr = get_ptr(obj);
43  size_t size = 0;
44  switch ( ser.mode() ) {
45  case serializer::SIZER:
46  size = objPtr->size();
47  ser.size(size);
48  break;
49 
50  case serializer::PACK:
51  size = objPtr->size();
52  ser.pack(size);
53  break;
54 
55  case serializer::UNPACK:
56  ser.unpack(size);
57  if constexpr ( std::is_pointer_v<T> )
58  obj = new std::remove_pointer_t<T>(size);
59  else
60  obj.resize(size);
61  break;
62 
63  case serializer::MAP:
64  size = objPtr->size();
65  ser.mapper().map_hierarchy_start(
66  ser.getMapName(), new ObjectMapContainer<std::remove_pointer_t<T>>(objPtr));
67  for ( size_t i = 0; i < size; ++i )
68  SST_SER_NAME((*objPtr)[i], std::to_string(i).c_str());
69  ser.mapper().map_hierarchy_end();
70  return;
71  }
72  ser.raw(&(*objPtr)[0], size * sizeof((*objPtr)[0]));
73  }
74 
75  SST_FRIEND_SERIALIZE();
76 };
77 
78 } // namespace SST::Core::Serialization
79 
80 #endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VALARRAY_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
Class used to map containers.
Definition: objectMap.h:1457
Base serialize class.
Definition: serialize.h:113