Skip to content

Commit

Permalink
dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
StarQTius committed Sep 27, 2024
1 parent 2f4ca46 commit 0636720
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 91 deletions.
26 changes: 13 additions & 13 deletions example/dynamixel-protocol-2.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int main() {
std::cout << std::hex;

auto ping_ex1 = description.instantiate(ser,
upd::kw<"id"> = 1, upd::kw<"length"> = 3, upd::kw<"instruction"> = 1);
"id"_kw = 1, "length"_kw = 3, "instruction"_kw = 1);
if (!ping_ex1) {
return (int)ping_ex1.error();
}
Expand All @@ -174,12 +174,12 @@ int main() {
}

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

auto answer2_seq = bytearray{0xff, 0xff, 0xfd, 0x00, 0x02, 0x07, 0x00, 0x55, 0x00, 0x06, 0x04, 0x26, 0x6f, 0x6d};
Expand All @@ -189,11 +189,11 @@ int main() {
}

std::printf("Answer 2: \n");
std::printf("- id: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"id">]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t)(*answer2)[upd::kw<"length">]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"instruction">]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"error">]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t)(*answer2)[upd::kw<"model_number">]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t)(*answer2)[upd::kw<"firmware_version">]);
std::printf("- id: %" PRIx8 "\n", (std::uint8_t)(*answer2)["id"_kw]);
std::printf("- length: %" PRIx16 "\n", (std::uint16_t)(*answer2)["length"_kw]);
std::printf("- instruction: %" PRIx8 "\n", (std::uint8_t)(*answer2)["instruction"_kw]);
std::printf("- error: %" PRIx8 "\n", (std::uint8_t)(*answer2)["error"_kw]);
std::printf("- model number: %" PRIx16 "\n", (std::uint16_t)(*answer2)["model_number"_kw]);
std::printf("- firmware version: %" PRIx8 "\n", (std::uint8_t)(*answer2)["firmware_version"_kw]);
std::printf("\n\n");
}
18 changes: 9 additions & 9 deletions include/upd/description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,13 @@ class description {
if constexpr (field.tag == field_tag::pure_field) {
constexpr auto width = field.width;
using representation = std::conditional_t<field.is_signed, xint<width>, xuint<width>>;
auto value = named_args[kw<field.identifier>];
return kw<field.identifier> = representation{value};
auto value = named_args[keyword<field.identifier>{}];
return keyword<field.identifier>{} = representation{value};
} else if constexpr (field.tag == field_tag::constant) {
return kw<field.identifier> = field.value;
return keyword<field.identifier>{} = field.value;
} else if constexpr (field.tag == field_tag::checksum) {
constexpr auto width = field.width;
return kw<field.identifier> = xuint<width>{field.init};
return keyword<field.identifier>{} = xuint<width>{field.init};
} else {
static_assert(UPD_ALWAYS_FALSE, "`field` is not a field object");
}
Expand All @@ -339,13 +339,13 @@ class description {
if constexpr (field.tag == field_tag::checksum) {
constexpr auto identifier = field.identifier;
auto &[op, init, get_range] = field;
auto &acc = retval[kw<identifier>];
auto &acc = retval[keyword<identifier>{}];
auto folder = invoker_iterator{
[&, op=op](auto x) { acc = op(acc, x); }
};

get_range(identifiers).for_each([&](auto id) {
return retval[kw<id.value>].serialize(ser, folder);
return retval[keyword<id.value>{}].serialize(ser, folder);
});
}
};
Expand All @@ -367,7 +367,7 @@ class description {
auto first_pass = [&](const auto &field) {
if constexpr (field.tag == field_tag::checksum) {
const auto &[op, init, get_range] = field;
auto &acc = accumulators[kw<field.identifier>];
auto &acc = accumulators[keyword<field.identifier>{}];
auto range = get_range(identifiers);
auto accumulate = [&, &op = op](auto identifier, const auto &value) {
constexpr auto p = [=](auto id_type) { return auto_constant<id_type->value == identifier>{}; };
Expand All @@ -393,7 +393,7 @@ class description {
return value;
}
};
return kw<identifier> = deserialize_into_xinteger<representation>(decorated_src, ser);
return keyword<identifier>{} = deserialize_into_xinteger<representation>(decorated_src, ser);
} else if constexpr (field.tag == field_tag::constant) {
constexpr auto width = field.width;
using representation = std::conditional_t<field.is_signed, xint<width>, xuint<width>>;
Expand All @@ -413,7 +413,7 @@ class description {
constexpr auto width = field.width;
using representation = std::conditional_t<field.is_signed, xint<width>, xuint<width>>;
auto received = deserialize_into_xinteger<representation>(detail::iterator_reference{src}, ser);
if (!err && accumulators[kw<identifier>] != received) {
if (!err && accumulators[keyword<identifier>{}] != received) {
err = error::checksum_mismatch;
}
} else {
Expand Down
24 changes: 4 additions & 20 deletions include/upd/named_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "description.hpp"
#include "named_value.hpp"
#include "detail/always_false.hpp"
#include "detail/integral_constant.hpp"
#include "detail/tuple_operations.hpp"
Expand All @@ -20,24 +21,7 @@

namespace upd {

template<std::size_t N>
struct name {
constexpr name(const char (&s)[N]): value{} {
std::ranges::copy(s, value);
}

template<std::size_t M>
[[nodiscard]] constexpr auto operator==(const name<M> &rhs) const noexcept -> bool {
return std::string_view{value} == std::string_view{rhs.value};
};

char value[N];
};

template<name>
struct kw_t;

template<name..., typename... Ts>
template<names, typename... Ts>
[[nodiscard]] constexpr auto name_tuple(std::tuple<Ts...>) noexcept;

template<typename... Named_Tuple_Ts>
Expand All @@ -47,7 +31,7 @@ template<typename Tuple_T, name... Ids>
class named_tuple {
using id_ts = detail::integral_constant_tuple_t<Ids...>;

template<name..., typename... Ts>
template<names, typename... Ts>
friend constexpr auto name_tuple(std::tuple<Ts...>) noexcept;

template<typename... Named_Tuple_Ts>
Expand Down Expand Up @@ -101,7 +85,7 @@ class named_tuple {
template<typename>
struct is_named_tuple_instance : std::false_type {};

template<typename Tuple, name... Identifiers>
template<typename Tuple, names Identifiers>
struct is_named_tuple_instance<named_tuple<Tuple, Identifiers...>> : std::true_type {};

template<name... Ids, typename... Ts>
Expand Down
104 changes: 64 additions & 40 deletions include/upd/named_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,39 @@
#include "detail/tuple_operations.hpp"
#include "detail/variadic/concat.hpp"
#include "integer.hpp"
#include "named_tuple.hpp"
#include "upd.hpp"
#include "detail/sequence.hpp"

namespace upd {

template<name>
struct kw_t;
template<auto Identifier>
struct keyword;

template<name Identifier>
constexpr auto kw = kw_t<Identifier>{};
template<std::size_t Size>
struct name {
consteval name(const char (&str)[Size]) noexcept(release): string{} {
std::ranges::copy(str, string);
}

char string[Size];
};

template<std::size_t LeftSize, std::size_t RightSize>
[[nodiscard]] consteval auto operator==(const name<LeftSize> &lhs, const name<RightSize> &rhs) noexcept(release) -> bool {
return std::string_view{lhs.string} == std::string_view{rhs.string};
};

template<std::size_t... Sizes>
struct names {
constexpr names(const name<Sizes> &... ns): strings{} {
auto i = std::size_t{0};
(std::ranges::copy(ns.string, strings[i++]), ...);
}

char strings[sizeof...(Sizes)][std::max(Sizes...)];
};

template<name Identifier, typename T>
template<auto Identifier, typename T>
class named_value {
public:
constexpr static auto identifier = Identifier;
Expand All @@ -34,19 +55,19 @@ class named_value {
template<typename F>
[[nodiscard]] constexpr auto map(F &&f) & {
decltype(auto) mapped_value = std::invoke(UPD_FWD(f), m_value);
return kw<identifier> = UPD_FWD(mapped_value);
return keyword<identifier>{} = UPD_FWD(mapped_value);
}

template<typename F>
[[nodiscard]] constexpr auto map(F &&f) const & {
decltype(auto) mapped_value = std::invoke(UPD_FWD(f), m_value);
return kw<identifier> = UPD_FWD(mapped_value);
return keyword<identifier>{} = UPD_FWD(mapped_value);
}

template<typename F>
[[nodiscard]] constexpr auto map(F &&f) && {
decltype(auto) mapped_value = std::invoke(UPD_FWD(f), std::move(m_value));
return kw<identifier> = UPD_FWD(mapped_value);
return keyword<identifier>{} = UPD_FWD(mapped_value);
}

[[nodiscard]] constexpr auto value() const & noexcept -> const T& {
Expand All @@ -57,46 +78,49 @@ class named_value {
return std::move(m_value);
}

template<typename NamedObject>
[[nodiscard]] constexpr auto operator,(NamedObject nobj) const & {
if constexpr (requires { upd::named_value{nobj}; }) {
auto values = std::tuple{m_value, std::move(nobj).value()};
return name_tuple<identifier, nobj.identifier>(std::move(values));
} else if constexpr (is_named_tuple_instance<NamedObject>::value) {
auto tail = std::tuple{m_value};
auto named_tail = name_tuple<identifier>(std::move(tail));
return concat_named_tuple(std::move(named_tail), std::move(nobj));
} else {
static_assert(UPD_ALWAYS_FALSE, "`nobj` must be a `named_value` or `named_tuple` instance");
}
}

template<typename NamedObject>
[[nodiscard]] constexpr auto operator,(NamedObject nobj) &&noexcept {
if constexpr (requires { upd::named_value{nobj}; }) {
auto values = std::tuple{std::move(m_value), std::move(nobj).value()};
return name_tuple<identifier, nobj.identifier>(std::move(values));
} else if constexpr (is_named_tuple_instance<NamedObject>::value) {
auto tail = std::tuple{std::move(m_value)};
auto named_tail = name_tuple<identifier>(std::move(tail));
return concat_named_tuple(std::move(named_tail), std::move(nobj));
} else {
static_assert(UPD_ALWAYS_FALSE, "`nobj` must be a `named_value` or `named_tuple` instance");
}
}

private:
T m_value;
};

template<name Identifier>
struct kw_t {
template<typename T>
concept named_value_instance = requires(T x) {
{ named_value{x} } -> std::same_as<T>;
}

template<typename T>
concept named_tuple_instance = requires(T x) {
{ named_tuple{x} } -> std::same_as<T>;
}

template<named_value_instance Lhs, named_value_instance Rhs>
[[nodiscard]] constexpr auto operator,(Lhs &&lhs, Rhs &&rhs) noexcept(release) {
auto values = std::tuple{UPD_FWD(lhs).value(), UPD_FWD(rhs).value()};
return name_tuple<{identifier, rhs.identifier}>(std::move(values));
}

template<named_tuple_instance Lhs, named_value_instance Rhs>
[[nodiscard]] constexpr auto operator,(Lhs &&lhs, Rhs &&rhs) noexcept(release) {
auto values = std::tuple{UPD_FWD(lhs).value(), UPD_FWD(rhs).value()};
return name_tuple<{identifier, rhs.identifier}>(std::move(values));
}

template<auto Identifier>
struct keyword {
constexpr static auto identifier = Identifier;

template<typename T>
[[nodiscard]] constexpr auto operator=(T x) const noexcept -> named_value<Identifier, T> {
[[nodiscard]] constexpr auto operator=(T x) const noexcept(release) -> named_value<identifier, T> {
return named_value<Identifier, T>{std::in_place, UPD_FWD(x)};
}
};

} // namespace upd

namespace upd::literals {

template<name Identifier>
[[nodiscard]] constexpr auto operator""_kw() noexcept(release) {
return keyword<Identifier>{};
}

} // namespace upd::literals
9 changes: 0 additions & 9 deletions include/upd/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
#include "detail/variadic/clean.hpp"
#include "detail/is_instance_of.hpp"

namespace upd::detail {

template<typename F, std::size_t... Is>
[[nodiscard]] constexpr auto apply_on_index_sequence(F &&f, std::index_sequence<Is...>) -> decltype(auto) {
return UPD_FWD(f)(std::integral_constant<std::size_t, Is>{}...);
}

} // namespace upd::detail

namespace upd {

template<typename T, template<typename...> typename TT>
Expand Down

0 comments on commit 0636720

Please sign in to comment.