Skip to content

Commit

Permalink
Add ToTuple for && case, distinguished for const&.
Browse files Browse the repository at this point in the history
  • Loading branch information
helly25 committed Oct 1, 2024
1 parent 6798471 commit d9b902b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
27 changes: 20 additions & 7 deletions mbo/types/extend.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#include <type_traits>

#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 {

Expand All @@ -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<typename T, typename... Extender>
Expand Down
10 changes: 8 additions & 2 deletions mbo/types/internal/extender.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename ActualType>
struct ExtendBase {
using Type = ActualType; // Used for type chaining in `UseExtender`
Expand All @@ -53,7 +57,9 @@ struct ExtendBase {
}

protected: // DO NOT expose anything publicly.
auto ToTuple() const { return StructToTuple(static_cast<const ActualType&>(*this)); }
auto ToTuple() const & { return StructToTuple(static_cast<const ActualType&>(*this)); }

auto ToTuple() && { return StructToTuple(std::move(*this)); }

private: // DO NOT expose anything publicly.
template<typename U, typename Extender>
Expand Down
2 changes: 1 addition & 1 deletion mbo/types/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ concept IsVariant = types_internal::IsVariantImpl<T>::value;
//
// This is used in mbo::types::Extend<...>::ConstructFrom{Tuple|Args}.
//
// See: https://godbolt.org/z/Wjq38PP6n
// See: https://godbolt.org/z/3nnsG6bEb
template<typename T, typename... Args>
concept IsConstructibleWithEmptyBaseAndArgs = requires(Args&&... args) {
{ T{{}, {std::forward<Args>(args)}...} };
Expand Down

0 comments on commit d9b902b

Please sign in to comment.