diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6a358db --- /dev/null +++ b/.clang-format @@ -0,0 +1,13 @@ +BasedOnStyle: Google +IndentWidth: 2 +ColumnLimit: 140 +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +BreakConstructorInitializers: AfterColon +AlwaysBreakAfterReturnType: None +SpaceBeforeParens: Never +AllowShortFunctionsOnASingleLine: None +AllowShortLambdasOnASingleLine: Empty +BinPackArguments: false +BinPackParameters: false +AlwaysBreakTemplateDeclarations: true diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..1d4ba70 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1 @@ +Checks: '-*,google-*,cppcoreguidelines-*,-cppcoreguidelines-c-copy-assignment-signature,-cppcoreguidelines-special-member-functions,-cppcoreguidelines-avoid-const-or-ref-data-members,modernize-*' diff --git a/include/utempl/attributes.hpp b/include/utempl/attributes.hpp index 3c0375e..822efbf 100644 --- a/include/utempl/attributes.hpp +++ b/include/utempl/attributes.hpp @@ -2,11 +2,8 @@ #include #include - namespace utempl { - - namespace impl { struct AttributesTag {}; @@ -14,40 +11,27 @@ struct AttributesTag {}; template struct AttributesCounterTag {}; +} // namespace impl - -} // namespace impl - - -template < - typename T, - typename..., - auto f = []{}, - auto = AddTypeToTag() -> +template ()> consteval auto OpenStruct() -> bool { return true; }; -template < - typename..., - auto f = []{}, - auto I = loopholes::CountValue(), - auto II = (I >= 2) ? I - 2 : I - 1, - typename T = decltype(Magic(loopholes::Getter{}>{}))::Type, - auto = AddTypeToTag() -> +template (), + auto II = (I >= 2) ? I - 2 : I - 1, + typename T = decltype(Magic(loopholes::Getter{}>{}))::Type, + auto = AddTypeToTag()> consteval auto CloseStruct() -> bool { return true; }; - - struct NoInfo { consteval auto operator==(const NoInfo&) const -> bool = default; }; - namespace impl { template @@ -60,59 +44,48 @@ struct FieldAttributeData<> { using Type = NoInfo; }; - -template < - typename T, - typename... Ts, - auto f = []{}, - typename Current = decltype(GetCurrentTagType())::Type, - auto = AddTypeToTag, typename FieldAttributeData::Type, decltype(f)>() -> +template ())::Type, + auto = AddTypeToTag, typename FieldAttributeData::Type, decltype(f)>()> consteval auto FieldAttribute() -> T; -} // namespace impl +} // namespace impl template using FieldAttribute = decltype(impl::FieldAttribute()); +#define ATTRIBUTE_STRUCT(name, ...) /* NOLINT */ \ + \ + struct name { \ + static_assert(::utempl::OpenStruct()); \ + template \ + static consteval auto GetAttribute(); \ + __VA_ARGS__ \ + static_assert(::utempl::CloseStruct()); \ + } -#define ATTRIBUTE_STRUCT(name, ...) struct name { \ -static_assert(::utempl::OpenStruct()); \ -template \ -static consteval auto GetAttribute(); \ - __VA_ARGS__ \ - static_assert(::utempl::CloseStruct());\ -} +#define GENERIC_ATTRIBUTE(value) /* NOLINT */ \ + template <> \ + consteval auto GetAttribute<::utempl::loopholes::Counter< \ + ::utempl::impl::AttributesCounterTag())::Type>, \ + decltype([] {})>()>() { \ + return value; \ + } +#define SKIP_ATTRIBUTE() /* NOLINT */ GENERIC_ATTRIBUTE(::utempl::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{}) - - -template < - typename T, - auto f = []{}, - bool R = (loopholes::CountValue, decltype(f)>() > 0) -> +template , decltype(f)>() > 0)> concept HasAttributes = R; template -concept HasMacroAttributes = requires {T::template GetAttribute<0>();}; - - +concept HasMacroAttributes = requires { T::template GetAttribute<0>(); }; template -consteval auto GetAttributes() requires HasMacroAttributes { +consteval auto GetAttributes() + requires HasMacroAttributes +{ constexpr auto I = loopholes::CountValue>(); return [](auto... is) { return Tuple{T::template GetAttribute()...}; @@ -127,7 +100,4 @@ consteval auto GetAttributes() { } | kSeq; }; - - - -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/constexpr_string.hpp b/include/utempl/constexpr_string.hpp index 0d0d939..501e74d 100644 --- a/include/utempl/constexpr_string.hpp +++ b/include/utempl/constexpr_string.hpp @@ -1,18 +1,19 @@ #pragma once #include + #include namespace utempl { template struct ConstexprString; -} // namespace utempl +} // namespace utempl template struct fmt::formatter> : public fmt::formatter { constexpr auto parse(format_parse_context& ctx) const { return ctx.begin(); }; - inline constexpr auto format(const utempl::ConstexprString& str, auto& ctx) const { + constexpr auto format(const utempl::ConstexprString& str, auto& ctx) const { return fmt::formatter::format({str.begin()}, ctx); }; }; @@ -22,75 +23,73 @@ namespace utempl { template struct ConstexprString { std::array data; - inline constexpr auto begin() -> char* { + constexpr auto begin() -> char* { return this->data.begin(); }; - inline constexpr auto begin() const -> const char* { + [[nodiscard]] constexpr auto begin() const -> const char* { return this->data.begin(); }; - inline constexpr auto end() -> char* { + [[nodiscard]] constexpr auto end() -> char* { return this->data.end(); }; - inline constexpr auto end() const -> const char* { + [[nodiscard]] constexpr auto end() const -> const char* { return this->data.end(); }; static constexpr auto kSize = Size == 0 ? 0 : Size - 1; - inline constexpr ConstexprString() = default; - inline constexpr ConstexprString(const char (&data)[Size]) : data{} { + explicit constexpr ConstexprString() = default; + constexpr ConstexprString(const char (&data)[Size]) : data{} { // NOLINT std::ranges::copy_n(data, Size, this->data.begin()); }; - inline constexpr ConstexprString(std::string data) : data{} { + explicit constexpr ConstexprString(std::string data) : data{} { std::ranges::copy_n(data.begin(), Size, this->data.begin()); }; - inline constexpr ConstexprString(std::array data) : data(std::move(data)) {}; - inline constexpr auto size() const { + explicit constexpr ConstexprString(std::array data) : data(std::move(data)) {}; + constexpr auto size() const { return Size == 0 ? 0 : Size - 1; }; - inline constexpr operator std::string_view() const & { + constexpr operator std::string_view() const& { // NOLINT return {this->begin()}; }; - inline constexpr bool operator==(std::string_view other) const { + constexpr auto operator==(std::string_view other) const -> bool { return static_cast(*this) == other; }; - inline constexpr bool operator==(const ConstexprString& other) const { + constexpr auto operator==(const ConstexprString& other) const { return static_cast(*this) == static_cast(other); }; template - inline constexpr bool operator==(const ConstexprString& other) const { + constexpr auto operator==(const ConstexprString& other) const -> bool { return false; }; - inline constexpr bool operator==(const std::string& other) const { + constexpr auto operator==(const std::string& other) const -> bool { return static_cast(*this) == other; }; template - inline constexpr auto operator+(const ConstexprString& other) -> ConstexprString { + constexpr auto operator+(const ConstexprString& other) -> ConstexprString { ConstexprString response; std::ranges::copy_n(this->begin(), Size - 1, response.begin()); std::ranges::copy_n(other.begin(), SSize, response.begin() + Size - 1); return response; }; - inline constexpr ConstexprString(const ConstexprString&) = default; - inline constexpr ConstexprString(ConstexprString&&) = default; + constexpr ConstexprString(const ConstexprString&) = default; + constexpr ConstexprString(ConstexprString&&) = default; }; template -inline constexpr auto operator<<(std::ostream& stream, const ConstexprString& str) -> std::ostream& { +constexpr auto operator<<(std::ostream& stream, const ConstexprString& str) -> std::ostream& { stream << static_cast(str); return stream; }; template -inline constexpr auto CreateStringWith(char c) { - ConstexprString str = {}; +constexpr auto CreateStringWith(char c) { + ConstexprString str{}; for(std::size_t i = 0; i < Count; i++) { str.data[i] = c; - }; + }; str.data[Count] = '\0'; return str; }; - template -ConstexprString(const char (&data)[Size]) -> ConstexprString; -} // namespace utempl - +ConstexprString(const char (&data)[Size]) -> ConstexprString; // NOLINT +} // namespace utempl diff --git a/include/utempl/go_interface.hpp b/include/utempl/go_interface.hpp index c2551f4..4a865d7 100644 --- a/include/utempl/go_interface.hpp +++ b/include/utempl/go_interface.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include #include +#include namespace utempl { @@ -12,7 +12,7 @@ consteval auto FindField() -> std::size_t { }; template -inline constexpr auto Get(T&& arg) { +constexpr auto Get(T&& arg) { return boost::pfr::get()>(std::forward(arg)); }; @@ -21,76 +21,76 @@ struct EmptyField {}; namespace impl { - template -inline constexpr auto TryGet(T&& arg) - -> decltype(boost::pfr::get()>(std::forward(arg))) - requires(FindField() < boost::pfr::tuple_size_v>) { +constexpr auto TryGet(T&& arg) -> decltype(boost::pfr::get()>(std::forward(arg))) + requires(FindField() < boost::pfr::tuple_size_v>) +{ constexpr auto I = FindField(); return boost::pfr::get(std::forward(arg)); }; template -inline constexpr auto TryGet(T&& arg) { +constexpr auto TryGet(T&& arg) { return EmptyField{}; }; - template -inline constexpr auto Transform(Transformer&& transformer, From&& from) { - return [&](std::index_sequence){ - return To{transformer(TryGet().size() + 1>{boost::pfr::get_name().begin()}>(from))...}; +constexpr auto Transform(Transformer&& transformer, From&& from) { + return [&](std::index_sequence) { + return To{ + transformer(TryGet().size() + 1>{boost::pfr::get_name().begin()}>(from))...}; }(std::make_index_sequence>()); }; -} // namespace impl +} // namespace impl struct DefaultFieldTransformer { - inline constexpr auto operator()(auto&& arg) -> auto&& { + constexpr auto operator()(auto&& arg) -> auto&& { return arg; }; - inline constexpr auto operator()(auto& arg) -> auto& { + constexpr auto operator()(auto& arg) -> auto& { return arg; }; template - inline constexpr auto operator()(EmptyField arg) { + constexpr auto operator()(EmptyField arg) { static_assert(str == "This Field Not Found"); }; }; template struct GoInterface : Value { - inline constexpr GoInterface(Value&& value) : - Value(std::move(value)) {}; - inline constexpr GoInterface(const Value& value) : - Value(value) {}; + constexpr GoInterface(Value&& value) : Value(std::move(value)) {}; // NOLINT + constexpr GoInterface(const Value& value) : Value(value) {}; // NOLINT template - inline constexpr GoInterface(T&& value) : - Value(impl::Transform(Transformer{}, std::forward(value))) {}; - inline constexpr auto operator=(const GoInterface&) -> GoInterface& = default; - inline constexpr auto operator=(GoInterface&&) -> GoInterface& = default; - inline constexpr auto operator==(const Value& other) const -> bool - requires requires(const Value& a){a == a;} { + constexpr GoInterface(T&& value) : Value(impl::Transform(Transformer{}, std::forward(value))){}; // NOLINT + constexpr auto operator=(const GoInterface&) -> GoInterface& = default; + constexpr auto operator=(GoInterface&&) -> GoInterface& = default; + constexpr auto operator==(const Value& other) const -> bool + requires requires(const Value& a) { a == a; } + { return static_cast(*this) == other; }; - inline constexpr auto operator==(const GoInterface& other) const -> bool { + constexpr auto operator==(const GoInterface& other) const -> bool { return *this == static_cast(other); }; template - inline constexpr auto operator==(T&& other) const -> bool { - return [&](std::index_sequence){ + constexpr auto operator==(T&& other) const -> bool { + return [&](std::index_sequence) { using Type = std::remove_cvref_t; Transformer transformer; // + 1 - NULL Terminator - return ((boost::pfr::get(static_cast(*this)) == - transformer(impl::TryGet().size() + 1>{boost::pfr::get_name().begin()}>(other))) && ...); + return ( + (boost::pfr::get(static_cast(*this)) == + transformer(impl::TryGet().size() + 1>{boost::pfr::get_name().begin()}>( + other))) && + ...); }(std::make_index_sequence>()); }; template - inline constexpr auto operator==(T&& other) const -> bool - requires (requires(const Value& value){value == other;}) { + constexpr auto operator==(T&& other) const -> bool + requires(requires(const Value& value) { value == other; }) + { return static_cast(*this) == other; }; }; -} // namespace utempl - +} // namespace utempl diff --git a/include/utempl/loopholes/core.hpp b/include/utempl/loopholes/core.hpp index 84a37c8..88dd128 100644 --- a/include/utempl/loopholes/core.hpp +++ b/include/utempl/loopholes/core.hpp @@ -9,10 +9,11 @@ struct Getter { }; template struct Injector { - friend constexpr auto Magic(Getter) {return Value;}; + friend constexpr auto Magic(Getter) { + return Value; + }; }; - template struct InjectedImpl { static constexpr bool value = false; @@ -25,7 +26,4 @@ struct InjectedImpl{}))>, Ts...> { template concept Injected = InjectedImpl::value; - - - -} // namespace utempl::loopholes +} // namespace utempl::loopholes diff --git a/include/utempl/loopholes/counter.hpp b/include/utempl/loopholes/counter.hpp index c765452..49045b7 100644 --- a/include/utempl/loopholes/counter.hpp +++ b/include/utempl/loopholes/counter.hpp @@ -1,7 +1,7 @@ #pragma once -#include #include #include +#include namespace utempl::loopholes { @@ -12,7 +12,7 @@ struct TagWithValue {}; template consteval auto Counter() -> std::size_t { - if constexpr(requires{Magic(Getter{}>{});}) { + if constexpr(requires { Magic(Getter{}>{}); }) { return Counter(); }; if constexpr(Add) { @@ -22,32 +22,20 @@ consteval auto Counter() -> std::size_t { }; }; - -} // namespace impl; +} // namespace impl // For incerement counter need a unique Ts... -template < - typename Tag, - typename... Ts, - std::size_t R = impl::Counter() -> +template ()> consteval auto Counter(auto...) -> std::size_t { return R; }; - - // Without increment -template < - typename Tag, - typename... Ts, - std::size_t R = impl::Counter() -> -consteval auto CountValue(auto...) -> std::size_t { +template ()> +consteval auto CountValue(auto...) -> std::size_t { return R; }; - /* static_assert(Counter() == 0); static_assert(Counter() == 1); @@ -55,4 +43,4 @@ static_assert(CountValue() == 2); static_assert(CountValue() == 2); */ -} // namespace utempl::loopholes +} // namespace utempl::loopholes diff --git a/include/utempl/menu.hpp b/include/utempl/menu.hpp index c5e8be0..167021c 100644 --- a/include/utempl/menu.hpp +++ b/include/utempl/menu.hpp @@ -1,30 +1,30 @@ #pragma once -#include +#include +#include + +#include +#include #include +#include #include #include -#include -#include -#include -#include - namespace utempl { -constexpr std::size_t CountDigits(std::size_t num) { +constexpr auto CountDigits(std::size_t num) -> std::size_t { std::size_t count = 0; - do { + do { // NOLINT ++count; - num /= 10; - } while (num != 0); + num /= 10; // NOLINT + } while(num != 0); return count; }; -constexpr std::size_t GetDigit(std::size_t num, std::size_t index) { - for (std::size_t i = 0; i < index; ++i) { - num /= 10; +constexpr auto GetDigit(std::size_t num, std::size_t index) -> std::size_t { + for(std::size_t i = 0; i < index; ++i) { + num /= 10; // NOLINT } - return num % 10; + return num % 10; // NOLINT }; template @@ -44,7 +44,6 @@ constexpr auto GetMax(Range&& range) { return response; }; - namespace menu { namespace impl { @@ -53,38 +52,27 @@ template struct CallbackMessage { ConstexprString message; Optional> need; - consteval CallbackMessage(const char (&need)[N2], const char (&message)[N1]) : - message(std::move(message)) - ,need(std::move(need)) {}; - consteval CallbackMessage(const char (&message)[N1]) : - message(std::move(message)) - ,need(std::nullopt) {}; + consteval CallbackMessage(const char (&need)[N2], const char (&message)[N1]) : // NOLINT + message(std::move(message)), need(std::move(need)) {}; + consteval CallbackMessage(const char (&message)[N1]) : message(std::move(message)), need(std::nullopt) {}; // NOLINT }; template -CallbackMessage(const char(&)[N1], const char(&)[N2]) -> CallbackMessage; +CallbackMessage(const char (&)[N1], const char (&)[N2]) -> CallbackMessage; // NOLINT template -CallbackMessage(const char(&)[N1]) -> CallbackMessage; - -} // namespace impl - +CallbackMessage(const char (&)[N1]) -> CallbackMessage; // NOLINT +} // namespace impl template struct Menu { -private: + private: template static consteval auto FormatMessage() { // + 1 - NULL Terminator - constexpr auto size = fmt::formatted_size(FMT_COMPILE(fmt.data.begin()) - ,neededInput - ,message - ,alignment) + 1; - char data[size]{}; - fmt::format_to(data, FMT_COMPILE(fmt.data.begin()) - ,neededInput - ,message - ,alignment); + constexpr auto size = fmt::formatted_size(FMT_COMPILE(fmt.data.begin()), neededInput, message, alignment) + 1; + std::array data{}; + fmt::format_to(data.begin(), FMT_COMPILE(fmt.data.begin()), neededInput, message, alignment); return ConstexprString(data); }; template @@ -94,27 +82,32 @@ private: if constexpr(Get(storage).need) { return *Get(storage).need; } else { - return ToString(); + return ToString(); }; }(); - constexpr ConstexprString alignment = CreateStringWith(storage).need ? Get(storage).need->size() : CountDigits(I))>(' '); + constexpr ConstexprString alignment = + CreateStringWith(storage).need ? Get(storage).need->size() : CountDigits(I))>(' '); return FormatMessage(); }; -public: + + public: Tuple functionStorage; static consteval auto GetMaxSize() -> std::size_t { - return [&](std::index_sequence){ - constexpr auto list = ListFromTuple(storage); - return GetMax(std::array{(std::remove_cvref_t(list))>().need)>::kSize != 0 ? std::remove_cvref_t(list))>().need)>::kSize : CountDigits(Is))...}); + return [&](std::index_sequence) { + constexpr auto list = ListFromTuple(storage); + return GetMax(std::array{(std::remove_cvref_t(list))>().need)>::kSize != 0 + ? std::remove_cvref_t(list))>().need)>::kSize + : CountDigits(Is))...}); }(std::index_sequence_for()); }; template constexpr auto With(F&& f) const { - return Menu>{.functionStorage = this->functionStorage + Tuple(std::forward(f))}; + return Menu>{.functionStorage = + this->functionStorage + Tuple(std::forward(f))}; }; template - inline constexpr auto Run(std::istream& in = std::cin, std::FILE* out = stdout) const -> std::size_t { + constexpr auto Run(std::istream& in = std::cin, std::FILE* out = stdout) const -> std::size_t { return [&](std::index_sequence) -> std::size_t { constexpr auto message = ((FormatMessageFor() + ...) + enter); auto result = std::fwrite(message.begin(), 1, message.size(), out); @@ -126,24 +119,23 @@ public: }; std::string input; std::getline(in, input); - ([&](storage)>(Wrapper) { - if constexpr(message.need) { - if(*message.need == input) { - Get(this->functionStorage)(); - }; - } else { - if(ToString() == input) { - Get(this->functionStorage)(); - }; - }; - }(Wrapper{}), ...); + ( + [&](storage)>(Wrapper) { + if constexpr(message.need) { + if(*message.need == input) { + Get(this->functionStorage)(); + }; + } else { + if(ToString() == input) { + Get(this->functionStorage)(); + }; + }; + }(Wrapper{}), + ...); return 0; }(std::index_sequence_for()); }; - }; -} // namespace menu -} // namespace utempl - - +} // namespace menu +} // namespace utempl diff --git a/include/utempl/meta_info.hpp b/include/utempl/meta_info.hpp index 74c6097..71ddbb8 100644 --- a/include/utempl/meta_info.hpp +++ b/include/utempl/meta_info.hpp @@ -7,37 +7,33 @@ namespace utempl { namespace impl { struct Types {}; -} // namespace impl - +} // namespace impl template struct MetaInfoKey {}; - template struct MetaInfo { static constexpr std::size_t kTypeId = loopholes::Counter(); using Type = T; -private: + + private: static constexpr auto _ = loopholes::Injector{}, TypeList{}>{}; }; template inline constexpr std::size_t kTypeId = MetaInfo::kTypeId; -template < - typename Tag, - typename T, - typename... Ts, - typename... TTs, - std::size_t Id = loopholes::Counter(), - auto = loopholes::Injector{}, TypeList{}>{} -> -consteval std::size_t AddTypeToTag(TTs&&...) { +template (), + auto = loopholes::Injector{}, TypeList{}>{}> +consteval auto AddTypeToTag(TTs&&...) -> std::size_t { return Id; }; - template using GetMetaInfo = MetaInfo{}>{}))::Type>; @@ -47,12 +43,13 @@ using GetType = GetMetaInfo::Type; namespace impl { template -static consteval auto GetTypeListForTag(G g) requires (I == 0 || - requires {Magic(loopholes::Getter{}>{});}) { - if constexpr(I == 0 && !requires {Magic(loopholes::Getter{}>{});}) { +static consteval auto GetTypeListForTag(G g) + requires(I == 0 || requires { Magic(loopholes::Getter{}>{}); }) +{ + if constexpr(I == 0 && !requires { Magic(loopholes::Getter{}>{}); }) { return TypeList{}; } else { - if constexpr(requires{GetTypeListForTag(g);}) { + if constexpr(requires { GetTypeListForTag(g); }) { constexpr auto type = Magic(loopholes::Getter{}>{}); return GetTypeListForTag(g); } else { @@ -62,24 +59,18 @@ static consteval auto GetTypeListForTag(G g) requires (I == 0 || }; }; - -} // namespace impl +} // namespace impl template consteval auto GetTypeListForTag() { return impl::GetTypeListForTag(TypeList{}); }; -template < - typename Tag, - typename... Ts, - auto I = utempl::loopholes::CountValue() - 1 -> +template () - 1> consteval auto GetCurrentTagType() { return Magic(utempl::loopholes::Getter{}>()); }; - /* static_assert(kTypeId == 0); static_assert(kTypeId == 1); @@ -88,5 +79,4 @@ static_assert(std::is_same_v -struct Optional { +struct Optional { // NOLINT bool flag = false; union { char null; T _value; }; - inline constexpr Optional() = default; - inline constexpr Optional(const Optional&) = default; - inline constexpr Optional(Optional&&) = default; - inline constexpr Optional(T&& arg) : _value(std::move(arg)), flag(true) {}; - inline constexpr Optional(const T& arg) : _value(arg), flag(true) {}; - inline constexpr Optional(std::nullopt_t) : flag(false), null(0) {}; - inline constexpr auto has_value() const -> bool { + constexpr Optional() = default; + constexpr Optional(const Optional&) = default; + constexpr Optional(Optional&&) = default; + explicit constexpr Optional(T&& arg) : _value(std::move(arg)), flag(true) {}; + explicit constexpr Optional(const T& arg) : _value(arg), flag(true) {}; + explicit constexpr Optional(std::nullopt_t) : null(0) {}; + [[nodiscard]] constexpr auto has_value() const -> bool { return this->flag; }; - inline constexpr auto value() -> T& { + constexpr auto value() -> T& { return this->_value; }; - inline constexpr auto operator*() -> T& { + constexpr auto operator*() -> T& { return this->value(); }; - inline constexpr auto operator->() -> T* { + constexpr auto operator->() -> T* { return &this->value(); }; - inline constexpr auto value() const -> const T& { + constexpr auto value() const -> const T& { return this->_value; }; - inline constexpr auto operator*() const -> const T& { + constexpr auto operator*() const -> const T& { return this->value(); }; - inline constexpr auto operator->() const -> const T* { + constexpr auto operator->() const -> const T* { return &this->value(); }; - inline constexpr explicit operator bool() const { + constexpr explicit operator bool() const { return this->has_value(); }; }; -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/overloaded.hpp b/include/utempl/overloaded.hpp index ae8b3a4..5d116b7 100644 --- a/include/utempl/overloaded.hpp +++ b/include/utempl/overloaded.hpp @@ -3,11 +3,11 @@ namespace utempl { template -inline constexpr auto Overloaded(Fs&&... fs) { +constexpr auto Overloaded(Fs&&... fs) { struct Overloaded : public std::remove_cvref_t... { using Fs::operator()...; }; return Overloaded{std::forward(fs)...}; }; -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/reference_wrapper.hpp b/include/utempl/reference_wrapper.hpp index 5d3e172..8b1bd6b 100644 --- a/include/utempl/reference_wrapper.hpp +++ b/include/utempl/reference_wrapper.hpp @@ -6,10 +6,10 @@ struct ReferenceWrapper; template struct ReferenceWrapper { - inline constexpr auto operator*() -> T& { + constexpr auto operator*() -> T& { return this->value; }; - inline constexpr auto operator->() -> T* { + constexpr auto operator->() -> T* { return &this->value; }; T&& value; @@ -17,10 +17,10 @@ struct ReferenceWrapper { template struct ReferenceWrapper { - inline constexpr auto operator*() -> const T& { + constexpr auto operator*() -> const T& { return this->value; }; - inline constexpr auto operator->() -> const T* { + constexpr auto operator->() -> const T* { return &this->value; }; const T& value; @@ -28,14 +28,13 @@ struct ReferenceWrapper { template struct ReferenceWrapper { - - inline constexpr auto operator*() -> T& { + constexpr auto operator*() -> T& { return this->value; }; - inline constexpr auto operator->() -> T* { + constexpr auto operator->() -> T* { return &this->value; }; T& value; }; -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/tuple.hpp b/include/utempl/tuple.hpp index 8bfdc51..03bce28 100644 --- a/include/utempl/tuple.hpp +++ b/include/utempl/tuple.hpp @@ -1,6 +1,6 @@ #pragma once -#include #include +#include namespace utempl { template @@ -10,83 +10,86 @@ namespace impl { template struct TupleLeaf { T value; - inline constexpr bool operator==(const TupleLeaf&) const = default; + constexpr auto operator==(const TupleLeaf&) const -> bool = default; }; - template struct TupleHelper; template struct TupleHelper, Ts...> : public impl::TupleLeaf... { - inline constexpr bool operator==(const TupleHelper&) const = default; + constexpr auto operator==(const TupleHelper&) const -> bool = default; }; -} // namespace impl +} // namespace impl template struct Tuple; template -inline constexpr auto Get(Tuple& tuple) -> auto& requires (I < sizeof...(Ts)) { +constexpr auto Get(Tuple& tuple) -> auto& + requires(I < sizeof...(Ts)) +{ using Type = decltype(Get(TypeList{})); return static_cast&>(tuple).value; }; template -inline constexpr auto Get(const Tuple& tuple) -> const auto& requires (I < sizeof...(Ts)) { +constexpr auto Get(const Tuple& tuple) -> const auto& + requires(I < sizeof...(Ts)) +{ using Type = decltype(Get(TypeList{})); return static_cast&>(tuple).value; }; template -inline constexpr auto Get(Tuple&& tuple) -> decltype(auto) requires (I < sizeof...(Ts)) { +constexpr auto Get(Tuple&& tuple) -> decltype(auto) + requires(I < sizeof...(Ts)) +{ using Type = decltype(Get(TypeList{})); return std::move(static_cast&&>(tuple).value); }; template -inline constexpr auto Get(T&& arg) -> decltype(get(std::forward(arg))) { +constexpr auto Get(T&& arg) -> decltype(get(std::forward(arg))) { return get(std::forward(arg)); }; template struct Tuple : impl::TupleHelper, Ts...> { template - inline constexpr Tuple(TTs&&... args) : - impl::TupleHelper, Ts...>{{std::forward(args)}...} {}; + constexpr Tuple(TTs&&... args) /* NOLINT */ : impl::TupleHelper, Ts...>{{std::forward(args)}...} {}; template - inline constexpr Tuple(Fs&&... fs) requires (!std::invocable || ...) : - impl::TupleHelper, Ts...>{{fs()}...} {}; - inline constexpr Tuple(Ts&&... args) : - impl::TupleHelper, Ts...>{{args}...} {}; - inline constexpr Tuple(const Tuple&) = default; - inline constexpr Tuple(Tuple&&) = default; - inline constexpr Tuple(Tuple&) = default; - inline constexpr Tuple& operator=(const Tuple&) = default; - inline constexpr Tuple& operator=(Tuple&&) = default; - inline constexpr Tuple& operator=(Tuple&) = default; - inline constexpr Tuple() : - impl::TupleHelper, Ts...>() {}; - inline constexpr bool operator==(const Tuple& other) const { - return [&](std::index_sequence){ + constexpr Tuple(Fs&&... fs) // NOLINT + requires(!std::invocable || ...) + : impl::TupleHelper, Ts...>{{fs()}...} {}; + constexpr Tuple(Ts&&... args) : impl::TupleHelper, Ts...>{{args}...} {}; // NOLINT + constexpr Tuple(const Tuple&) = default; + constexpr Tuple(Tuple&&) = default; + constexpr Tuple(Tuple&) = default; + constexpr auto operator=(const Tuple&) -> Tuple& = default; + constexpr auto operator=(Tuple&&) -> Tuple& = default; + constexpr auto operator=(Tuple&) -> Tuple& = default; + constexpr Tuple() : impl::TupleHelper, Ts...>() {}; + constexpr auto operator==(const Tuple& other) const -> bool { + return [&](std::index_sequence) { return ((Get(*this) == Get(other)) && ...); }(std::index_sequence_for()); - }; + }; template - inline constexpr auto operator+(const Tuple& other) const -> Tuple { + constexpr auto operator+(const Tuple& other) const -> Tuple { return [&](std::index_sequence, std::index_sequence) -> Tuple { return {Get(*this)..., Get(other)...}; }(std::index_sequence_for(), std::index_sequence_for()); }; - + template - inline constexpr auto operator[](Wrapper) const -> const auto& { + constexpr auto operator[](Wrapper) const -> const auto& { return Get(*this); }; - + template - inline constexpr auto operator[](Wrapper) -> auto& { + constexpr auto operator[](Wrapper) -> auto& { return Get(*this); }; }; @@ -94,10 +97,10 @@ struct Tuple : impl::TupleHelper, Ts...> { template <> struct Tuple<> { template - inline constexpr auto operator+(const Tuple& other) const -> Tuple { + constexpr auto operator+(const Tuple& other) const -> Tuple { return other; }; - inline constexpr auto operator==(const Tuple<>&) const { + constexpr auto operator==(const Tuple<>&) const { return true; }; }; @@ -109,4 +112,4 @@ consteval auto ListFromTuple(Tuple) -> TypeList { return {}; }; -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/type_list.hpp b/include/utempl/type_list.hpp index 840afde..bb1c075 100644 --- a/include/utempl/type_list.hpp +++ b/include/utempl/type_list.hpp @@ -1,10 +1,9 @@ #pragma once -#include -#include -#include -#include #include - +#include +#include +#include +#include namespace utempl { @@ -24,10 +23,12 @@ inline constexpr auto kTypeList = TypeList{}; template concept IsTypeList = Overloaded( - [](TypeList>) {return true;}, - [](auto&&) {return false;} -)(kType>); - + [](TypeList>) { + return true; + }, + [](auto&&) { + return false; + })(kType>); namespace impl { @@ -40,9 +41,7 @@ struct Caster {}; template struct Caster, Ts...> : IndexedType... {}; - - -} // namespace impl +} // namespace impl template consteval auto operator==(const TypeList& first, const TypeList& second) -> bool { return std::same_as; @@ -54,8 +53,7 @@ consteval auto operator+(const TypeList&, const TypeList&) -> Typ }; template -consteval auto Get(TypeList) -> decltype( - [](impl::IndexedType&&) -> T { +consteval auto Get(TypeList) -> decltype([](impl::IndexedType&&) -> T { }(impl::Caster, Ts...>{})); template @@ -70,7 +68,6 @@ consteval auto Find(TypeList) -> std::size_t { return std::ranges::find(arr, true) - arr.begin(); }; - template consteval auto Reverse(TypeList list) { return [&](std::index_sequence) -> TypeList(list))...> { @@ -84,24 +81,20 @@ consteval auto Transform(TypeList, auto&& f) -> TypeList consteval auto FilterTypeList(TypeList, auto&& f) { - return ( - (kTypeList<> - + [](auto&& list) { - if constexpr(decltype(f(list))::kValue) { - return list; - } else { - return kTypeList<>; - } - }(kType) - ) - + ...); + return ((kTypeList<> + + [](auto&& list) { + if constexpr(decltype(f(list))::kValue) { + return list; + } else { + return kTypeList<>; + } + }(kType)) + + ...); }; - template consteval auto Size(TypeList) -> std::size_t { return sizeof...(Ts); }; - -} // namespace utempl +} // namespace utempl diff --git a/include/utempl/utils.hpp b/include/utempl/utils.hpp index abd068e..34d3d86 100644 --- a/include/utempl/utils.hpp +++ b/include/utempl/utils.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include namespace utempl { @@ -15,7 +15,7 @@ struct Wrapper { inline constexpr auto operator==(auto&& arg) const { return arg == Value; }; - consteval operator decltype(Value)() const { + consteval operator decltype(Value)() const { // NOLINT return Value; }; consteval auto operator*() const { @@ -32,25 +32,23 @@ template struct kSeq { template friend constexpr auto operator|(F&& f, const kSeq&) { - return [&](std::index_sequence){ + return [&](std::index_sequence) { return std::forward(f)(kWrapper...); }(std::make_index_sequence()); }; }; -} // namespace impl +} // namespace impl template inline constexpr impl::kSeq kSeq; - - template consteval auto ParseNumber() -> T { T response{}; for(const auto& c : string) { - if (c >= '0' && c <= '9') { - response = response * 10 + (c - '0'); + if(c >= '0' && c <= '9') { + response = response * 10 + (c - '0'); // NOLINT }; }; return response; @@ -62,10 +60,12 @@ consteval auto operator"" _c() { return Wrapper({cs...})>()>{}; }; -} // namespace literals +} // namespace literals template -inline constexpr auto Arg(Ts&&... args) -> decltype(auto) requires (I < sizeof...(Ts)) { +inline constexpr auto Arg(Ts&&... args) -> decltype(auto) + requires(I < sizeof...(Ts)) +{ return [&](std::index_sequence) -> decltype(auto) { return [](decltype(Caster(Is))..., auto&& response, ...) -> decltype(auto) { return response; @@ -73,10 +73,9 @@ inline constexpr auto Arg(Ts&&... args) -> decltype(auto) requires (I < sizeof.. }(std::make_index_sequence()); }; - template inline constexpr auto Times(auto&& f) { - [&](std::index_sequence){ + [&](std::index_sequence) { (Arg<0>(f, Is)(), ...); }(std::make_index_sequence()); }; @@ -87,7 +86,6 @@ inline constexpr std::size_t kTupleSize = []() -> std::size_t { return 0; }(); - template inline constexpr std::size_t kTupleSize = kTupleSize>; @@ -104,8 +102,7 @@ template