SST  14.1.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_IMPL_SERIALIZE_ARRAY_H
13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_ARRAY_H
14 
15 #ifndef SST_INCLUDING_SERIALIZE_H
16 #warning \
17  "The header file sst/core/serialization/impl/serialize_array.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 namespace SST {
23 namespace Core {
24 namespace Serialization {
25 namespace pvt {
26 
27 template <class TPtr, class IntType>
29 {
30 public:
31  TPtr*& bufptr;
32  IntType& sizeptr;
33  ser_array_wrapper(TPtr*& buf, IntType& size) : bufptr(buf), sizeptr(size) {}
34 };
35 
36 // template <class IntType>
37 // class ser_buffer_wrapper
38 // {
39 // public:
40 // void*& bufptr;
41 // IntType& sizeptr;
42 // ser_buffer_wrapper(void*& buf, IntType& size) :
43 // bufptr(buf), sizeptr(size) {}
44 // };
45 
46 template <class TPtr>
48 {
49 public:
50  TPtr*& bufptr;
51  raw_ptr_wrapper(TPtr*& ptr) : bufptr(ptr) {}
52 };
53 
54 } // namespace pvt
55 /** I have typedefing pointers, but no other way.
56  * T could be "void and TPtr void* */
57 template <class TPtr, class IntType>
59 array(TPtr*& buf, IntType& size)
60 {
61  return pvt::ser_array_wrapper<TPtr, IntType>(buf, size);
62 }
63 
64 template <class IntType>
66 buffer(void*& buf, IntType& size)
67 {
68  return pvt::ser_array_wrapper<void, IntType>(buf, size);
69 }
70 
71 template <class TPtr>
73 raw_ptr(TPtr*& ptr)
74 {
75  return pvt::raw_ptr_wrapper<TPtr>(ptr);
76 }
77 
78 /***** Specializations of serialize class *****/
79 
80 /*** For statically allocated arrays ***/
81 
82 /**
83  Version of serialize that works for statically allocated arrays of
84  fundamental types and enums.
85  */
86 template <class T, int N>
87 class serialize_impl<T[N], typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
88 {
89  template <class A>
90  friend class serialize;
91  void operator()(T arr[N], serializer& ser) { ser.array<T, N>(arr); }
92 
93  void operator()(T UNUSED(arr[N]), serializer& UNUSED(ser), const char* UNUSED(name))
94  {
95  // TODO: Implement mapping mode
96  }
97 };
98 
99 /**
100  Version of serialize that works for statically allocated arrays of
101  non base types.
102  */
103 template <class T, int N>
104 class serialize_impl<T[N], typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
105 {
106  template <class A>
107  friend class serialize;
108  void operator()(T arr[N], serializer& ser)
109  {
110  for ( int i = 0; i < N; i++ ) {
111  ser& arr[i];
112  }
113  }
114 
115  void operator()(T UNUSED(arr[N]), serializer& UNUSED(ser), const char* UNUSED(name))
116  {
117  // TODO: Implement mapping mode
118  }
119 };
120 
121 /*** For dynamically allocated arrays ***/
122 
123 /**
124  Version of serialize that works for dynamically allocated arrays of
125  fundamental types and enums.
126  */
127 template <class T, class IntType>
129  pvt::ser_array_wrapper<T, IntType>,
130  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
131 {
132  template <class A>
133  friend class serialize;
134  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
135 
136  void operator()(pvt::ser_array_wrapper<T, IntType> UNUSED(arr), serializer& UNUSED(ser), const char* UNUSED(name))
137  {
138  // TODO: Implement mapping mode
139  }
140 };
141 
142 /**
143  Version of serialize that works for dynamically allocated arrays of
144  non base types.
145  */
146 template <class T, class IntType>
148  pvt::ser_array_wrapper<T, IntType>,
149  typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
150 {
151  template <class A>
152  friend class serialize;
153  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser)
154  {
155  ser.primitive(arr.sizeptr);
156  for ( int i = 0; i < arr.sizeptr; i++ ) {
157  ser& arr[i];
158  }
159  }
160 
161  void operator()(pvt::ser_array_wrapper<T, IntType> UNUSED(arr), serializer& UNUSED(ser), const char* UNUSED(name))
162  {
163  // TODO: Implement mapping mode
164  }
165 };
166 
167 /**
168  Version of serialize that works for statically allocated arrays of
169  void*.
170  */
171 template <class IntType>
172 class serialize_impl<pvt::ser_array_wrapper<void, IntType>>
173 {
174  template <class A>
175  friend class serialize;
176  void operator()(pvt::ser_array_wrapper<void, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
177 
178  void
179  operator()(pvt::ser_array_wrapper<void, IntType> UNUSED(arr), serializer& UNUSED(ser), const char* UNUSED(name))
180  {
181  // TODO: Implement mapping mode
182  }
183 };
184 
185 /*** Other Specializations (raw_ptr and trivially_serializable) ***/
186 
187 /**
188  Version of serialize that works for copying raw pointers (only
189  copying the value of the pointer. Note that this is only useful
190  if the value is going to be sent back to the originator, since it
191  won't be valid on the other rank.
192  */
193 template <class TPtr>
194 class serialize_impl<pvt::raw_ptr_wrapper<TPtr>>
195 {
196  template <class A>
197  friend class serialize;
198  void operator()(pvt::raw_ptr_wrapper<TPtr> ptr, serializer& ser) { ser.primitive(ptr.bufptr); }
199 
200  void operator()(pvt::raw_ptr_wrapper<TPtr> UNUSED(ptr), serializer& UNUSED(ser), const char* UNUSED(name))
201  {
202  // TODO: Implement mapping mode
203  }
204 };
205 
206 // Needed only because the default version in serialize.h can't get
207 // the template expansions quite right trying to look through several
208 // levels of expansion
209 template <class TPtr, class IntType>
210 inline void
212 {
213  operator&<pvt::ser_array_wrapper<TPtr, IntType>>(ser, arr);
214  // serialize<pvt::ser_array_wrapper<TPtr, IntType>>()(arr, ser);
215 }
216 
217 // Needed only because the default version in serialize.h can't get
218 // the template expansions quite right trying to look through several
219 // levels of expansion
220 template <class TPtr>
221 inline void
222 operator&(serializer& ser, pvt::raw_ptr_wrapper<TPtr> ptr)
223 {
224  operator&<pvt::raw_ptr_wrapper<TPtr>>(ser, ptr);
225  // serialize<pvt::raw_ptr_wrapper<TPtr>>()(ptr, ser);
226 }
227 
228 } // namespace Serialization
229 } // namespace Core
230 } // namespace SST
231 
232 #endif // SST_CORE_SERIALIZATION_IMPL_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:43
Base serialize class.
Definition: serialize.h:45
Definition: action.cc:18
Definition: serialize_array.h:47
Serialization "gateway" object.
Definition: serialize.h:133
Definition: serialize_array.h:28