SST  14.0.0
StructuralSimulationToolkit
serialize_array.h
1 // Copyright 2009-2024 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-2024, 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_SERIALIZE_ARRAY_H
13 #define SST_CORE_SERIALIZATION_SERIALIZE_ARRAY_H
14 
15 #include "sst/core/serialization/serializer.h"
16 
17 namespace SST {
18 namespace Core {
19 namespace Serialization {
20 namespace pvt {
21 
22 template <class TPtr, class IntType>
24 {
25 public:
26  TPtr*& bufptr;
27  IntType& sizeptr;
28  ser_array_wrapper(TPtr*& buf, IntType& size) : bufptr(buf), sizeptr(size) {}
29 };
30 
31 // template <class IntType>
32 // class ser_buffer_wrapper
33 // {
34 // public:
35 // void*& bufptr;
36 // IntType& sizeptr;
37 // ser_buffer_wrapper(void*& buf, IntType& size) :
38 // bufptr(buf), sizeptr(size) {}
39 // };
40 
41 template <class TPtr>
43 {
44 public:
45  TPtr*& bufptr;
46  raw_ptr_wrapper(TPtr*& ptr) : bufptr(ptr) {}
47 };
48 
49 } // namespace pvt
50 /** I have typedefing pointers, but no other way.
51  * T could be "void and TPtr void* */
52 template <class TPtr, class IntType>
54 array(TPtr*& buf, IntType& size)
55 {
56  return pvt::ser_array_wrapper<TPtr, IntType>(buf, size);
57 }
58 
59 template <class IntType>
61 buffer(void*& buf, IntType& size)
62 {
63  return pvt::ser_array_wrapper<void, IntType>(buf, size);
64 }
65 
66 template <class TPtr>
68 raw_ptr(TPtr*& ptr)
69 {
70  return pvt::raw_ptr_wrapper<TPtr>(ptr);
71 }
72 
73 /***** Specializations of serialize class *****/
74 
75 /*** For statically allocated arrays ***/
76 
77 /**
78  Version of serialize that works for statically allocated arrays of
79  fundamental types and enums.
80  */
81 template <class T, int N>
82 class serialize_impl<T[N], typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
83 {
84  template <class A>
85  friend class serialize;
86  void operator()(T arr[N], serializer& ser) { ser.array<T, N>(arr); }
87 };
88 
89 /**
90  Version of serialize that works for statically allocated arrays of
91  non base types.
92  */
93 template <class T, int N>
94 class serialize_impl<T[N], typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
95 {
96  template <class A>
97  friend class serialize;
98  void operator()(T arr[N], serializer& ser)
99  {
100  for ( int i = 0; i < N; i++ ) {
101  ser& arr[i];
102  }
103  }
104 };
105 
106 /*** For dynamically allocated arrays ***/
107 
108 /**
109  Version of serialize that works for dynamically allocated arrays of
110  fundamental types and enums.
111  */
112 template <class T, class IntType>
114  pvt::ser_array_wrapper<T, IntType>,
115  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
116 {
117  template <class A>
118  friend class serialize;
119  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
120 };
121 
122 /**
123  Version of serialize that works for dynamically allocated arrays of
124  non base types.
125  */
126 template <class T, class IntType>
128  pvt::ser_array_wrapper<T, IntType>,
129  typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
130 {
131  template <class A>
132  friend class serialize;
133  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser)
134  {
135  ser.primitive(arr.sizeptr);
136  for ( int i = 0; i < arr.sizeptr; i++ ) {
137  ser& arr[i];
138  }
139  }
140 };
141 
142 /**
143  Version of serialize that works for statically allocated arrays of
144  void*.
145  */
146 template <class IntType>
147 class serialize_impl<pvt::ser_array_wrapper<void, IntType>>
148 {
149  template <class A>
150  friend class serialize;
151  void operator()(pvt::ser_array_wrapper<void, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
152 };
153 
154 /*** Other Specializations (raw_ptr and trivially_serializable) ***/
155 
156 /**
157  Version of serialize that works for copying raw pointers (only
158  copying the value of the pointer. Note that this is only useful
159  if the value is going to be sent back to the originator, since it
160  won't be valid on the other rank.
161  */
162 template <class TPtr>
163 class serialize_impl<pvt::raw_ptr_wrapper<TPtr>>
164 {
165  template <class A>
166  friend class serialize;
167  void operator()(pvt::raw_ptr_wrapper<TPtr> ptr, serializer& ser) { ser.primitive(ptr.bufptr); }
168 };
169 
170 // Needed only because the default version in serialize.h can't get
171 // the template expansions quite right trying to look through several
172 // levels of expansion
173 template <class TPtr, class IntType>
174 inline void
176 {
177  operator&<pvt::ser_array_wrapper<TPtr, IntType>>(ser, arr);
178  // serialize<pvt::ser_array_wrapper<TPtr, IntType>>()(arr, ser);
179 }
180 
181 // Needed only because the default version in serialize.h can't get
182 // the template expansions quite right trying to look through several
183 // levels of expansion
184 template <class TPtr>
185 inline void
186 operator&(serializer& ser, pvt::raw_ptr_wrapper<TPtr> ptr)
187 {
188  operator&<pvt::raw_ptr_wrapper<TPtr>>(ser, ptr);
189  // serialize<pvt::raw_ptr_wrapper<TPtr>>()(ptr, ser);
190 }
191 
192 } // namespace Serialization
193 } // namespace Core
194 } // namespace SST
195 
196 #endif // SST_CORE_SERIALIZATION_SERIALIZE_ARRAY_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Base serialize class.
Definition: serialize.h:32
Definition: action.cc:18
Definition: serialize_array.h:42
Serialization "gateway" object.
Definition: serialize.h:110
Definition: serialize_array.h:23