Skip to content

Commit 23444ad

Browse files
zander-xyzjhunsaker
authored andcommitted
is_specialization_of_v, is_monad_trait_v, is_evm_trait_v
1 parent f269912 commit 23444ad

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

category/core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ check_if_boost_fiber_needs_ucontext_macro()
6666
# ##############################################################################
6767

6868
add_library(
69-
monad_core
70-
OBJECT
69+
monad_core OBJECT
7170
# config
7271
"config.hpp"
7372
# core
@@ -89,6 +88,7 @@ add_library(
8988
"hash.hpp"
9089
"hex_literal.hpp"
9190
"int.hpp"
91+
"is_specialization_of.hpp"
9292
"keccak.c"
9393
"keccak.h"
9494
"keccak.hpp"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (C) 2025 Category Labs, Inc.
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2098r0.pdf
17+
18+
#pragma once
19+
20+
#include <category/core/config.hpp>
21+
22+
MONAD_NAMESPACE_BEGIN
23+
24+
template <template <auto...> class Template, typename T>
25+
struct is_specialization_of : std::false_type
26+
{
27+
};
28+
29+
template <template <auto...> class Template, auto... Args>
30+
struct is_specialization_of<Template, Template<Args...>> : std::true_type
31+
{
32+
};
33+
34+
template <template <auto...> class Template, typename T>
35+
inline constexpr bool is_specialization_of_v =
36+
is_specialization_of<Template, T>::value;
37+
38+
MONAD_NAMESPACE_END

category/execution/ethereum/evm_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,11 @@ TYPED_TEST(TraitsTest, create_inside_delegated_call)
688688
auto const result = h.call(m);
689689
// CREATE should fail on Monad chains and succeed on Ethereum chains
690690
if constexpr (TestFixture::Trait::can_create_inside_delegated()) {
691-
static_assert(TestFixture::is_evmc_revision());
691+
static_assert(TestFixture::is_evm_trait());
692692
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
693693
}
694694
else {
695-
static_assert(TestFixture::is_monad_revision());
695+
static_assert(TestFixture::is_monad_trait());
696696
EXPECT_EQ(result.status_code, EVMC_FAILURE);
697697
}
698698
}
@@ -802,11 +802,11 @@ TYPED_TEST(TraitsTest, create2_inside_delegated_call_via_delegatecall)
802802
auto const result = h.call(m);
803803
// CREATE2 should fail on Monad chains and succeed on Ethereum chains
804804
if constexpr (TestFixture::Trait::can_create_inside_delegated()) {
805-
static_assert(TestFixture::is_evmc_revision());
805+
static_assert(TestFixture::is_evm_trait());
806806
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
807807
}
808808
else {
809-
static_assert(TestFixture::is_monad_revision());
809+
static_assert(TestFixture::is_monad_trait());
810810
EXPECT_EQ(result.status_code, EVMC_FAILURE);
811811
}
812812
}

category/vm/evm/traits.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#pragma once
1717

18+
#include <category/core/is_specialization_of.hpp>
1819
#include <category/vm/core/assert.h>
1920
#include <category/vm/evm/monad/revision.h>
2021

@@ -289,4 +290,16 @@ namespace monad
289290
// complete.
290291
using evm_base = EvmTraits<MonadTraits::evm_rev()>;
291292
};
293+
294+
template <typename T>
295+
inline constexpr bool is_evm_trait_v = is_specialization_of_v<EvmTraits, T>;
296+
297+
template <typename T>
298+
inline constexpr bool is_monad_trait_v =
299+
is_specialization_of_v<MonadTraits, T>;
300+
301+
static_assert(is_monad_trait_v<MonadTraits<MONAD_ZERO>> == true);
302+
static_assert(is_monad_trait_v<EvmTraits<EVMC_FRONTIER>> == false);
303+
static_assert(is_evm_trait_v<MonadTraits<MONAD_ZERO>> == false);
304+
static_assert(is_evm_trait_v<EvmTraits<EVMC_FRONTIER>> == true);
292305
}

test/unit/common/include/monad/test/traits_test.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ struct TraitsTest : public ::testing::Test
122122
}
123123
}
124124

125-
static consteval bool is_monad_revision() noexcept
125+
using Trait = decltype(get_trait());
126+
127+
static consteval bool is_monad_trait() noexcept
126128
{
127-
return std::same_as<typename T::value_type, monad_revision>;
129+
return monad::is_monad_trait_v<Trait>;
128130
}
129131

130-
static consteval bool is_evmc_revision() noexcept
132+
static consteval bool is_evm_trait() noexcept
131133
{
132-
return std::same_as<typename T::value_type, evmc_revision>;
134+
return monad::is_evm_trait_v<Trait>;
133135
}
134-
135-
using Trait = decltype(get_trait());
136136
};
137137

138138
TYPED_TEST_SUITE(

0 commit comments

Comments
 (0)