Skip to content

Commit c5adebf

Browse files
authored
Serialize std::bitset (#1348)
1 parent f50b69c commit c5adebf

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/sst/core/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ nobase_dist_sst_HEADERS = \
101101
serialization/impl/serialize_adapter.h \
102102
serialization/impl/serialize_array.h \
103103
serialization/impl/serialize_atomic.h \
104+
serialization/impl/serialize_bitset.h \
104105
serialization/impl/ser_buffer_accessor.h \
105106
serialization/impl/serialize_insertable.h \
106107
serialization/impl/serialize_optional.h \
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2009-2025 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-2025, 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_BITSET_H
13+
#define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_BITSET_H
14+
15+
#ifndef SST_INCLUDING_SERIALIZE_H
16+
#warning \
17+
"The header file sst/core/serialization/impl/serialize_bitset.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+
#include <bitset>
23+
#include <cstddef>
24+
#include <type_traits>
25+
26+
namespace SST::Core::Serialization {
27+
28+
// Serialize std::bitset
29+
template <size_t N>
30+
class serialize_impl<std::bitset<N>>
31+
{
32+
using T = std::bitset<N>;
33+
void operator()(T& t, serializer& ser, ser_opt_t UNUSED(options))
34+
{
35+
switch ( ser.mode() ) {
36+
case serializer::MAP:
37+
{
38+
// TODO: Should this be mapped as an array? It will have the same problems as std::vector<bool>
39+
break;
40+
}
41+
42+
default:
43+
static_assert(std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>);
44+
ser.primitive(t);
45+
break;
46+
}
47+
}
48+
SST_FRIEND_SERIALIZE();
49+
};
50+
51+
template <size_t N>
52+
class serialize_impl<std::bitset<N>*>
53+
{
54+
using T = std::bitset<N>;
55+
void operator()(T*& t, serializer& ser, ser_opt_t UNUSED(options))
56+
{
57+
if ( ser.mode() == serializer::UNPACK ) t = new T {};
58+
SST_SER(*t);
59+
}
60+
SST_FRIEND_SERIALIZE();
61+
};
62+
63+
} // namespace SST::Core::Serialization
64+
65+
#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_BITSET_H

src/sst/core/serialization/serialize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ sst_ser_or_helper(Args... args)
512512
#include "sst/core/serialization/impl/serialize_adapter.h"
513513
#include "sst/core/serialization/impl/serialize_array.h"
514514
#include "sst/core/serialization/impl/serialize_atomic.h"
515+
#include "sst/core/serialization/impl/serialize_bitset.h"
515516
#include "sst/core/serialization/impl/serialize_insertable.h"
516517
#include "sst/core/serialization/impl/serialize_optional.h"
517518
#include "sst/core/serialization/impl/serialize_string.h"

src/sst/core/testElements/coreTest_Serialization.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "sst/core/warnmacros.h"
2525

2626
#include <array>
27+
#include <bitset>
2728
#include <deque>
2829
#include <forward_list>
2930
#include <list>
@@ -660,6 +661,11 @@ coreTestSerialization::coreTestSerialization(ComponentId_t id, Params& params) :
660661
checkSimpleSerializeDeserialize<double>::check_all(rng->nextUniform() * 1000000, out, "double");
661662
checkSimpleSerializeDeserialize<std::string>::check_all("test_string", out, "std::string");
662663

664+
checkSimpleSerializeDeserialize<std::bitset<1>>::check_all(rng->generateNextUInt64(), out, "std::bitset<1>");
665+
checkSimpleSerializeDeserialize<std::bitset<10>>::check_all(rng->generateNextUInt64(), out, "std::bitset<10>");
666+
checkSimpleSerializeDeserialize<std::bitset<100>>::check_all(
667+
rng->generateNextUInt64(), out, "std::bitset<100>");
668+
663669
passed = checkSimpleSerializeDeserialize<std::tuple<int32_t, int32_t, int32_t>>::check(
664670
std::tuple<int32_t, int32_t, int32_t>(
665671
rng->generateNextInt32(), rng->generateNextInt32(), rng->generateNextInt32()));

0 commit comments

Comments
 (0)