Skip to content

Commit

Permalink
add same_as concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Jul 11, 2024
1 parent bb8b240 commit f75daa7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,22 @@ void runTests() { form::run_tests<^for_tests>(); }
*/

```
## extension of same_as concept to templates
concept `form::same_as` checks if type represent specific template
``` c++
template <typename T> int foo(T) { return -1; }
template <typename T>
requires form::same_as<T, ^std::complex>
int foo(T) {
return 1;
}
foo(std::complex<float>{1,1}); //1
```
5 changes: 4 additions & 1 deletion include/form/form.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ template <typename S> consteval std::size_t get_padding() {
std::size_t padding{0};
std::size_t pointer{offset_of(nonstatic_data_members_of(^S)[0])};

[: util::expand(nonstatic_data_members_of(^S)):] >> [&, i = 0]<auto e>() {
[:util::expand(nonstatic_data_members_of(^S)):] >> [&, i = 0]<auto e>() {
if (pointer == offset_of(e))
pointer += size_of(e);
else {
Expand All @@ -123,6 +123,9 @@ concept no_padding =
std::same_as<std::integral_constant<std::size_t, get_padding<T>()>,
std::integral_constant<std::size_t, 0>>;

template <typename T, auto refl>
concept same_as = template_of(^T) == refl;

} // namespace form

template <> struct std::formatter<std::basic_string_view<char8_t>> {
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
#include <boxed-cpp/boxed.hpp>
#include <complex>
#include <cstdio>
#include <form/form.h>
#include <print>
Expand Down
1 change: 0 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

int main() {
form::run_tests<^form::tests>();
form::run_tests<^form::round_trip_tests>();
form::run_tests<^form::examples>();
}
20 changes: 16 additions & 4 deletions src/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once
#include <algorithm>
#include <boxed-cpp/boxed.hpp>
#include <complex>
#include <form/form.h>
#include <print>
#include <thread>
Expand Down Expand Up @@ -173,6 +174,18 @@ template <typename T> constexpr auto CreateClass() {
// clang-format on

} // namespace detail

namespace form_same_as {
template <typename T> int foo(T) { return -1; }

template <typename T>
requires form::same_as<T, ^std::complex>
int foo(T) {
return 1;
}

} // namespace form_same_as

namespace form::tests {

void testZ() {
Expand Down Expand Up @@ -230,9 +243,8 @@ void testSJson() {
s.j = 2;
std::println("{}", form::format_json(s));
}
} // namespace form::tests

namespace form::round_trip_tests {
bool CheckSameAs() { return form_same_as::foo(std::complex<float>{1, 1}) == 1; }

bool compareTrue() {
return form::compare(3.0, 3.0) && form::compare(3, 3) &&
Expand Down Expand Up @@ -312,7 +324,7 @@ bool ConfigRoundTrip() {
return round_trip(config);
}

} // namespace form::round_trip_tests
} // namespace form::tests

namespace list {
struct CancelSelection {};
Expand Down Expand Up @@ -349,8 +361,8 @@ template <form::no_padding T> void foo(T t) { std::println("Without padding"); }
template <typename T> void foo(T t) {
std::println("With padding: {}", form::get_padding<T>());
}

namespace form::examples {

bool VariantCreate() {
list_variant v{list::CancelSelection{}};
return std::holds_alternative<list::CancelSelection>(v);
Expand Down

0 comments on commit f75daa7

Please # to comment.