SST 12.1.0
Structural Simulation Toolkit
serialize_array.h
1// Copyright 2009-2022 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-2022, 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
17namespace SST {
18namespace Core {
19namespace Serialization {
20namespace pvt {
21
22template <class TPtr, class IntType>
24{
25public:
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
41template <class TPtr>
43{
44public:
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* */
52template <class TPtr, class IntType>
54array(TPtr*& buf, IntType& size)
55{
57}
58
59template <class IntType>
61buffer(void*& buf, IntType& size)
62{
64}
65
66template <class TPtr>
68raw_ptr(TPtr*& ptr)
69{
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 */
81template <class T, int N>
82class serialize<T[N], typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
83{
84public:
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 */
92template <class T, int N>
93class serialize<T[N], typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
94{
95public:
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 */
110template <class T, class IntType>
112 pvt::ser_array_wrapper<T, IntType>,
113 typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type>
114{
115public:
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 */
123template <class T, class IntType>
125 pvt::ser_array_wrapper<T, IntType>,
126 typename std::enable_if<!std::is_fundamental<T>::value && !std::is_enum<T>::value>::type>
127{
128public:
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 */
142template <class IntType>
143class serialize<pvt::ser_array_wrapper<void, IntType>>
144{
145public:
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 */
157template <class TPtr>
158class serialize<pvt::raw_ptr_wrapper<TPtr>>
159{
160public:
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
167template <class TPtr, class IntType>
168inline 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
177template <class TPtr>
178inline void
179operator&(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
Definition: serialize_array.h:43
Definition: serialize_array.h:24
Base serialize class.
Definition: serialize.h:32
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35