From 091a100f22bd5cd628a47c5b469ba8fbbbd2df0b Mon Sep 17 00:00:00 2001 From: sha512sum Date: Fri, 28 Jun 2024 16:37:33 +0000 Subject: [PATCH] Add attributes --- examples/src/attributes.cpp | 27 ++++++++++++++++++++ examples/src/make.cpp | 5 ++++ include/utempl/attributes.hpp | 48 +++++++++++++++++++++++++++++++++++ include/utempl/meta_info.hpp | 10 ++++++++ 4 files changed, 90 insertions(+) create mode 100644 examples/src/attributes.cpp create mode 100644 examples/src/make.cpp create mode 100644 include/utempl/attributes.hpp diff --git a/examples/src/attributes.cpp b/examples/src/attributes.cpp new file mode 100644 index 0000000..337c9c5 --- /dev/null +++ b/examples/src/attributes.cpp @@ -0,0 +1,27 @@ +#include +#include + +template +struct AttributeData { + T value; +}; + + +#define MY_ATTRIBUTE(type, ...) GENERIC_ATTRIBUTE(AttributeData{__VA_ARGS__}) + + +ATTRIBUTE_STRUCT(SomeStruct, + MY_ATTRIBUTE(int, .value = 2); + int field; + SKIP_ATTRIBUTE(); + int field2; + MY_ATTRIBUTE(std::string, .value = "HEY!") + std::string field3; +); + +static_assert(SomeStruct::GetAttribute<0>().value == 2); +static_assert(SomeStruct::GetAttribute<2>().value == "HEY!"); + + + +auto main() -> int {}; diff --git a/examples/src/make.cpp b/examples/src/make.cpp new file mode 100644 index 0000000..82f2534 --- /dev/null +++ b/examples/src/make.cpp @@ -0,0 +1,5 @@ +#include + +auto main() -> int { + constexpr auto arr = utempl::TupleMaker>::Make(); +}; diff --git a/include/utempl/attributes.hpp b/include/utempl/attributes.hpp new file mode 100644 index 0000000..82e9035 --- /dev/null +++ b/include/utempl/attributes.hpp @@ -0,0 +1,48 @@ +#include +#include + + +namespace utempl { + + + +namespace impl { + +struct AttributesTag {}; + +template +struct AttributesCounterTag {}; + + +template ()> +struct CurrentSetter {}; + + +} // namespace impl + + + + + +#define ATTRIBUTE_STRUCT(name, ...) struct name { \ +static_assert((::utempl::impl::CurrentSetter(), true)); \ +template \ +static consteval auto GetAttribute(); __VA_ARGS__ } + +struct NoInfo {}; + +#define GENERIC_ATTRIBUTE(value) \ + template <> \ + consteval auto GetAttribute< \ + ::utempl::loopholes::Counter< \ + ::utempl::impl::AttributesCounterTag())::Type \ + >, \ + decltype([]{}) >()>() { return value; } + +#define SKIP_ATTRIBUTE() GENERIC_ATTRIBUTE(::utempl::NoInfo{}) + + +} // namespace utempl diff --git a/include/utempl/meta_info.hpp b/include/utempl/meta_info.hpp index ace4259..74c6097 100644 --- a/include/utempl/meta_info.hpp +++ b/include/utempl/meta_info.hpp @@ -70,6 +70,16 @@ consteval auto GetTypeListForTag() { return impl::GetTypeListForTag(TypeList{}); }; +template < + typename Tag, + typename... Ts, + auto I = utempl::loopholes::CountValue() - 1 +> +consteval auto GetCurrentTagType() { + return Magic(utempl::loopholes::Getter{}>()); +}; + + /* static_assert(kTypeId == 0); static_assert(kTypeId == 1);