AmpGen
2.1
Toggle main menu visibility
Loading...
Searching...
No Matches
enum.h
Go to the documentation of this file.
1
#ifndef AMPGEN_ENUM_H
2
#define AMPGEN_ENUM_H 1
3
#include "
AmpGen/MsgService.h
"
4
#include "
AmpGen/Utilities.h
"
5
#include <string.h>
6
7
#define declare_enum(name, ...) \
8
enum class name { __VA_ARGS__, Invalid }; \
9
template <> name parse(const std::string &word); \
10
template <> std::string to_string(const name &enumItem); \
11
std::ostream &operator<<(std::ostream &os, const name &np)
12
13
#define complete_enum(name, ...) \
14
template <> name parse(const std::string &word) { \
15
constexpr auto args = #__VA_ARGS__; \
16
return AmpGen::detail::parse<name>(word, args); \
17
} \
18
template <> std::string to_string(const name &enumItem) { \
19
constexpr auto args = #__VA_ARGS__; \
20
return AmpGen::detail::to_string<name>(enumItem, args); \
21
} \
22
template <> name lexical_cast(const std::string &word, bool &
/*status*/
) { return parse<name>(word); } \
23
std::ostream &operator<<(std::ostream &os, const name &np) { return os << to_string<name>(np); }
24
25
#define make_enum(name, ...) \
26
enum class name { __VA_ARGS__, Invalid }; \
27
template <> inline name parse(const std::string &word) { \
28
constexpr auto args = #__VA_ARGS__; \
29
return AmpGen::detail::parse<name>(word, args); \
30
} \
31
template <> inline std::string to_string(const name &enumItem) { \
32
constexpr auto args = #__VA_ARGS__; \
33
return AmpGen::detail::to_string<name>(enumItem, args); \
34
} \
35
template <> inline name lexical_cast(const std::string &word, bool &
/*status*/
) { return parse<name>(word); } \
36
inline std::ostream &operator<<(std::ostream &os, const name &np) { return os << to_string<name>(np); }
37
38
namespace
AmpGen
{
39
template
<
class
T> T
parse
(
const
std::string &word) {
return
T(); }
40
template
<
class
T> std::string
to_string
(
const
T &enumItem) {
return
""
; }
41
42
namespace
detail {
43
template
<
class
T> T
parse
(
const
std::string &word,
const
char
*args) {
44
char
*p;
45
auto
number = strtoul(word.c_str(), &p, 10);
46
if
(*p == 0)
return
T(number);
47
unsigned
counter = 0;
48
unsigned
begin = 0;
49
unsigned
end = 0;
50
auto
compare = [](
const
char
*word,
const
char
*otherWord,
const
unsigned
&nChar) {
51
if
(strlen(word) != nChar)
return
false
;
52
for
(
size_t
x = 0; x != nChar; ++x)
53
if
(word[x] != otherWord[x])
return
false
;
54
return
true
;
55
};
56
bool
found =
false
;
57
while
(args[begin] !=
'\0'
) {
58
while
(args[begin] ==
' '
) begin++;
59
for
(end = begin; args[end] !=
'\0'
; end++)
60
if
(args[end] ==
','
)
break
;
61
if
(compare(word.c_str(), args + begin, end - begin)) {
62
found =
true
;
63
break
;
64
}
65
begin = end + 1;
66
counter++;
67
if
(args[end] ==
'\0'
)
break
;
68
}
69
if
(!found)
return
T::Invalid;
70
return
T(counter);
71
}
72
template
<
class
T> std::string
to_string
(
const
T &enumItem,
const
char
*args) {
73
unsigned
counter = 0;
74
unsigned
sBegin = 0;
75
unsigned
sLength = 0;
76
for
(; args[sBegin] !=
'\0'
&& counter != unsigned(enumItem); sBegin++) {
77
if
(args[sBegin] ==
','
) counter++;
78
}
79
while
(args[sBegin] ==
' '
) sBegin++;
80
for
(; args[sLength + sBegin] !=
'\0'
; ++sLength)
81
if
(args[sBegin + sLength] ==
','
)
break
;
82
return
std::string(args).substr(sBegin, sLength);
83
}
84
}
85
}
86
#endif
MsgService.h
Utilities.h
AmpGen::detail::parse
T parse(const std::string &word, const char *args)
Definition
enum.h:43
AmpGen::detail::to_string
std::string to_string(const T &enumItem, const char *args)
Definition
enum.h:72
AmpGen
Definition
AddCPConjugate.h:2
AmpGen::to_string
std::string to_string(const T &enumItem)
Definition
enum.h:40
AmpGen::parse
T parse(const std::string &word)
Definition
enum.h:39
AmpGen
enum.h
Generated on
for AmpGen by
1.17.0