Skip to content

Commit 4cf8d2e

Browse files
committed
Work on serializaing multiple arguments
1 parent 5561d13 commit 4cf8d2e

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

include/bitstream/stream/bit_reader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ namespace bitstream
271271
return true;
272272
}
273273

274+
/**
275+
* @brief Reads from the buffer into mulitple variables.
276+
* @note Pass multi<T>(...) to this in place of multiple calls to the regular serialize functions.
277+
* @tparam ...Args The types of the arguments to pass to the serialize function
278+
* @param ...args The arguments to pass to the serialize function
279+
* @return Whether successful or not
280+
*/
274281
template<typename... Args, typename = std::enable_if_t<(utility::has_instance_serialize_v<Args, bit_reader> && ...)>>
275282
[[nodiscard]] bool serialize(Args&&... args)
276283
noexcept((noexcept(std::declval<Args&>().serialize(std::declval<bit_reader&>())) && ...))

include/bitstream/stream/bit_writer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ namespace bitstream
316316
return true;
317317
}
318318

319+
/**
320+
* @brief Writes to the buffer from multiple variables.
321+
* @note Pass multi<T>(...) to this in place of multiple calls to the regular serialize functions.
322+
* @tparam ...Args The types of the arguments to pass to the serialize function
323+
* @param ...args The arguments to pass to the serialize function
324+
* @return Whether successful or not
325+
*/
319326
template<typename... Args, typename = std::enable_if_t<(utility::has_instance_serialize_v<Args, bit_writer> && ...)>>
320327
[[nodiscard]] bool serialize(Args&&... args)
321328
noexcept((noexcept(std::declval<Args&>().serialize(std::declval<bit_writer&>())) && ...))

include/bitstream/stream/multi.h

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,8 @@
88

99
namespace bitstream
1010
{
11-
namespace utility
12-
{
13-
// Check if type has a serializable trait
14-
template<typename T, typename Stream, typename Void = void>
15-
struct has_instance_serialize : std::false_type {};
16-
17-
template<typename T, typename Stream>
18-
struct has_instance_serialize<T, Stream, std::void_t<decltype(std::declval<T&>().serialize(std::declval<Stream&>()))>> : std::true_type {};
19-
20-
template<typename T, typename Stream>
21-
constexpr bool has_instance_serialize_v = has_instance_serialize<T, Stream>::value;
22-
}
23-
24-
2511
template<typename T, typename... Ts>
26-
struct multi_tuple
12+
struct multi_args
2713
{
2814
std::tuple<Ts...> Args;
2915

@@ -35,8 +21,8 @@ namespace bitstream
3521
};
3622

3723
template<typename T, typename... Args>
38-
multi_tuple<T, Args&&...> multi(Args&&... args) noexcept
24+
multi_args<T, Args&&...> multi(Args&&... args) noexcept
3925
{
40-
return multi_tuple<T, Args&&...>{ std::forward_as_tuple(std::forward<Args>(args) ...) };
26+
return multi_args<T, Args&&...>{ std::forward_as_tuple(std::forward<Args>(args) ...) };
4127
}
4228
}

include/bitstream/utility/meta.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
namespace bitstream::utility
88
{
9+
// Check if instance has a serializable trait
10+
template<typename T, typename Stream, typename Void = void>
11+
struct has_instance_serialize : std::false_type {};
12+
13+
template<typename T, typename Stream>
14+
struct has_instance_serialize<T, Stream, std::void_t<decltype(std::declval<T&>().serialize(std::declval<Stream&>()))>> : std::true_type {};
15+
16+
template<typename T, typename Stream>
17+
constexpr bool has_instance_serialize_v = has_instance_serialize<T, Stream>::value;
18+
19+
920
// Check if type has a serializable trait
1021
template<typename Void, typename T, typename Stream, typename... Args>
1122
struct has_serialize : std::false_type {};

src/test/serialize_multi_test.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,42 @@
55
#include <bitstream/stream/bit_writer.h>
66

77
#include <bitstream/traits/integral_traits.h>
8+
#include <bitstream/traits/quantization_traits.h>
89

910
namespace bitstream::test::multi_serialize
1011
{
1112
BS_ADD_TEST(test_serialize_multi)
1213
{
1314
// Test serializing multiple values at once
1415
uint32_t in_value1 = 511;
15-
uint32_t in_value2 = 99;
16+
float in_value2 = 99.12345f;
17+
18+
bounded_range range(-1000.0f, 1000.0f, 0.001f);
1619

1720
// Write some values
1821
byte_buffer<16> buffer;
1922
fixed_bit_writer writer(buffer);
2023

2124
BS_TEST_ASSERT(writer.serialize(
2225
multi<uint32_t>(in_value1, 328, 611),
23-
multi<uint32_t>(in_value2, 11, 111)
26+
multi<bounded_range>(range, in_value2)
2427
));
2528

2629
uint32_t num_bits = writer.flush();
2730

28-
BS_TEST_ASSERT(num_bits == 16);
31+
BS_TEST_ASSERT(num_bits == 30);
2932

3033
// Read the values back and validate
3134
uint32_t out_value1;
32-
uint32_t out_value2;
35+
float out_value2;
3336
fixed_bit_reader reader(buffer, num_bits);
3437

3538
BS_TEST_ASSERT(reader.serialize(
3639
multi<uint32_t>(out_value1, 328U, 611U),
37-
multi<uint32_t>(out_value2, 11U, 111U)
40+
multi<bounded_range>(range, out_value2)
3841
));
3942

4043
BS_TEST_ASSERT(out_value1 == in_value1);
41-
BS_TEST_ASSERT(out_value2 == in_value2);
44+
BS_TEST_ASSERT(std::abs(in_value2 - out_value2) <= range.get_precision());
4245
}
4346
}

0 commit comments

Comments
 (0)