xgboost
Classes | Namespaces | Macros
parameter.h File Reference

macro for using C++11 enum class as DMLC parameter More...

#include <dmlc/parameter.h>
#include <xgboost/base.h>
#include <string>
#include <type_traits>
Include dependency graph for parameter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  xgboost::XGBoostParameter< Type >
 

Namespaces

 xgboost
 namespace of xgboost
 

Macros

#define DECLARE_FIELD_ENUM_CLASS(EnumClass)
 Specialization of FieldEntry for enum class (backed by int) More...
 

Detailed Description

macro for using C++11 enum class as DMLC parameter

Copyright 2018 by Contributors

Author
Hyunsu Philip Cho

Macro Definition Documentation

◆ DECLARE_FIELD_ENUM_CLASS

#define DECLARE_FIELD_ENUM_CLASS (   EnumClass)
Value:
namespace dmlc { \
namespace parameter { \
template <> \
class FieldEntry<EnumClass> : public FieldEntry<int> { \
public: \
FieldEntry<EnumClass>() { \
static_assert( \
std::is_same<int, typename std::underlying_type<EnumClass>::type>::value, \
"enum class must be backed by int"); \
is_enum_ = true; \
} \
using Super = FieldEntry<int>; \
void Set(void *head, const std::string &value) const override { \
Super::Set(head, value); \
} \
inline FieldEntry<EnumClass>& add_enum(const std::string &key, EnumClass value) { \
Super::add_enum(key, static_cast<int>(value)); \
return *this; \
} \
inline FieldEntry<EnumClass>& set_default(const EnumClass& default_value) { \
default_value_ = static_cast<int>(default_value); \
has_default_ = true; \
return *this; \
} \
inline void Init(const std::string &key, void *head, EnumClass& ref) { /* NOLINT */ \
Super::Init(key, head, *reinterpret_cast<int*>(&ref)); \
} \
}; \
} /* namespace parameter */ \
} /* namespace dmlc */
Definition: data.h:606

Specialization of FieldEntry for enum class (backed by int)

Use this macro to use C++11 enum class as DMLC parameters

Usage:

// enum class must inherit from int type
enum class Foo : int {
kBar = 0, kFrog = 1, kCat = 2, kDog = 3
};
// This line is needed to prevent compilation error
// Now define DMLC parameter as usual;
// enum classes can now be members.
struct MyParam : dmlc::Parameter<MyParam> {
Foo foo;
DMLC_DECLARE_PARAMETER(MyParam) {
DMLC_DECLARE_FIELD(foo)
.set_default(Foo::kBar)
.add_enum("bar", Foo::kBar)
.add_enum("frog", Foo::kFrog)
.add_enum("cat", Foo::kCat)
.add_enum("dog", Foo::kDog);
}
};
DMLC_REGISTER_PARAMETER(MyParam);