-
Notifications
You must be signed in to change notification settings - Fork 4
Split detail into logical parts #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cda7af4
Clean up comments and eradicate unused noexcept from fn_ptr_t
jbcoe 04aa3da
Split detail out of protocol header
jbcoe f91b5c9
Add support for operator[]
jbcoe ab7cc94
Add operator -> and operator *
jbcoe cf8ef9e
Code cleanup (remove unusable args) and add missing tests for mangling
jbcoe e4fec91
Split detail into logical parts
jbcoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| /* Copyright (c) 2025 The XYZ Protocol Authors. All Rights Reserved. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| the Software, and to permit persons to whom the Software is furnished to do so, | ||
| subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| ==============================================================================*/ | ||
| #ifndef XYZ_PROTOCOL_CONFORMANCE_HH_ | ||
| #define XYZ_PROTOCOL_CONFORMANCE_HH_ | ||
|
|
||
| #include <algorithm> | ||
| #include <meta> | ||
| #include <ranges> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace xyz::detail { | ||
|
|
||
| template <std::meta::operators Operator> | ||
| consteval bool is_operator(std::meta::info function) { | ||
| return is_operator_function(function) && operator_of(function) == Operator; | ||
| } | ||
|
|
||
| // Returns `true` if `a` and `b` are the same operators or have the same | ||
| // identifier. Otherwise returns false. | ||
| consteval bool same_name(std::meta::info a, std::meta::info b) { | ||
| if (has_identifier(a) && has_identifier(b)) { | ||
| return identifier_of(a) == identifier_of(b); | ||
| } | ||
| if (is_operator_function(a) && is_operator_function(b)) { | ||
| return operator_of(a) == operator_of(b); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Returns `true` if the member functions `candidate` and `interface` have | ||
| // the same name, de-aliased return type and de-aliased parameter types. | ||
| consteval bool same_name_and_parameters(std::meta::info candidate, | ||
| std::meta::info interface) { | ||
| if (!same_name(candidate, interface)) return false; | ||
| if (dealias(return_type_of(interface)) != dealias(return_type_of(candidate))) | ||
| return false; | ||
| auto dealiased_type_of = [](std::meta::info parameter) { | ||
| return dealias(type_of(parameter)); | ||
| }; | ||
| return std::ranges::equal(parameters_of(interface), parameters_of(candidate), | ||
| {}, dealiased_type_of, dealiased_type_of); | ||
| } | ||
|
|
||
| // Returns `true` if the member functions `candidate` and `interface` have | ||
| // the same name, reference qualifiers, de-aliased return type and de-aliased | ||
| // parameter types; const and noexcept are not compared. | ||
| consteval bool same_signature_ignoring_const(std::meta::info candidate, | ||
| std::meta::info interface) { | ||
| if (is_lvalue_reference_qualified(interface) != | ||
| is_lvalue_reference_qualified(candidate)) | ||
| return false; | ||
| if (is_rvalue_reference_qualified(interface) != | ||
| is_rvalue_reference_qualified(candidate)) | ||
| return false; | ||
| return same_name_and_parameters(candidate, interface); | ||
| } | ||
|
|
||
| // Returns `true` if `candidate is a function with an explicit object parameter, | ||
| // the member functions `candidate` and `interface` have the | ||
| // same name, de-aliased return type and parameter types, and const / reference | ||
| // qualification. | ||
| consteval bool same_function_with_explicit_object(std::meta::info candidate, | ||
| std::meta::info interface) { | ||
| if (!same_name(candidate, interface)) return false; | ||
| if (dealias(return_type_of(candidate)) != dealias(return_type_of(interface))) | ||
| return false; | ||
|
|
||
| const auto params = parameters_of(candidate); | ||
| if (params.empty() || !is_explicit_object_parameter(params.front())) | ||
| return false; | ||
|
|
||
| if (is_const(interface) != | ||
| is_const(remove_reference(type_of(params.front())))) | ||
| return false; | ||
|
|
||
| return std::ranges::equal(parameters_of(interface), | ||
| params | std::views::drop(1), {}, | ||
| std::meta::type_of, std::meta::type_of); | ||
| } | ||
|
|
||
| // Returns `true` if the `candidate` member function is consistent with the | ||
| // `interface` member function for the purposes of structural subtyping; | ||
| // otherwise returns `false`. | ||
| consteval bool member_function_conforms_to(std::meta::info candidate, | ||
| std::meta::info interface) { | ||
| if (is_static_member(candidate)) { | ||
| // A static candidate has no object parameter, so it satisfies any const | ||
| // or reference qualification of `interface`. | ||
| if (!same_name_and_parameters(candidate, interface)) return false; | ||
| } else if (same_function_with_explicit_object(candidate, interface)) { | ||
| // No additional conditions. | ||
| } else { | ||
| if (!same_signature_ignoring_const(candidate, interface)) return false; | ||
| // `const` qualifiers must match. | ||
| if (is_const(interface) != is_const(candidate)) return false; | ||
| } | ||
| // If interface is `noexcept`, `candidate` must be noexcept. | ||
| return !is_noexcept(interface) || is_noexcept(candidate); | ||
| } | ||
|
|
||
| // Per ISO C++ ([expr.prim.lambda.closure]), closure types are unique, unnamed, | ||
| // non-union class types. | ||
| // The closure type is not an aggregate type. | ||
| // This concept will also match an unnamed class type with a single | ||
| // `operator()`. | ||
| // | ||
| // In practice a lambda has no base classes and no template arguments, although | ||
| // there is no wording in the standard to guarantee this. | ||
| // | ||
| // TODO(jbcoe): Refine this concept to match only lambdas. | ||
| template <typename T> | ||
| concept is_maybe_lambda = | ||
| is_class_type(dealias(^^T)) && !has_identifier(dealias(^^T)) && | ||
| !is_aggregate_type(dealias(^^T)) && !has_template_arguments(dealias(^^T)) && | ||
| bases_of(dealias(^^T), std::meta::access_context::unprivileged()).empty() && | ||
| requires { &T::operator(); }; | ||
|
|
||
| // The named, non-special member functions and call operators of `Type`, | ||
| // static or not, in declaration order. | ||
| template <std::meta::info Type> | ||
| consteval auto conformance_candidate_infos() { | ||
| auto named = | ||
| members_of(Type, std::meta::access_context::unprivileged()) | | ||
| std::views::filter(std::meta::is_function) | | ||
| std::views::filter([](std::meta::info member) consteval { | ||
| return has_identifier(member) || | ||
| is_operator<std::meta::operators::op_star>(member) || | ||
|
jbcoe marked this conversation as resolved.
|
||
| is_operator<std::meta::operators::op_arrow>(member) || | ||
| is_operator<std::meta::operators::op_parentheses>(member) || | ||
| is_operator<std::meta::operators::op_square_brackets>(member); | ||
| }); | ||
| std::vector<std::meta::info> result(std::ranges::begin(named), | ||
| std::ranges::end(named)); | ||
| // GCC Workaround: Per [meta.reflection.member.queries], a closure type's | ||
| // function call operator is members-of-eligible, but GCC16's `members_of` | ||
| // does not yet enumerate it, leaving `result` empty for lambdas; name the | ||
| // operator directly as a fallback. | ||
| using T = typename[:Type:]; | ||
| if constexpr (is_maybe_lambda<T>) { | ||
| if (result.empty()) result.push_back(^^T::operator()); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <std::meta::info Type> | ||
| constexpr inline auto conformance_candidates_of = | ||
| std::define_static_array(conformance_candidate_infos<Type>()); | ||
|
|
||
| // The named, non-static, non-special member functions and call operators of | ||
| // `Type`, static or not, in declaration order. Ref-qualified functions are | ||
| // unsupported on protocol interfaces. | ||
| template <std::meta::info Type> | ||
| consteval std::vector<std::meta::info> protocol_interface_function_infos() { | ||
| std::vector<std::meta::info> result; | ||
| for (std::meta::info member : conformance_candidates_of<Type>) { | ||
| if (is_static_member(member)) continue; | ||
| if (is_lvalue_reference_qualified(member) || | ||
| is_rvalue_reference_qualified(member)) { | ||
| std::string name = has_identifier(member) | ||
| ? std::string(identifier_of(member)) | ||
| : std::string(display_string_of(member)); | ||
| throw std::runtime_error("ref-qualified member function '" + name + | ||
| "' is not supported in a protocol interface"); | ||
| } | ||
| result.push_back(member); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <std::meta::info Type> | ||
| constexpr inline auto protocol_interface_functions_of = | ||
| std::define_static_array(protocol_interface_function_infos<Type>()); | ||
|
|
||
| // Finds the member of `CandidateType` that structurally conforms to | ||
| // `Member`. | ||
| template <std::meta::info Member, std::meta::info CandidateType> | ||
| consteval std::meta::info find_conforming_member() { | ||
| for (std::meta::info candidate : conformance_candidates_of<CandidateType>) { | ||
| if (member_function_conforms_to(candidate, Member)) return candidate; | ||
| } | ||
| std::unreachable(); | ||
| } | ||
|
|
||
| } // namespace xyz::detail | ||
|
|
||
| #endif // XYZ_PROTOCOL_CONFORMANCE_HH_ | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.