OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
define_enum_class.h File Reference

Macros/templates for defining enum classes and related utilities. Place macro calls in the heavyai namespace so that functions like to_string() can be found by the compiler via ADL. More...

#include <boost/preprocessor.hpp>
#include <algorithm>
#include <array>
#include <optional>
#include <ostream>
#include <string_view>
+ Include dependency graph for define_enum_class.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  heavyai::StringEnum< Enum >
 
struct  heavyai::IsAny< Values >
 

Namespaces

 heavyai
 

Macros

#define HEAVYAI_DEFINE_ENUM_CLASS(enum_class,...)
 
#define HEAVYAI_DEFINE_ENUM_CLASS_WITH_DESCRIPTIONS(enum_class,...)
 
#define HEAVYAI_QUOTE(r, data, i, elem)   BOOST_PP_COMMA_IF(i) BOOST_PP_STRINGIZE(elem)
 
#define HEAVYAI_QUOTE_EACH(...)   BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_QUOTE, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
 
#define HEAVYAI_PLUCK_ONE(r, j, i, pair)   BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, j, pair)
 
#define HEAVYAI_PLUCK(j,...)   BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_PLUCK_ONE, j, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
 

Functions

template<typename Enum , size_t... I>
constexpr auto heavyai::enum_to_array (std::index_sequence< I...>)
 
template<typename T , size_t N>
constexpr void heavyai::insertion_sort (std::array< T, N > &arr)
 
template<typename Enum >
constexpr std::array
< StringEnum< Enum >, size_t(Enum::N_)> 
heavyai::sort_by_name ()
 
template<typename Enum >
std::optional< Enum > heavyai::to_enum (std::string_view const name)
 

Detailed Description

Macros/templates for defining enum classes and related utilities. Place macro calls in the heavyai namespace so that functions like to_string() can be found by the compiler via ADL.

Example: HEAVYAI_DEFINE_ENUM_CLASS(Color, Red, Green, Blue) Defines:

  1. enum class Color { Red, Green, Blue };
  2. constexpr char const* to_string(Color const);
  3. inline std::ostream &operator<<(std::ostream&, Color const);

The macro HEAVYAI_DEFINE_ENUM_CLASS_WITH_DESCRIPTIONS() additionally defines

  1. constexpr char const* to_description(Color const);

template <typename enum>=""> constexpr std::optional<Enum> to_enum(std::string_view const name); returns the Enum if found by its string representation in O(log(N)) time.

Definition in file define_enum_class.h.

Macro Definition Documentation

#define HEAVYAI_DEFINE_ENUM_CLASS (   enum_class,
  ... 
)
Value:
enum class enum_class { __VA_ARGS__, N_ }; \
\
constexpr char const* to_string(enum_class const e) { \
constexpr char const* strings[]{HEAVYAI_QUOTE_EACH(__VA_ARGS__)}; \
constexpr size_t nstrings = sizeof(strings) / sizeof(*strings); \
static_assert(nstrings == size_t(enum_class::N_)); \
return strings[size_t(e)]; \
} \
\
inline std::ostream& operator<<(std::ostream& os, enum_class const e) { \
return os << to_string(e); \
}
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:57
std::string to_string(char const *&&v)
#define HEAVYAI_QUOTE_EACH(...)

Definition at line 48 of file define_enum_class.h.

#define HEAVYAI_DEFINE_ENUM_CLASS_WITH_DESCRIPTIONS (   enum_class,
  ... 
)
Value:
HEAVYAI_DEFINE_ENUM_CLASS(enum_class, HEAVYAI_PLUCK(0, __VA_ARGS__)) \
\
constexpr char const* to_description(enum_class const e) { \
constexpr char const* strings[]{HEAVYAI_PLUCK(1, __VA_ARGS__)}; \
constexpr size_t nstrings = sizeof(strings) / sizeof(*strings); \
static_assert(nstrings == size_t(enum_class::N_)); \
return strings[size_t(e)]; \
}
#define HEAVYAI_PLUCK(j,...)
#define HEAVYAI_DEFINE_ENUM_CLASS(enum_class,...)

Definition at line 62 of file define_enum_class.h.

#define HEAVYAI_PLUCK (   j,
  ... 
)    BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_PLUCK_ONE, j, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))

Definition at line 78 of file define_enum_class.h.

#define HEAVYAI_PLUCK_ONE (   r,
  j,
  i,
  pair 
)    BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, j, pair)

Definition at line 76 of file define_enum_class.h.

#define HEAVYAI_QUOTE (   r,
  data,
  i,
  elem 
)    BOOST_PP_COMMA_IF(i) BOOST_PP_STRINGIZE(elem)

Definition at line 73 of file define_enum_class.h.

#define HEAVYAI_QUOTE_EACH (   ...)    BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_QUOTE, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))

Definition at line 74 of file define_enum_class.h.