Skip to content

Commit

Permalink
Add tuple classes
Browse files Browse the repository at this point in the history
  • Loading branch information
StarQTius committed Sep 15, 2024
1 parent 931a974 commit 45262dc
Show file tree
Hide file tree
Showing 18 changed files with 856 additions and 626 deletions.
95 changes: 41 additions & 54 deletions example/dynamixel-protocol-2.0.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <upd/integer.hpp>
#include <upd/description.hpp>
#include <iostream>
#include <cinttypes>
#include <climits>
#include <iostream>

#include <upd/description.hpp>
#include <upd/integer.hpp>

namespace std {

ostream &operator<<(ostream &os, byte b) {
return os << static_cast<int>(b);
}
ostream &operator<<(ostream &os, byte b) { return os << static_cast<int>(b); }

} // namespace std

Expand All @@ -16,28 +16,20 @@ constexpr auto description = [] {
using namespace upd::literals;
using namespace upd::descriptor;

return
constant<"header"_h>(0xfdffff, width<32>) |
field<"id"_h > (unsigned_int, width<8>) |
field<"length"_h>(unsigned_int, width<16>) |
field<"instruction"_h>(unsigned_int, width<8>) |
field<"checksum"_h>(unsigned_int, width<16>);
return constant<"header"_h>(0xfdffff, width<32>) | field<"id"_h>(unsigned_int, width<8>) |
field<"length"_h>(unsigned_int, width<16>) | field<"instruction"_h>(unsigned_int, width<8>) |
field<"checksum"_h>(unsigned_int, width<16>);
}();

constexpr auto answer_description = [] {
using namespace upd;
using namespace upd::literals;
using namespace upd::descriptor;

return
constant<"header"_h>(0xfdffff, width<32>) |
field<"id"_h > (unsigned_int, width<8>) |
field<"length"_h>(unsigned_int, width<16>) |
field<"instruction"_h>(unsigned_int, width<8>) |
field<"error"_h>(unsigned_int, width<8>) |
field<"model_number"_h>(unsigned_int, width<16>) |
field<"firmware_version"_h>(unsigned_int, width<8>) |
field<"checksum"_h>(unsigned_int, width<16>);
return constant<"header"_h>(0xfdffff, width<32>) | field<"id"_h>(unsigned_int, width<8>) |
field<"length"_h>(unsigned_int, width<16>) | field<"instruction"_h>(unsigned_int, width<8>) |
field<"error"_h>(unsigned_int, width<8>) | field<"model_number"_h>(unsigned_int, width<16>) |
field<"firmware_version"_h>(unsigned_int, width<8>) | field<"checksum"_h>(unsigned_int, width<16>);
}();

struct serializer {
Expand All @@ -56,7 +48,7 @@ struct serializer {
void serialize_signed(XInteger value, OutputIt output) {
static_assert(upd::is_extended_integer_v<XInteger>, "`value` must be an instance of `extended_integer`");
static_assert(upd::is_signed_v<XInteger>, "`value` must be signed");

auto sign = value.signbit();
auto abs = value.abs().enlarge(upd::width<1>);

Expand Down Expand Up @@ -94,10 +86,10 @@ struct serializer {
auto last_written = std::copy_n(input, size, byteseq.begin());

UPD_ASSERT(last_written == byteseq.end());

auto raw = upd::recompose_into_xuint(byteseq);
auto sign = (raw & upd::nth_bit<Bitsize> != 0);
auto abs = (raw & upd::nth_bit<Bitsize>) ? ~raw + 1: raw;
auto abs = (raw & upd::nth_bit<Bitsize>) ? ~raw + 1 : raw;

return sign ? -abs : abs;
}
Expand All @@ -106,12 +98,11 @@ struct serializer {
template<std::size_t N>
struct bytearray : std::array<std::byte, N> {
template<typename... Bytes>
constexpr explicit bytearray(Bytes... bytes) noexcept: std::array<std::byte, N>{static_cast<std::byte>(bytes)...} {
}
constexpr explicit bytearray(Bytes... bytes) noexcept : std::array<std::byte, N>{static_cast<std::byte>(bytes)...} {}
};

template<typename... Bytes>
explicit bytearray(Bytes...) noexcept -> bytearray<sizeof...(Bytes)>;
explicit bytearray(Bytes...) noexcept->bytearray<sizeof...(Bytes)>;

int main() {
using namespace upd::literals;
Expand All @@ -121,50 +112,46 @@ int main() {
std::cout << std::hex;

auto ping_ex1 = description.instantiate(
upd::kw<"header"_h> = 0xfdffff,
upd::kw<"id"_h> = 1,
upd::kw<"length"_h> = 3,
upd::kw<"instruction"_h> = 1,
upd::kw<"checksum"_h> = 0x4e19);
upd::kw<"id"_h> = 1, upd::kw<"length"_h> = 3, upd::kw<"instruction"_h> = 1, upd::kw<"checksum"_h> = 0x4e19);
if (!ping_ex1) {
return (int) ping_ex1.error();
return (int)ping_ex1.error();
}

std::printf("Ping: example 1\n");
ping_ex1->serialize(ser, oit);
std::printf("\n\n");

auto answer1_seq = bytearray {0xff, 0xff, 0xfd, 0x00, 0x01, 0x07, 0x00, 0x55, 0x00, 0x06, 0x04, 0x26, 0x65, 0x5d};
auto answer1_seq = bytearray{0xff, 0xff, 0xfd, 0x00, 0x01, 0x07, 0x00, 0x55, 0x00, 0x06, 0x04, 0x26, 0x65, 0x5d};
auto answer1 = answer_description.decode(answer1_seq.begin(), ser);
if (!answer1) {
return (int) answer1.error();
return (int)answer1.error();
}

std::printf("Answer 1: \n");
std::printf("- header: %" PRIx32 "\n", (std::uint32_t) (*answer1)[upd::kw<"header"_h>]);
std::printf("- id: %" PRIx8 "\n", (std::uint8_t) (*answer1)[upd::kw<"id"_h>]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t) (*answer1)[upd::kw<"length"_h>]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t) (*answer1)[upd::kw<"instruction"_h>]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t) (*answer1)[upd::kw<"error"_h>]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t) (*answer1)[upd::kw<"model_number"_h>]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t) (*answer1)[upd::kw<"firmware_version"_h>]);
std::printf("- checksum: %" PRIx16 "\n", (std::uint16_t) (*answer1)[upd::kw<"checksum"_h>]);
std::printf("- header: %" PRIx32 "\n", (std::uint32_t)(*answer1)[upd::kw<"header"_h>]);
std::printf("- id: %" PRIx8 "\n", (std::uint8_t)(*answer1)[upd::kw<"id"_h>]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t)(*answer1)[upd::kw<"length"_h>]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t)(*answer1)[upd::kw<"instruction"_h>]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t)(*answer1)[upd::kw<"error"_h>]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t)(*answer1)[upd::kw<"model_number"_h>]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t)(*answer1)[upd::kw<"firmware_version"_h>]);
std::printf("- checksum: %" PRIx16 "\n", (std::uint16_t)(*answer1)[upd::kw<"checksum"_h>]);
std::printf("\n\n");

auto answer2_seq = bytearray {0xff, 0xff, 0xfd, 0x00, 0x02, 0x07, 0x00, 0x55, 0x00, 0x06, 0x04, 0x26, 0x6f, 0x6d};
auto answer2_seq = bytearray{0xff, 0xff, 0xfd, 0x00, 0x02, 0x07, 0x00, 0x55, 0x00, 0x06, 0x04, 0x26, 0x6f, 0x6d};
auto answer2 = answer_description.decode(answer2_seq.begin(), ser);
if (!answer2) {
return (int) answer2.error();
return (int)answer2.error();
}

std::printf("Answer 2: \n");
std::printf("- header: %" PRIx32 "\n", (std::uint32_t) (*answer2)[upd::kw<"header"_h>]);
std::printf("- id: %" PRIx8 "\n", (std::uint8_t) (*answer2)[upd::kw<"id"_h>]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t) (*answer2)[upd::kw<"length"_h>]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t) (*answer2)[upd::kw<"instruction"_h>]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t) (*answer2)[upd::kw<"error"_h>]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t) (*answer2)[upd::kw<"model_number"_h>]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t) (*answer2)[upd::kw<"firmware_version"_h>]);
std::printf("- checksum: %" PRIx16 "\n", (std::uint16_t) (*answer2)[upd::kw<"checksum"_h>]);
std::printf("- header: %" PRIx32 "\n", (std::uint32_t)(*answer2)[upd::kw<"header"_h>]);
std::printf("- id: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"id"_h>]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t)(*answer2)[upd::kw<"length"_h>]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"instruction"_h>]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"error"_h>]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t)(*answer2)[upd::kw<"model_number"_h>]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"firmware_version"_h>]);
std::printf("- checksum: %" PRIx16 "\n", (std::uint16_t)(*answer2)[upd::kw<"checksum"_h>]);
std::printf("\n\n");
}
}
3 changes: 2 additions & 1 deletion include/upd/basic_ibytestream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class basic_ibytestream {
} else if constexpr (std::is_enum_v<byte_t>) {
return std::numeric_limits<std::underlying_type_t<byte_t>>::digits;
} else {
static_assert(UPD_ALWAYS_FALSE, "Producers can only produce `extended_integer` values, integral values or enumerators");
static_assert(UPD_ALWAYS_FALSE,
"Producers can only produce `extended_integer` values, integral values or enumerators");
}
}();

Expand Down
Loading

0 comments on commit 45262dc

Please sign in to comment.