SST  11.1.0
StructuralSimulationToolkit
serialize_array.h
1 // Copyright 2009-2021 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-2021, 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<T[N], typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
83 {
84 public:
85  void operator()(T arr[N], serializer& ser) { ser.array<T, N>(arr); }
86 };
87 
88 /**
89  Version of serialize that works for statically allocated arrays of
90  non base types.
91  */
92 template <class T, int N>
93 class serialize<T[N], typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
94 {
95 public:
96  void operator()(T arr[N], serializer& ser)
97  {
98  for ( int i = 0; i < N; i++ ) {
99  serialize<T>()(arr[i], ser);
100  }
101  }
102 };
103 
104 /*** For dynamically allocated arrays ***/
105 
106 /**
107  Version of serialize that works for dynamically allocated arrays of
108  fundamental types and enums.
109  */
110 template <class T, class IntType>
111 class serialize<
112  pvt::ser_array_wrapper<T, IntType>,
113  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
114 {
115 public:
116  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
117 };
118 
119 /**
120  Version of serialize that works for dynamically allocated arrays of
121  non base types.
122  */
123 template <class T, class IntType>
124 class serialize<
125  pvt::ser_array_wrapper<T, IntType>,
126  typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
127 {
128 public:
129  void operator()(pvt::ser_array_wrapper<T, IntType> arr, serializer& ser)
130  {
131  ser.primitive(arr.sizeptr);
132  for ( int i = 0; i < arr.sizeptr; i++ ) {
133  serialize<T>()(arr[i], ser);
134  }
135  }
136 };
137 
138 /**
139  Version of serialize that works for statically allocated arrays of
140  void*.
141  */
142 template <class IntType>
143 class serialize<pvt::ser_array_wrapper<void, IntType>>
144 {
145 public:
146  void operator()(pvt::ser_array_wrapper<void, IntType> arr, serializer& ser) { ser.binary(arr.bufptr, arr.sizeptr); }
147 };
148 
149 /*** Other Specializations (raw_ptr and trivially_serializable) ***/
150 
151 /**
152  Version of serialize that works for copying raw pointers (only
153  copying the value of the pointer. Note that this is only useful
154  if the value is going to be sent back to the originator, since it
155  won't be valid on the other rank.
156  */
157 template <class TPtr>
158 class serialize<pvt::raw_ptr_wrapper<TPtr>>
159 {
160 public:
161  void operator()(pvt::raw_ptr_wrapper<TPtr> ptr, serializer& ser) { ser.primitive(ptr.bufptr); }
162 };
163 
164 // Needed only because the default version in serialize.h can't get
165 // the template expansions quite right trying to look through several
166 // levels of expansion
167 template <class TPtr, class IntType>
168 inline void
170 {
172 }
173 
174 // Needed only because the default version in serialize.h can't get
175 // the template expansions quite right trying to look through several
176 // levels of expansion
177 template <class TPtr>
178 inline void
179 operator&(serializer& ser, pvt::raw_ptr_wrapper<TPtr> ptr)
180 {
182 }
183 
184 } // namespace Serialization
185 } // namespace Core
186 } // namespace SST
187 
188 #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:34
Definition: serialize_array.h:42
Base serialize class.
Definition: serialize.h:31
Definition: serialize_array.h:23