OOFEM 3.0
Loading...
Searching...
No Matches
enum-impl.h
Go to the documentation of this file.
1/*
2This header is to be included in-place to define an enum.
3
4The following macros must be defined prior to inclusion (will be #undef'd)
5
6- ENUM_TYPE: name of the enum
7- ENUM_DEF: sequence of ENUM_ITEM(name) or ENUM_ITEM_WITH_VALUE(name,value)
8
9The following may be defined prior to includion (will be #undef'd)
10
11- ENUM_PREFIX: if defined, name to value conversion will allow dropping prefix from the name (e.g. saying "Total" instead of "VM_Total", the prefix being "VM_")
12- ENUM_CLASS: if defined, define the enum as "enum class"; otherwise, a plain enum
13
14*/
15
16#ifndef enum_h
17 #error enum.h MUST be included prior to including this header
18#endif
19
20#if !defined(ENUM_TYPE) || !defined(ENUM_DEF)
21 #error both ENUM_TYPE and ENUM_DEF macros must be defined before including this header
22#endif
23
24
25// leaked from enumitem.hpp perhaps, so undefine to avoid warnings and then re-define again
26#ifdef ENUM_ITEM
27 #undef ENUM_ITEM
28 #undef ENUM_ITEM_WITH_VALUE
29 #define _MUST_RESTORE_ENUM_ITEM
30#endif
31
32// helper macros
33#define _str(s) #s
34#define _xstr(s) _str(s)
35
36// define the enum itself
37#define ENUM_ITEM(i) i,
38#define ENUM_ITEM_WITH_VALUE(i,v) i=v,
39enum
40 #ifdef ENUM_CLASS
41 class
42 #endif
44#undef ENUM_ITEM_WITH_VALUE
45#undef ENUM_ITEM
46
47#ifndef ENUM_PREFIX
48 #define ENUM_PREFIX ""
49#endif
50
51/* minimal class holding metadata about the enum: values, names, opt_prefix */
52template<> struct EnumData<ENUM_TYPE>{
53 typedef ENUM_TYPE Enum;
54 static constexpr const char* enum_name = _xstr(ENUM_TYPE);
55 static constexpr const char opt_prefix[]=ENUM_PREFIX;
56 static constexpr size_t opt_prefix_len=sizeof(opt_prefix)-1;
57
58 struct EnumItem { Enum value; const char* name; };
59 #define ENUM_ITEM_WITH_VALUE(i,v) {ENUM_TYPE::i,#i},
60 #define ENUM_ITEM(i) {ENUM_TYPE::i,#i},
61 static constexpr std::initializer_list<EnumItem> value_to_name={ ENUM_DEF };
62 #undef ENUM_ITEM_WITH_VALUE
63 #undef ENUM_ITEM
64};
65
66// conversion function for compatibility
67#define __TO_STRING(T) inline const char* __ ## T ## ToString(ENUM_TYPE value){ return EnumTraits<ENUM_TYPE>::name(value).value(); }
68#define _TO_STRING(T) __TO_STRING(T)
70#undef _TO_STRING
71#undef __TO_STRING
72
73// don't leak macros out
74#undef ENUM_TYPE
75#undef ENUM_DEF
76#undef ENUM_NAME
77#undef ENUM_PREFIX
78#ifdef ENUM_CLASS
79 #undef ENUM_CLASS
80#endif
81#undef _str
82#undef _xstr
83
84// restore
85#ifdef _MUST_RESTORE_ENUM_ITEM
86 #define ENUM_ITEM(element) element,
87 #define ENUM_ITEM_WITH_VALUE(element, val) element = val,
88 #undef _MUST_RESTORE_ENUM_ITEM
89#endif
ENUM_TYPE
Definition enum-impl.h:43
@ ENUM_DEF
Definition enum-impl.h:43
#define _TO_STRING(T)
Definition enum-impl.h:68
#define ENUM_PREFIX
Definition enum-impl.h:48
#define _xstr(s)
Definition enum-impl.h:34
#define ENUM_DEF
#define ENUM_TYPE
static constexpr std::initializer_list< EnumItem > value_to_name
Definition enum-impl.h:61
static constexpr const char opt_prefix[]
Definition enum-impl.h:55
static constexpr const char * enum_name
Definition enum-impl.h:54
static constexpr size_t opt_prefix_len
Definition enum-impl.h:56

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011