Debian Codegraph @

src/include/xgboost/parameter.h

raw symbols SCIP
1 /*!
2 * Copyright 2018 by Contributors
3 * \file parameter.h
4 * \brief macro for using C++11 enum class as DMLC parameter
5 * \author Hyunsu Philip Cho
6 */
7
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
16 /*!
17 * \brief Specialization of FieldEntry for enum class (backed by int)
18 *
19 * Use this macro to use C++11 enum class as DMLC parameters
20 *
21 * Usage:
22 *
23 * \code{.cpp}
24 *
25 * // enum class must inherit from int type
26 * enum class Foo : int {
27 * kBar = 0, kFrog = 1, kCat = 2, kDog = 3
28 * };
29 *
30 * // This line is needed to prevent compilation error
31 * DECLARE_FIELD_ENUM_CLASS(Foo);
32 *
33 * // Now define DMLC parameter as usual;
34 * // enum classes can now be members.
35 * struct MyParam : dmlc::Parameter<MyParam> {
36 * Foo foo;
37 * DMLC_DECLARE_PARAMETER(MyParam) {
38 * DMLC_DECLARE_FIELD(foo)
39 * .set_default(Foo::kBar)
40 * .add_enum("bar", Foo::kBar)
41 * .add_enum("frog", Foo::kFrog)
42 * .add_enum("cat", Foo::kCat)
43 * .add_enum("dog", Foo::kDog);
44 * }
45 * };
46 *
47 * DMLC_REGISTER_PARAMETER(MyParam);
48 * \endcode
49 */
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_v<int, typename std::underlying_type_t<EnumClass>>, \
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_