Skip to content

Commit 29d6894

Browse files
Allow a trivially copyable type to opt out of the byte copy
The specialisations of Serialise are selected by conditions assumed to be mutually exclusive, so a type that satisfies two of them makes Serialise<T> ambiguous rather than preferring one. That makes the trivially copyable specialisation unreachable to opt out of: a code generator emitting plain structs of scalars cannot supply the encoding and type identity those messages carry, because its partial specialisation ties with the byte copy and neither is more specialised than the other. A full specialisation would win outright, but that is one specialisation per type rather than one covering everything a generator emits. Route the condition through a serialise_trivially trait so it can be specialised to false. The second parameter is an SFINAE slot, so a family of types can be opted out with a single partial specialisation. The default is std::is_trivially_copyable, so behaviour is unchanged for every type that does not opt out. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 822f036 commit 29d6894

2 files changed

Lines changed: 156 additions & 2 deletions

File tree

src/util/serialise/Serialise.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,26 @@ namespace util {
5151
template <typename T, typename Check = T>
5252
struct Serialise;
5353

54+
/**
55+
* Whether a type should be serialised by copying its bytes.
56+
*
57+
* The specialisations of Serialise below are selected by conditions that are assumed to be mutually exclusive,
58+
* so a type satisfying two of them makes Serialise<T> ambiguous rather than preferring one of them. Specialise
59+
* this trait to false for a type that is trivially copyable but carries its own serialisation, which is
60+
* otherwise unreachable: the byte copy is selected alongside it, and hash() would identify the type by its
61+
* demangled C++ name rather than by whatever identity the type defines for itself.
62+
*
63+
* The second parameter is an SFINAE slot, so a family of types can be opted out with a single partial
64+
* specialisation rather than one specialisation per type.
65+
*
66+
* @tparam T The type to check
67+
*/
68+
template <typename T, typename = void>
69+
struct serialise_trivially : std::is_trivially_copyable<T> {};
70+
5471
// Trivially copyable data
5572
template <typename T>
56-
struct Serialise<T, std::enable_if_t<std::is_trivially_copyable<T>::value, T>> {
73+
struct Serialise<T, std::enable_if_t<serialise_trivially<T>::value, T>> {
5774

5875
static std::vector<uint8_t> serialise(const T& in) {
5976
std::vector<uint8_t> out(sizeof(T));
@@ -81,7 +98,7 @@ namespace util {
8198
using iterator_value_type_t =
8299
typename std::iterator_traits<decltype(std::begin(std::declval<T>()))>::value_type;
83100
template <typename T>
84-
struct Serialise<T, std::enable_if_t<std::is_trivially_copyable<iterator_value_type_t<T>>::value, T>> {
101+
struct Serialise<T, std::enable_if_t<serialise_trivially<iterator_value_type_t<T>>::value, T>> {
85102

86103
using V = std::remove_reference_t<iterator_value_type_t<T>>;
87104

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2015 NUClear Contributors
5+
*
6+
* This file is part of the NUClear codebase.
7+
* See https://github.com/Fastcode/NUClear for further info.
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15+
* Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include <catch2/catch_test_macros.hpp>
24+
#include <cstdint>
25+
#include <type_traits>
26+
#include <vector>
27+
28+
#include "util/serialise/Serialise.hpp"
29+
30+
namespace {
31+
32+
/**
33+
* A type that is trivially copyable but carries its own wire format.
34+
*
35+
* A code generator's message class is the motivating case: it is a plain struct of scalars, so it is trivially
36+
* copyable, yet it has an encoding and a type identity of its own that a byte copy would bypass.
37+
*/
38+
struct Encoded {
39+
uint16_t value{0};
40+
};
41+
42+
/// Marks the family of types that carry their own encoding, as a generator would for everything it emits
43+
template <typename T>
44+
struct is_encoded : std::is_same<T, Encoded> {};
45+
46+
/// The identity these types define for themselves, which is not their demangled C++ name
47+
constexpr uint64_t ENCODED_HASH = 0x0123456789ABCDEFULL;
48+
49+
/// The wire format these types define for themselves, which is not their object representation
50+
constexpr uint8_t ENCODED_PREFIX = 0xAB;
51+
52+
} // namespace
53+
54+
namespace NUClear {
55+
namespace util {
56+
namespace serialise {
57+
58+
/**
59+
* Opt the family out of the byte copy.
60+
*
61+
* Without this the partial specialisation below is ambiguous with the trivially copyable one: neither is more
62+
* specialised than the other, so the two are equally good matches and the instantiation is ill-formed. A full
63+
* specialisation would win outright, but that is one specialisation per type rather than one for everything a
64+
* generator emits.
65+
*/
66+
template <typename T>
67+
struct serialise_trivially<T, std::enable_if_t<is_encoded<T>::value>> : std::false_type {};
68+
69+
template <typename T>
70+
struct Serialise<T, std::enable_if_t<is_encoded<T>::value, T>> {
71+
static std::vector<uint8_t> serialise(const T& in) {
72+
return {ENCODED_PREFIX, static_cast<uint8_t>(in.value >> 8), static_cast<uint8_t>(in.value & 0xFF)};
73+
}
74+
75+
static T deserialise(const std::vector<uint8_t>& in) {
76+
if (in.size() != 3 || in[0] != ENCODED_PREFIX) {
77+
throw std::length_error("Not an encoded value");
78+
}
79+
return T{static_cast<uint16_t>((uint16_t(in[1]) << 8) | in[2])};
80+
}
81+
82+
static uint64_t hash() {
83+
return ENCODED_HASH;
84+
}
85+
};
86+
87+
} // namespace serialise
88+
} // namespace util
89+
} // namespace NUClear
90+
91+
SCENARIO("A trivially copyable type can provide its own serialisation", "[util][serialise][trivially]") {
92+
93+
GIVEN("a trivially copyable type that has opted out of the byte copy") {
94+
STATIC_REQUIRE(std::is_trivially_copyable<Encoded>::value);
95+
STATIC_REQUIRE_FALSE(NUClear::util::serialise::serialise_trivially<Encoded>::value);
96+
97+
const Encoded in{0x1234};
98+
99+
WHEN("it is serialised") {
100+
const auto serialised = NUClear::util::serialise::Serialise<Encoded>::serialise(in);
101+
102+
THEN("its own wire format is used rather than its object representation") {
103+
REQUIRE(serialised == std::vector<uint8_t>{ENCODED_PREFIX, 0x12, 0x34});
104+
}
105+
}
106+
107+
WHEN("it is round tripped") {
108+
const auto serialised = NUClear::util::serialise::Serialise<Encoded>::serialise(in);
109+
const auto deserialised = NUClear::util::serialise::Serialise<Encoded>::deserialise(serialised);
110+
111+
THEN("the deserialised value is the same as the input") {
112+
REQUIRE(deserialised.value == in.value);
113+
}
114+
}
115+
116+
WHEN("its hash is taken") {
117+
THEN("it is the identity the type defines rather than one derived from its C++ name") {
118+
REQUIRE(NUClear::util::serialise::Serialise<Encoded>::hash() == ENCODED_HASH);
119+
}
120+
}
121+
}
122+
123+
GIVEN("a trivially copyable type that has not opted out") {
124+
STATIC_REQUIRE(NUClear::util::serialise::serialise_trivially<uint32_t>::value);
125+
126+
const uint32_t in = 0xCAFEFECA; // Mirrored so that endianess doesn't matter for the test
127+
128+
WHEN("it is serialised") {
129+
const auto serialised = NUClear::util::serialise::Serialise<uint32_t>::serialise(in);
130+
131+
THEN("it still uses the byte copy") {
132+
REQUIRE(serialised.size() == sizeof(uint32_t));
133+
REQUIRE(serialised == std::vector<uint8_t>{0xCA, 0xFE, 0xFE, 0xCA});
134+
}
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)