-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathenum_example.cpp
More file actions
67 lines (52 loc) · 1.93 KB
/
enum_example.cpp
File metadata and controls
67 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/meta.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2021-2025, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <meta.hpp/meta_all.hpp>
#include <doctest/doctest.h>
#include <fmt/core.h>
namespace
{
enum class align {
left,
right,
center,
};
}
TEST_CASE("meta/meta_examples/enum/type") {
namespace meta = meta_hpp;
// 'align' enumeration type registration
meta::enum_<align>()
.evalue_("left", align::left)
.evalue_("right", align::right)
.evalue_("center", align::center);
// resolves a enumeration type by enumerator
const meta::enum_type align_type = meta::resolve_type(align::left);
// also, it can be resolved by static enumeration type
CHECK(align_type == meta::resolve_type<align>());
// allows to know the underlying type
CHECK(align_type.get_underlying_type() == meta::resolve_type<int>());
// prints all enumerators
fmt::print("* align\n");
for ( const meta::evalue& evalue : align_type.get_evalues() ) {
fmt::print(" - {}/{}\n",
evalue.get_name(),
evalue.get_underlying_value().as<int>());
}
// Output:
// * align
// - center/2
// - left/0
// - right/1
}
TEST_CASE("meta/meta_examples/enum/usage") {
namespace meta = meta_hpp;
const align e = align::center;
// resolves a enumeration type by enumerator instance 'e'
const meta::enum_type align_type = meta::resolve_type(e);
// converts the enumerator to its name
CHECK(align_type.value_to_evalue(e).get_name() == "center");
// ... and back again
CHECK(align_type.name_to_evalue("center").get_value().as<align>() == e);
}