SST  9.0.0
StructuralSimulationToolkit
serialize_array.h
1 // Copyright 2009-2019 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-2019, 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 SERIALIZE_ARRAY_H
13 #define 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) :
29  bufptr(buf), sizeptr(size) {}
30 };
31 
32 // template <class IntType>
33 // class ser_buffer_wrapper
34 // {
35 // public:
36 // void*& bufptr;
37 // IntType& sizeptr;
38 // ser_buffer_wrapper(void*& buf, IntType& size) :
39 // bufptr(buf), sizeptr(size) {}
40 // };
41 
42 template <class TPtr>
44 {
45 public:
46  TPtr*& bufptr;
47  raw_ptr_wrapper(TPtr*& ptr) :
48  bufptr(ptr) {}
49 };
50 
51 }
52 /** I have typedefing pointers, but no other way.
53  * T could be "void and TPtr void* */
54 template <class TPtr, class IntType>
56 array(TPtr*& buf, IntType& size)
57 {
58  return pvt::ser_array_wrapper<TPtr,IntType>(buf, size);
59 }
60 
61 template <class IntType>
63 buffer(void*& buf, IntType& size)
64 {
65  return pvt::ser_array_wrapper<void,IntType>(buf, size);
66 }
67 
68 template <class TPtr>
70 raw_ptr(TPtr*& ptr)
71 {
72  return pvt::raw_ptr_wrapper<TPtr>(ptr);
73 }
74 
75 /***** Specializations of serialize class *****/
76 
77 /*** For statically allocated arrays ***/
78 
79 /**
80  Version of serialize that works for statically allocated arrays of
81  fundamental types and enums.
82  */
83 template <class T, int N>
84 class serialize<T[N], typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type> {
85 public:
86  void operator()(T arr[N], serializer& ser){
87  ser.array<T,N>(arr);
88  }
89 };
90 
91 /**
92  Version of serialize that works for statically allocated arrays of
93  non base types.
94  */
95 template <class T, int N>
96 class serialize<T[N], typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type> {
97  public:
98  void operator()(T arr[N], serializer& ser){
99  for ( int i = 0; i < N; i++ ) {
100  serialize<T>()(arr[i],ser);
101  }
102  }
103 };
104 
105 /*** For dynamically allocated arrays ***/
106 
107 /**
108  Version of serialize that works for dynamically allocated arrays of
109  fundamental types and enums.
110  */
111 template <class T, class IntType>
112 class serialize<pvt::ser_array_wrapper<T,IntType>, typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type> {
113 public:
114  void operator()(pvt::ser_array_wrapper<T,IntType> arr, serializer& ser) {
115  ser.binary(arr.bufptr, arr.sizeptr);
116  }
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<pvt::ser_array_wrapper<T,IntType>, typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type> {
125 public:
126  void operator()(pvt::ser_array_wrapper<T,IntType> arr, serializer& ser) {
127  ser.primitive(arr.sizeptr);
128  for ( int i = 0; i < arr.sizeptr; i++ ) {
129  serialize<T>()(arr[i],ser);
130  }
131  }
132 };
133 
134 /**
135  Version of serialize that works for statically allocated arrays of
136  void*.
137  */
138 template <class IntType>
139 class serialize<pvt::ser_array_wrapper<void,IntType> > {
140 public:
141  void operator()(pvt::ser_array_wrapper<void,IntType> arr, serializer& ser) {
142  ser.binary(arr.bufptr, arr.sizeptr);
143  }
144 };
145 
146 /*** Other Specializations (raw_ptr and trivially_serializable) ***/
147 
148 /**
149  Version of serialize that works for copying raw pointers (only
150  copying the value of the pointer. Note that this is only useful
151  if the value is going to be sent back to the originator, since it
152  won't be valid on the other rank.
153  */
154 template <class TPtr>
155 class serialize<pvt::raw_ptr_wrapper<TPtr> > {
156 public:
157  void operator()(pvt::raw_ptr_wrapper<TPtr> ptr, serializer& ser) {
158  ser.primitive(ptr.bufptr);
159  }
160 };
161 
162 
163 
164 
165 // Needed only because the default version in serialize.h can't get
166 // the template expansions quite right trying to look through several
167 // levels of expansion
168 template <class TPtr, class IntType>
169 inline void
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){
181 }
182 
183 }
184 }
185 }
186 #endif // 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
Definition: serialize_array.h:43
Base serialize class.
Definition: serialize.h:33
Definition: serialize_array.h:23