From d9b902b6e332e98c070454715f75c71689441f07 Mon Sep 17 00:00:00 2001 From: helly25 Date: Tue, 1 Oct 2024 22:41:50 +0200 Subject: [PATCH] Add `ToTuple` for `&&` case, distinguished for `const&`. --- mbo/types/extend.h | 27 ++++++++++++++++++++------- mbo/types/internal/extender.h | 10 ++++++++-- mbo/types/traits.h | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mbo/types/extend.h b/mbo/types/extend.h index e613adb..2472676 100644 --- a/mbo/types/extend.h +++ b/mbo/types/extend.h @@ -18,9 +18,9 @@ #include -#include "mbo/types/extender.h" // IWYU pragma: export -#include "mbo/types/internal/extend.h" // IWYU pragma: export -#include "mbo/types/internal/extender.h" +#include "mbo/types/extender.h" // IWYU pragma: export +#include "mbo/types/internal/extend.h" // IWYU pragma: export +#include "mbo/types/internal/extender.h" // IWYU pragma: export namespace mbo::types { @@ -42,11 +42,24 @@ namespace mbo::types { // std::cout << Name{.first = "First", .last = "Last"} << std::endl; // ``` // -// The struct `Name` automatically gains the ability to print, stream and -// compare itself. In the above example `{"First", "Last"}` will be printed. +// The struct `Name` automatically gains the ability to print, stream, compare +// and hash itself. In the above example `{"First", "Last"}` will be printed. // If compiled on Clang it will print `{first: "First", last: "Last"}` (see -// AbslStringify for restrictions). Also, the field names can be suppressed -// by adding `using MboTypesStringifyDoNotPrintFieldNames = void;` to the type. +// Stringify and AbslStringify for restrictions). Also, the field names can be +// suppressed by adding `using MboTypesStringifyDoNotPrintFieldNames = void;` +// to the type. +// +// Additional base operations: +// * `static ConstructFromArgs`: Construct the type from an argument list. +// * `static ConstructFromTuple`: Construct the type from a tuple. +// +// Additional API extension points, common to all `Extend`ed types: +// * type `MboTypesStringifyDoNotPrintFieldNames, +// * function `MboTypesStringifyFieldNames`, and +// * function `MboTypesStringifyOptions`. +// +// NOTE: The `Stringify` extension API point `MboTypesStringifySupport` is not +// allowed here. // // NOTE: No member may be an anonymous union or struct. template diff --git a/mbo/types/internal/extender.h b/mbo/types/internal/extender.h index ef4fe16..a640abe 100644 --- a/mbo/types/internal/extender.h +++ b/mbo/types/internal/extender.h @@ -31,7 +31,11 @@ namespace mbo::types { // Base Extender implementation for CRTP functionality injection. -// This must always be present. +// +// This must be the base of every `Extend`ed struct's CRTP chain. +// +// This is only not in the internal namespace `types_internal` to shorten the +// resulting type names. template struct ExtendBase { using Type = ActualType; // Used for type chaining in `UseExtender` @@ -53,7 +57,9 @@ struct ExtendBase { } protected: // DO NOT expose anything publicly. - auto ToTuple() const { return StructToTuple(static_cast(*this)); } + auto ToTuple() const & { return StructToTuple(static_cast(*this)); } + + auto ToTuple() && { return StructToTuple(std::move(*this)); } private: // DO NOT expose anything publicly. template diff --git a/mbo/types/traits.h b/mbo/types/traits.h index 9e257d7..466d8f8 100644 --- a/mbo/types/traits.h +++ b/mbo/types/traits.h @@ -335,7 +335,7 @@ concept IsVariant = types_internal::IsVariantImpl::value; // // This is used in mbo::types::Extend<...>::ConstructFrom{Tuple|Args}. // -// See: https://godbolt.org/z/Wjq38PP6n +// See: https://godbolt.org/z/3nnsG6bEb template concept IsConstructibleWithEmptyBaseAndArgs = requires(Args&&... args) { { T{{}, {std::forward(args)}...} };