mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-16 22:17:02 +07:00
add enum example
This commit is contained in:
1
TODO.md
1
TODO.md
@@ -9,3 +9,4 @@
|
|||||||
- add enum_type::create, number_type::create, and so on.
|
- add enum_type::create, number_type::create, and so on.
|
||||||
- add return value policy
|
- add return value policy
|
||||||
- add is_invocable_with by dynamic types
|
- add is_invocable_with by dynamic types
|
||||||
|
- add metadata to every type and state
|
||||||
|
|||||||
@@ -8,8 +8,52 @@
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
enum class align {
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
center,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("meta/meta_examples/enums") {
|
TEST_CASE("meta/meta_examples/enums/type") {
|
||||||
namespace meta = meta_hpp;
|
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 ( auto&& [index, evalue] : align_type.get_evalues() ) {
|
||||||
|
fmt::print(" - {} = {}\n", index.name, evalue.get_underlying_value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("meta/meta_examples/enums/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_name(e).value() == "center");
|
||||||
|
|
||||||
|
// also, it works with dynamic value types
|
||||||
|
CHECK(align_type.value_to_name(meta::value{e}).value() == "center");
|
||||||
|
|
||||||
|
// ... and back again
|
||||||
|
CHECK(align_type.name_to_value("center").value() == e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,20 @@
|
|||||||
#include <meta.hpp/meta_all.hpp>
|
#include <meta.hpp/meta_all.hpp>
|
||||||
#include <doctest/doctest.h>
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
|
||||||
|
#pragma clang diagnostic ignored "-Wsigned-enum-bitfield"
|
||||||
|
#pragma clang diagnostic ignored "-Wswitch-enum"
|
||||||
|
#pragma clang diagnostic ignored "-Wundefined-func-template"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include <fmt/ostream.h>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|||||||
Reference in New Issue
Block a user