2020-02-17 23:08:46 +00:00
|
|
|
//
|
2020-03-20 03:24:06 +00:00
|
|
|
// Enum.hpp
|
2020-02-17 23:08:46 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/02/2020.
|
|
|
|
// Copyright © 2020 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2020-02-17 23:08:46 +00:00
|
|
|
|
2020-03-14 04:22:23 +00:00
|
|
|
#include <algorithm>
|
2020-02-17 23:08:46 +00:00
|
|
|
#include <cctype>
|
2020-03-10 02:05:31 +00:00
|
|
|
#include <string>
|
2020-03-11 03:36:52 +00:00
|
|
|
#include <typeindex>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
2020-02-17 23:08:46 +00:00
|
|
|
|
|
|
|
namespace Reflection {
|
|
|
|
|
2020-03-12 03:28:38 +00:00
|
|
|
#define ReflectableEnum(Name, ...) \
|
|
|
|
enum class Name { __VA_ARGS__ }; \
|
2020-03-10 02:05:31 +00:00
|
|
|
constexpr static const char *__declaration##Name = #__VA_ARGS__;
|
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
#define EnumDeclaration(Name) #Name, __declaration##Name
|
2020-03-10 02:05:31 +00:00
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
#define AnnounceEnum(Name) ::Reflection::Enum::declare<Name>(EnumDeclaration(Name))
|
2020-03-17 03:25:05 +00:00
|
|
|
#define AnnounceEnumNS(Namespace, Name) ::Reflection::Enum::declare<Namespace::Name>(#Name, Namespace::__declaration##Name)
|
2020-03-12 02:06:16 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
This provides a very slight version of enum reflection; you can introspect only:
|
|
|
|
|
|
|
|
* enums have been registered, along with the text of their declarations;
|
|
|
|
* provided that those enums do not declare specific values for their members.
|
|
|
|
|
|
|
|
The macros above help avoid duplication of the declaration, making this just mildly less
|
|
|
|
terrible than it might have been.
|
|
|
|
|
|
|
|
No guarantees of speed or any other kind of efficiency are offered.
|
|
|
|
*/
|
2020-03-11 03:36:52 +00:00
|
|
|
class Enum {
|
|
|
|
public:
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
Registers @c name and the entries within @c declaration for the enum type @c Type.
|
|
|
|
|
|
|
|
Assuming the caller used the macros above, a standard pattern where both things can be placed in
|
|
|
|
the same namespace might look like:
|
|
|
|
|
|
|
|
ReflectableEnum(MyEnum, int, A, B, C);
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
AnnounceEnum(MyEnum)
|
|
|
|
|
|
|
|
If AnnounceEnum cannot be placed into the same namespace as ReflectableEnum, see the
|
|
|
|
EnumDeclaration macro.
|
|
|
|
*/
|
|
|
|
template <typename Type> static void declare(const char *name, const char *declaration) {
|
2020-02-17 23:08:46 +00:00
|
|
|
const char *d_ptr = declaration;
|
|
|
|
|
2020-03-11 03:36:52 +00:00
|
|
|
std::vector<std::string> result;
|
2020-02-17 23:08:46 +00:00
|
|
|
while(true) {
|
|
|
|
// Skip non-alphas, and exit if the terminator is found.
|
|
|
|
while(*d_ptr && !isalpha(*d_ptr)) ++d_ptr;
|
|
|
|
if(!*d_ptr) break;
|
|
|
|
|
|
|
|
// Note the current location and proceed for all alphas and digits.
|
|
|
|
const auto start = d_ptr;
|
|
|
|
while(isalpha(*d_ptr) || isdigit(*d_ptr)) ++d_ptr;
|
|
|
|
|
|
|
|
// Add a string view.
|
2020-03-11 03:36:52 +00:00
|
|
|
result.emplace_back(std::string(start, size_t(d_ptr - start)));
|
2020-02-17 23:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:38:29 +00:00
|
|
|
members_by_type_.emplace(std::make_pair(std::type_index(typeid(Type)), result));
|
|
|
|
names_by_type_.emplace(std::make_pair(std::type_index(typeid(Type)), std::string(name)));
|
2020-02-17 23:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
@returns the declared name of the enum @c Type if it has been registered; the empty string otherwise.
|
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
template <typename Type> static const std::string &name() {
|
2020-03-12 02:06:16 +00:00
|
|
|
return name(typeid(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns the declared name of the enum with type_info @c type if it has been registered; the empty string otherwise.
|
|
|
|
*/
|
2020-03-14 03:38:29 +00:00
|
|
|
static const std::string &name(std::type_index type) {
|
|
|
|
const auto entry = names_by_type_.find(type);
|
2020-03-12 02:06:16 +00:00
|
|
|
if(entry == names_by_type_.end()) return empty_string_;
|
|
|
|
return entry->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns the number of members of the enum @c Type if it has been registered; 0 otherwise.
|
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
template <typename Type> static size_t size() {
|
2020-03-11 03:36:52 +00:00
|
|
|
return size(typeid(Type));
|
|
|
|
}
|
2020-02-17 23:08:46 +00:00
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
|
|
|
@returns the number of members of the enum with type_info @c type if it has been registered; @c std::string::npos otherwise.
|
|
|
|
*/
|
2020-03-14 03:38:29 +00:00
|
|
|
static size_t size(std::type_index type) {
|
|
|
|
const auto entry = members_by_type_.find(type);
|
2020-03-12 02:06:16 +00:00
|
|
|
if(entry == members_by_type_.end()) return std::string::npos;
|
2020-03-11 03:36:52 +00:00
|
|
|
return entry->second.size();
|
|
|
|
}
|
2020-02-17 23:08:46 +00:00
|
|
|
|
2020-03-11 03:36:52 +00:00
|
|
|
/*!
|
2020-03-12 02:06:16 +00:00
|
|
|
@returns A @c std::string name for the enum value @c e.
|
2020-03-11 03:36:52 +00:00
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
template <typename Type> static const std::string &to_string(Type e) {
|
2020-03-19 02:31:32 +00:00
|
|
|
return to_string(typeid(Type), int(e));
|
2020-03-12 02:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns A @c std::string name for the enum value @c e from the enum with type_info @c type.
|
|
|
|
*/
|
2020-03-19 02:31:32 +00:00
|
|
|
static const std::string &to_string(std::type_index type, int e) {
|
2020-03-14 03:38:29 +00:00
|
|
|
const auto entry = members_by_type_.find(type);
|
2020-03-11 03:36:52 +00:00
|
|
|
if(entry == members_by_type_.end()) return empty_string_;
|
2020-03-19 03:29:09 +00:00
|
|
|
return entry->second[size_t(e)];
|
2020-03-11 03:36:52 +00:00
|
|
|
}
|
2020-03-10 02:05:31 +00:00
|
|
|
|
2020-03-13 00:56:02 +00:00
|
|
|
/*!
|
|
|
|
@returns a vector naming the members of the enum with type_info @c type if it has been registered; an empty vector otherwise.
|
|
|
|
*/
|
2020-03-14 03:38:29 +00:00
|
|
|
static const std::vector<std::string> &all_values(std::type_index type) {
|
|
|
|
const auto entry = members_by_type_.find(type);
|
2020-03-13 00:56:02 +00:00
|
|
|
if(entry == members_by_type_.end()) return empty_vector_;
|
|
|
|
return entry->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns a vector naming the members of the enum @c Type type if it has been registered; an empty vector otherwise.
|
|
|
|
*/
|
|
|
|
template <typename Type> static const std::vector<std::string> &all_values() {
|
|
|
|
return all_values(typeid(Type));
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:36:52 +00:00
|
|
|
/*!
|
2020-03-12 02:06:16 +00:00
|
|
|
@returns A value of @c Type for the name @c str, or @c EnumType(std::string::npos) if
|
|
|
|
the name is not found.
|
2020-03-11 03:36:52 +00:00
|
|
|
*/
|
2020-03-13 00:56:02 +00:00
|
|
|
template <typename Type> static Type from_string(const std::string &str) {
|
|
|
|
return Type(from_string(typeid(Type), str));
|
2020-03-11 03:36:52 +00:00
|
|
|
}
|
2020-03-10 02:05:31 +00:00
|
|
|
|
2020-03-12 02:06:16 +00:00
|
|
|
/*!
|
2020-03-19 02:31:32 +00:00
|
|
|
@returns A value for the name @c str in the enum with type_info @c type , or @c -1 if
|
2020-03-12 02:06:16 +00:00
|
|
|
the name is not found.
|
|
|
|
*/
|
2020-03-19 02:31:32 +00:00
|
|
|
static int from_string(std::type_index type, const std::string &str) {
|
2020-03-14 03:38:29 +00:00
|
|
|
const auto entry = members_by_type_.find(type);
|
2020-03-19 02:31:32 +00:00
|
|
|
if(entry == members_by_type_.end()) return -1;
|
2020-03-11 03:36:52 +00:00
|
|
|
const auto iterator = std::find(entry->second.begin(), entry->second.end(), str);
|
2020-03-19 02:31:32 +00:00
|
|
|
if(iterator == entry->second.end()) return -1;
|
|
|
|
return int(iterator - entry->second.begin());
|
2020-03-11 03:36:52 +00:00
|
|
|
}
|
2020-03-10 02:05:31 +00:00
|
|
|
|
2020-03-11 03:36:52 +00:00
|
|
|
private:
|
2020-03-14 03:38:29 +00:00
|
|
|
static inline std::unordered_map<std::type_index, std::vector<std::string>> members_by_type_;
|
|
|
|
static inline std::unordered_map<std::type_index, std::string> names_by_type_;
|
2020-03-11 03:36:52 +00:00
|
|
|
static inline const std::string empty_string_;
|
2020-03-13 00:56:02 +00:00
|
|
|
static inline const std::vector<std::string> empty_vector_;
|
2020-03-11 03:36:52 +00:00
|
|
|
};
|
2020-03-10 02:05:31 +00:00
|
|
|
|
2020-03-11 03:36:52 +00:00
|
|
|
}
|