xgboost
parameter.h
Go to the documentation of this file.
1 
8 #ifndef XGBOOST_PARAMETER_H_
9 #define XGBOOST_PARAMETER_H_
10 
11 #include <dmlc/parameter.h>
12 #include <xgboost/base.h>
13 #include <string>
14 #include <type_traits>
15 
50 #define DECLARE_FIELD_ENUM_CLASS(EnumClass) \
51 namespace dmlc { \
52 namespace parameter { \
53 template <> \
54 class FieldEntry<EnumClass> : public FieldEntry<int> { \
55  public: \
56  FieldEntry() { \
57  static_assert( \
58  std::is_same<int, typename std::underlying_type<EnumClass>::type>::value, \
59  "enum class must be backed by int"); \
60  is_enum_ = true; \
61  } \
62  using Super = FieldEntry<int>; \
63  void Set(void *head, const std::string &value) const override { \
64  Super::Set(head, value); \
65  } \
66  inline FieldEntry<EnumClass>& add_enum(const std::string &key, EnumClass value) { \
67  Super::add_enum(key, static_cast<int>(value)); \
68  return *this; \
69  } \
70  inline FieldEntry<EnumClass>& set_default(const EnumClass& default_value) { \
71  default_value_ = static_cast<int>(default_value); \
72  has_default_ = true; \
73  return *this; \
74  } \
75  inline void Init(const std::string &key, void *head, EnumClass& ref) { /* NOLINT */ \
76  Super::Init(key, head, *reinterpret_cast<int*>(&ref)); \
77  } \
78 }; \
79 } /* namespace parameter */ \
80 } /* namespace dmlc */
81 
82 namespace xgboost {
83 template <typename Type>
84 struct XGBoostParameter : public dmlc::Parameter<Type> {
85  protected:
86  bool initialised_ {false};
87 
88  public:
89  template <typename Container>
90  Args UpdateAllowUnknown(Container const& kwargs) {
91  if (initialised_) {
92  return dmlc::Parameter<Type>::UpdateAllowUnknown(kwargs);
93  } else {
94  auto unknown = dmlc::Parameter<Type>::InitAllowUnknown(kwargs);
95  initialised_ = true;
96  return unknown;
97  }
98  }
99  bool GetInitialised() const { return static_cast<bool>(this->initialised_); }
100 };
101 } // namespace xgboost
102 
103 #endif // XGBOOST_PARAMETER_H_
Defines configuration macros and basic types for xgboost.
Core data structure for multi-target trees.
Definition: base.h:87
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:310
Definition: parameter.h:84
bool initialised_
Definition: parameter.h:86
bool GetInitialised() const
Definition: parameter.h:99
Args UpdateAllowUnknown(Container const &kwargs)
Definition: parameter.h:90