2015-04-20 17:51:00 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-04-20 17:51:00 +10:00
|
|
|
*
|
2017-06-08 15:48:15 +10:00
|
|
|
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
|
2015-04-20 17:51:00 +10:00
|
|
|
*/
|
|
|
|
|
2019-04-16 10:17:53 +10:00
|
|
|
#pragma once
|
2015-04-20 17:51:00 +10:00
|
|
|
|
2017-05-16 17:06:36 +10:00
|
|
|
#include <array>
|
2020-09-24 15:41:58 +10:00
|
|
|
#include <cruft/util/preprocessor.hpp>
|
2017-02-09 16:48:42 +11:00
|
|
|
|
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
namespace cruft::introspection {
|
2016-03-11 13:02:52 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Lists valid values of an enumeration
|
|
|
|
///
|
|
|
|
/// E: enumeration type
|
2016-12-21 16:46:16 +11:00
|
|
|
///
|
|
|
|
/// Specialisations must provide the following constexpr:
|
|
|
|
/// value_type: typename
|
|
|
|
/// value_count: size_t
|
|
|
|
/// values: static const std::array<value_type,value_count>
|
2016-03-11 13:02:52 +11:00
|
|
|
template <
|
|
|
|
typename E
|
2020-09-24 15:41:58 +10:00
|
|
|
>
|
2016-12-21 16:46:16 +11:00
|
|
|
struct enum_traits;
|
2016-03-11 13:02:52 +11:00
|
|
|
|
2016-03-11 19:16:35 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Defines specialisations for introspection data structures for an
|
|
|
|
/// enum E, in namespace NS, with variadic values __VA_ARGS__.
|
|
|
|
///
|
|
|
|
/// Expects to be caleld from outside all namespaces.
|
2016-12-21 16:46:16 +11:00
|
|
|
///
|
|
|
|
/// XXX: If we define the constexpr fields in a template specialised class
|
|
|
|
/// clang has trouble instantiating them (with std=c++1z) resulting in
|
|
|
|
/// undefined symbols at link time. By using a simple struct and inheriting
|
|
|
|
/// from it we can avoid this problem. Revist this solution at clang-4.0.
|
|
|
|
|
|
|
|
#define INTROSPECTION_ENUM_DECL(NS,E, ...) \
|
2020-09-24 15:41:58 +10:00
|
|
|
namespace cruft::introspection { \
|
2016-12-21 16:46:16 +11:00
|
|
|
struct PASTE(__enum_traits_,E) { \
|
|
|
|
using value_type = ::NS::E; \
|
|
|
|
\
|
|
|
|
static constexpr \
|
|
|
|
size_t value_count = VA_ARGS_COUNT(__VA_ARGS__); \
|
|
|
|
\
|
|
|
|
static const \
|
|
|
|
std::array<value_type,value_count> \
|
|
|
|
values; \
|
|
|
|
\
|
|
|
|
static const \
|
|
|
|
std::array<const char*,value_count> \
|
|
|
|
names; \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
template <> \
|
|
|
|
struct enum_traits<::NS::E> : public PASTE(__enum_traits_,E) \
|
|
|
|
{ }; \
|
|
|
|
} \
|
2016-03-11 19:16:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Declares specialisations for introspection data structures for an
|
|
|
|
/// enum E, in namespace NS, with variadic values __VA_ARGS__.
|
|
|
|
///
|
2016-12-21 16:46:16 +11:00
|
|
|
/// Expects to be called from outside all namespaces.
|
2016-03-11 19:16:35 +11:00
|
|
|
#define INTROSPECTION_ENUM_IMPL(NS,E, ...) \
|
2017-05-16 17:07:14 +10:00
|
|
|
const \
|
2016-03-11 19:16:35 +11:00
|
|
|
std::array< \
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::enum_traits<::NS::E>::value_type, \
|
|
|
|
cruft::introspection::enum_traits<::NS::E>::value_count \
|
|
|
|
> PASTE(cruft::introspection::__enum_traits_,E)::values = { \
|
2016-12-21 16:46:16 +11:00
|
|
|
MAP1(NAMESPACE_LIST, ::NS::E, __VA_ARGS__) \
|
|
|
|
}; \
|
2016-03-11 19:16:35 +11:00
|
|
|
\
|
2016-12-21 16:46:16 +11:00
|
|
|
const \
|
2016-03-11 19:16:35 +11:00
|
|
|
std::array< \
|
|
|
|
const char*, \
|
2020-09-24 15:41:58 +10:00
|
|
|
cruft::introspection::enum_traits<::NS::E>::value_count \
|
|
|
|
> PASTE(cruft::introspection::__enum_traits_,E)::names = { \
|
2017-09-12 14:17:30 +10:00
|
|
|
MAP0(STRINGIZE_LIST, __VA_ARGS__) \
|
2017-06-08 15:48:15 +10:00
|
|
|
};
|
2016-03-11 19:16:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Defines an istream extraction operator for an enumeration E, within
|
|
|
|
/// namespace NS
|
|
|
|
///
|
|
|
|
/// Expects to be called from outside all namespaces.
|
|
|
|
///
|
2018-08-05 14:42:02 +10:00
|
|
|
/// The user is responsible for specialising the ::cruft::enum_traits<NS::E>
|
2016-03-11 19:16:35 +11:00
|
|
|
/// parameters which are used to drive the implementation (eg, through
|
|
|
|
/// INTROSPECTION_ENUM_DECL, and INTROSPECTION_ENUM_IMPL).
|
|
|
|
///
|
|
|
|
/// For trivial enumerations INTROSPECTION_ENUM may be easier to use.
|
|
|
|
|
2020-09-24 15:41:58 +10:00
|
|
|
#define INTROSPECTION_ENUM_ISTREAM(NS,E) \
|
|
|
|
std::istream& \
|
|
|
|
::NS::operator>> (std::istream &is, ::NS::E &e) \
|
|
|
|
{ \
|
|
|
|
using traits = ::cruft::introspection::enum_traits<::NS::E>; \
|
|
|
|
\
|
|
|
|
std::string name; \
|
|
|
|
is >> name; \
|
|
|
|
\
|
|
|
|
std::transform (std::begin (name), \
|
|
|
|
std::end (name), \
|
|
|
|
std::begin (name), \
|
|
|
|
::toupper); \
|
|
|
|
\
|
|
|
|
auto name_pos = std::find ( \
|
|
|
|
std::cbegin (traits::names), \
|
|
|
|
std::cend (traits::names), \
|
|
|
|
name \
|
|
|
|
); \
|
|
|
|
\
|
|
|
|
if (name_pos == std::cend (traits::names)) { \
|
|
|
|
is.setstate (std::istream::failbit); \
|
|
|
|
} else { \
|
|
|
|
auto d = std::distance ( \
|
|
|
|
std::begin (traits::names), \
|
|
|
|
name_pos \
|
|
|
|
); \
|
|
|
|
\
|
|
|
|
e = traits::values[d]; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return is; \
|
2016-03-11 19:16:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Defines an ostream insertion operator for an enumeration E, within
|
|
|
|
/// namespace NS.
|
|
|
|
///
|
|
|
|
/// Expects to be called from outside all namespaces.
|
|
|
|
///
|
2020-09-24 15:41:58 +10:00
|
|
|
/// The user is responsible for specialising the
|
|
|
|
/// ::cruft::introspection:;enum_traits<NS::E> parameters which are used
|
|
|
|
/// to drive the implementation (eg, through INTROSPECTION_ENUM_DECL,
|
|
|
|
/// and INTROSPECTION_ENUM_IMPL).
|
2016-03-11 19:16:35 +11:00
|
|
|
///
|
|
|
|
/// For trivial enumerations INTROSPECTION_ENUM may be easier to use.
|
2020-09-24 15:41:58 +10:00
|
|
|
#define INTROSPECTION_ENUM_OSTREAM(NS,E) \
|
|
|
|
std::ostream& \
|
|
|
|
::NS::operator<< (std::ostream &os, ::NS::E e) \
|
|
|
|
{ \
|
|
|
|
using traits = ::cruft::introspection::enum_traits<::NS::E>;\
|
|
|
|
\
|
|
|
|
auto val_pos = std::find ( \
|
|
|
|
std::cbegin (traits::values), \
|
|
|
|
std::cend (traits::values), \
|
|
|
|
e \
|
|
|
|
); \
|
|
|
|
\
|
|
|
|
if (val_pos == std::cend (traits::values)) { \
|
|
|
|
os.setstate (std::ostream::failbit); \
|
|
|
|
} else { \
|
|
|
|
auto d = std::distance ( \
|
|
|
|
std::cbegin (traits::values), \
|
|
|
|
val_pos \
|
|
|
|
); \
|
|
|
|
\
|
|
|
|
os << traits::names[d]; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return os; \
|
2016-03-11 19:16:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Defines an enum, its values, associated introspection structures, and
|
|
|
|
/// istream and ostream operators.
|
2016-12-21 16:46:16 +11:00
|
|
|
///
|
2016-03-11 19:16:35 +11:00
|
|
|
/// This must be called from outside all namespaces as
|
|
|
|
/// INTROSPECTION_ENUM_DECL and INTROSPECTION_ENUM_IMPL need to declare
|
|
|
|
/// and define structures outside the user's namespace.
|
|
|
|
///
|
|
|
|
/// The enum will be defined inside an inline namespace to simplify the
|
|
|
|
/// passing of parameters to functions which require some namespace
|
|
|
|
/// prefixing. This shouldn't have a practical effect on user code.
|
|
|
|
|
|
|
|
#define INTROSPECTION_ENUM(E, ...) \
|
|
|
|
inline namespace detail_intr_enum { \
|
|
|
|
enum E { __VA_ARGS__ }; \
|
|
|
|
std::ostream& operator<< (std::ostream&, E); \
|
|
|
|
std::istream& operator>> (std::istream&, E&); \
|
|
|
|
} \
|
|
|
|
INTROSPECTION_ENUM_DECL(detail_intr_enum,E,__VA_ARGS__) \
|
|
|
|
INTROSPECTION_ENUM_IMPL(detail_intr_enum,E,__VA_ARGS__) \
|
|
|
|
INTROSPECTION_ENUM_ISTREAM(detail_intr_enum,E) \
|
|
|
|
INTROSPECTION_ENUM_OSTREAM(detail_intr_enum,E)
|
|
|
|
|
|
|
|
|
2016-03-14 22:34:14 +11:00
|
|
|
#define INTROSPECTION_ENUM_CLASS(E, ...) \
|
|
|
|
inline namespace detail_intr_enum { \
|
|
|
|
enum class E { __VA_ARGS__ }; \
|
|
|
|
std::ostream& operator<< (std::ostream&, E); \
|
|
|
|
std::istream& operator>> (std::istream&, E&); \
|
|
|
|
} \
|
|
|
|
INTROSPECTION_ENUM_DECL(detail_intr_enum,E,__VA_ARGS__) \
|
|
|
|
INTROSPECTION_ENUM_IMPL(detail_intr_enum,E,__VA_ARGS__) \
|
|
|
|
INTROSPECTION_ENUM_ISTREAM(detail_intr_enum,E) \
|
|
|
|
INTROSPECTION_ENUM_OSTREAM(detail_intr_enum,E)
|
2020-09-24 15:41:58 +10:00
|
|
|
}
|