xgboost
enum_class_param.h
Go to the documentation of this file.
1 
8 #ifndef XGBOOST_COMMON_ENUM_CLASS_PARAM_H_
9 #define XGBOOST_COMMON_ENUM_CLASS_PARAM_H_
10 
11 #include <dmlc/parameter.h>
12 #include <string>
13 #include <type_traits>
14 
49 #define DECLARE_FIELD_ENUM_CLASS(EnumClass) \
50 namespace dmlc { \
51 namespace parameter { \
52 template <> \
53 class FieldEntry<EnumClass> : public FieldEntry<int> { \
54  public: \
55  FieldEntry<EnumClass>() { \
56  static_assert( \
57  std::is_same<int, typename std::underlying_type<EnumClass>::type>::value, \
58  "enum class must be backed by int"); \
59  is_enum_ = true; \
60  } \
61  using Super = FieldEntry<int>; \
62  void Set(void *head, const std::string &value) const override { \
63  Super::Set(head, value); \
64  } \
65  inline FieldEntry<EnumClass>& add_enum(const std::string &key, EnumClass value) { \
66  Super::add_enum(key, static_cast<int>(value)); \
67  return *this; \
68  } \
69  inline FieldEntry<EnumClass>& set_default(const EnumClass& default_value) { \
70  default_value_ = static_cast<int>(default_value); \
71  has_default_ = true; \
72  return *this; \
73  } \
74  inline void Init(const std::string &key, void *head, EnumClass& ref) { /* NOLINT */ \
75  Super::Init(key, head, *reinterpret_cast<int*>(&ref)); \
76  } \
77 }; \
78 } /* namespace parameter */ \
79 } /* namespace dmlc */
80 
81 #endif // XGBOOST_COMMON_ENUM_CLASS_PARAM_H_