ARTS 2.5.9 (git: 825fa5f2)
enums.h File Reference
#include <algorithm>
#include <array>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include "debug.h"
#include "nonstd.h"

Go to the source code of this file.

Macros

#define ENUMCLASS(ENUMTYPE, TYPE, ...)
 

Functions

template<typename EnumType >
constexpr bool good_enum (EnumType x) noexcept
 Checks if the enum number is good. More...
 
template<typename EnumType >
constexpr std::array< std::string_view, size_t(EnumType::FINAL)> enum_strarray (const std::string_view strchars) noexcept
 Internal string view array generator. More...
 
template<typename EnumType >
constexpr std::array< std::string_view, 0 > enum_strarray () noexcept
 
template<typename EnumType >
constexpr std::array< EnumType, size_t(EnumType::FINAL)> enum_typarray () noexcept
 A list of all enum types by index-conversion. More...
 
template<typename EnumType , typename... Messages>
constexpr void check_enum_error (EnumType type, Messages... args)
 Checks if the enum class type is good and otherwise throws an error message composed by variadic input. More...
 

Macro Definition Documentation

◆ ENUMCLASS

#define ENUMCLASS (   ENUMTYPE,
  TYPE,
  ... 
)

Enum style

Generates a "enum class ENUMTYPE : TYPE" with all the following arguments and terminated by FINAL

Additionally, will fill a local namespace "enumstrs" with a std::array<std::string_view, TYPE(ENUMTYPE::FINAL)> with all of the names in the ENUMTYPE enum class (bar FINAL)

Additionally, will fill a local namespace "enumtyps" with a std::array<ENUMTYPE, TYPE(ENUMTYPE::FINAL)> with all of the types in the ENUMTYPE enum class (bar FINAL)

Additionally, will generate a constexpr "toString" function that takes a ENUMTYPE object and returns either its partial name or "BAD ENUMTYPE" as a string_view

Additionally, will generate a constexpr "toENUMTYPE" function that takes a std::string_view and returns a corresponding ENUMTYPE object or ENUMTYPE::FINAL if the object is bad

Additionally, will generate intuitive std::istream& and std::ostream& operators

Use the "good_enum(ENUMTYPE)" template function to check if the enum class object is any good

Use check_enum_error(ENUMTYPE, message...) to throw an error composed by the variadic message if the enum value is bad

// Example:
ENUMCLASS(Test, char, Value)

// Generates (effectively):
enum class Test : char {Value, FINAL};
namespace enumstrs {
   constexpr std::array<std::string_view, 1> TestNames={"Value"};
}
namespace enumtyps {
   constexpr std::array<Test, 1> TestTypes={Test::Value};
}
constexpr std::string_view toString(Test x) noexcept;
constexpr Test toTest(const std::string_view& x) noexcept;
inline std::ostream &operator<<(std::ostream &os, const Test x);
inline std::istream &operator>>(std::istream &is, Test &x);  // throws if not
good_enum(x) at end 

Definition at line 142 of file enums.h.

Function Documentation

◆ check_enum_error()

template<typename EnumType , typename... Messages>
constexpr void check_enum_error ( EnumType  type,
Messages...  args 
)
constexpr

Checks if the enum class type is good and otherwise throws an error message composed by variadic input.

Parameters
[in]typeThe enum class type variable
[in]...argsA list of errors to print if type is bad

Definition at line 90 of file enums.h.

References ARTS_USER_ERROR_IF, and good_enum().

Referenced by abs_linesLineShapeType(), LineShape::from_linefunctiondata(), and time_stepper_selection().

◆ enum_strarray() [1/2]

template<typename EnumType >
constexpr std::array< std::string_view, 0 > enum_strarray ( )
constexprnoexcept

Definition at line 64 of file enums.h.

◆ enum_strarray() [2/2]

template<typename EnumType >
constexpr std::array< std::string_view, size_t(EnumType::FINAL)> enum_strarray ( const std::string_view  strchars)
constexprnoexcept

Internal string view array generator.

Automagically seperates a list by the commas in it, removing spaces before creating the named variable

Parameters
[in]strcharsA list of characters
Returns
An array of views into the original strchars

Definition at line 35 of file enums.h.

References nonstd::isspace(), and N1.

◆ enum_typarray()

template<typename EnumType >
constexpr std::array< EnumType, size_t(EnumType::FINAL)> enum_typarray ( )
constexprnoexcept

A list of all enum types by index-conversion.

Note that this assumes the enums are sorted from 0-FINAL

Returns
A list of all enum types

Definition at line 76 of file enums.h.

◆ good_enum()

template<typename EnumType >
constexpr bool good_enum ( EnumType  x)
constexprnoexcept