Add attributes

This commit is contained in:
sha512sum 2024-06-28 16:37:33 +00:00
parent 475d310f67
commit 091a100f22
4 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,27 @@
#include <utempl/attributes.hpp>
#include <string>
template <typename T>
struct AttributeData {
T value;
};
#define MY_ATTRIBUTE(type, ...) GENERIC_ATTRIBUTE(AttributeData<type>{__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 {};

5
examples/src/make.cpp Normal file
View file

@ -0,0 +1,5 @@
#include <utempl/utils.hpp>
auto main() -> int {
constexpr auto arr = utempl::TupleMaker<std::array<void*, 0>>::Make();
};

View file

@ -0,0 +1,48 @@
#include <utempl/loopholes/counter.hpp>
#include <utempl/meta_info.hpp>
namespace utempl {
namespace impl {
struct AttributesTag {};
template <typename T>
struct AttributesCounterTag {};
template <typename Current, auto f = []{}, auto = AddTypeToTag<impl::AttributesTag, Current, decltype(f)>()>
struct CurrentSetter {};
} // namespace impl
#define ATTRIBUTE_STRUCT(name, ...) struct name { \
static_assert((::utempl::impl::CurrentSetter<name>(), true)); \
template <std::size_t N> \
static consteval auto GetAttribute(); __VA_ARGS__ }
struct NoInfo {};
#define GENERIC_ATTRIBUTE(value) \
template <> \
consteval auto GetAttribute< \
::utempl::loopholes::Counter< \
::utempl::impl::AttributesCounterTag<decltype(::utempl::GetCurrentTagType< \
::utempl::impl::AttributesTag, \
decltype([]{}) \
>())::Type \
>, \
decltype([]{}) >()>() { return value; }
#define SKIP_ATTRIBUTE() GENERIC_ATTRIBUTE(::utempl::NoInfo{})
} // namespace utempl

View file

@ -70,6 +70,16 @@ consteval auto GetTypeListForTag() {
return impl::GetTypeListForTag<Tag>(TypeList<Ts...>{}); return impl::GetTypeListForTag<Tag>(TypeList<Ts...>{});
}; };
template <
typename Tag,
typename... Ts,
auto I = utempl::loopholes::CountValue<Tag, Ts...>() - 1
>
consteval auto GetCurrentTagType() {
return Magic(utempl::loopholes::Getter<MetaInfoKey<I, Tag>{}>());
};
/* /*
static_assert(kTypeId<int> == 0); static_assert(kTypeId<int> == 0);
static_assert(kTypeId<void> == 1); static_assert(kTypeId<void> == 1);