forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_enum.h
37 lines (32 loc) · 1.66 KB
/
string_enum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef NMOS_STRING_ENUM_H
#define NMOS_STRING_ENUM_H
#include "cpprest/details/basic_types.h"
namespace nmos
{
// Many of the JSON fields in the NMOS specifications are strings with an enumerated set of values.
// Sometimes these enumerations are extensible (i.e. not a closed set), such as those for media types.
// string_enum is a base class using CRTP to implement type safe enums with simple conversion to string.
// See nmos/type.h for a usage example.
template <class Derived>
struct string_enum
{
utility::string_t name;
// could add explicit string conversion operator?
// totally_ordered rather than just equality_comparable only to allow use of type as a key
// in associative containers; an alternative would be adding a std::hash override so that
// unordered associative containers could be used instead?
friend bool operator==(const Derived& lhs, const Derived& rhs) { return lhs.name == rhs.name; }
friend bool operator< (const Derived& lhs, const Derived& rhs) { return lhs.name < rhs.name; }
friend bool operator> (const Derived& lhs, const Derived& rhs) { return rhs < lhs; }
friend bool operator!=(const Derived& lhs, const Derived& rhs) { return !(lhs == rhs); }
friend bool operator<=(const Derived& lhs, const Derived& rhs) { return !(rhs < lhs); }
friend bool operator>=(const Derived& lhs, const Derived& rhs) { return !(lhs < rhs); }
};
}
#define DEFINE_STRING_ENUM(Type) \
struct Type : public nmos::string_enum<Type> \
{ \
Type() {} \
explicit Type(utility::string_t name) : string_enum{ std::move(name) } {} \
};
#endif