-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AnyParameter.h
149 lines (128 loc) · 3.08 KB
/
AnyParameter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef __ANYPARAMETER_H__
#define __ANYPARAMETER_H__
#include <shogun/lib/any.h>
#include <shogun/io/CerealVisitor.h>
#include <memory>
#include <string>
namespace shogun
{
/** model selection availability */
enum EModelSelectionAvailability
{
MS_NOT_AVAILABLE = 0,
MS_AVAILABLE = 1,
};
/** gradient availability */
enum EGradientAvailability
{
GRADIENT_NOT_AVAILABLE = 0,
GRADIENT_AVAILABLE = 1
};
class AnyParameterProperties
{
public:
AnyParameterProperties()
: m_description(), m_model_selection(MS_NOT_AVAILABLE),
m_gradient(GRADIENT_NOT_AVAILABLE)
{
}
AnyParameterProperties(
std::string description,
EModelSelectionAvailability model_selection = MS_NOT_AVAILABLE,
EGradientAvailability gradient = GRADIENT_NOT_AVAILABLE)
: m_description(description), m_model_selection(model_selection),
m_gradient(gradient)
{
}
AnyParameterProperties(const AnyParameterProperties& other)
: m_description(other.m_description),
m_model_selection(other.m_model_selection),
m_gradient(other.m_gradient)
{
}
std::string get_description() const
{
return m_description;
}
EModelSelectionAvailability get_model_selection() const
{
return m_model_selection;
}
EGradientAvailability get_gradient() const
{
return m_gradient;
}
/** serialize the object using cereal
*
* @param ar Archive type
*/
template<class Archive>
void serialize(Archive& ar)
{
ar(m_description, m_model_selection, m_gradient);
}
private:
std::string m_description;
EModelSelectionAvailability m_model_selection;
EGradientAvailability m_gradient;
};
class AnyParameter
{
public:
AnyParameter() : m_value(), m_properties()
{
}
explicit AnyParameter(const Any& value) : m_value(value), m_properties()
{
}
AnyParameter(const Any& value, AnyParameterProperties properties)
: m_value(value), m_properties(properties)
{
}
AnyParameter(const AnyParameter& other)
: m_value(other.m_value), m_properties(other.m_properties)
{
}
Any get_value() const
{
return m_value;
}
void set_value(const Any& value)
{
m_value = value;
}
AnyParameterProperties get_properties() const
{
return m_properties;
}
/** Equality operator which compares value but not properties.
* @return true if value of other parameter equals own */
inline bool operator==(const AnyParameter& other) const
{
return m_value == other.get_value();
}
/** @see operator==() */
inline bool operator!=(const AnyParameter& other) const
{
return !(*this == other);
}
template <class Archive>
void cereal_save(Archive& ar) const
{
std::unique_ptr<CerealWriterVisitor<Archive>> visitor(new CerealWriterVisitor<Archive>(ar));
m_value.visit(visitor.get());
ar(m_properties);
}
template <class Archive>
void cereal_load(Archive& ar)
{
std::unique_ptr<CerealReaderVisitor<Archive>> visitor(new CerealReaderVisitor<Archive>(ar));
m_value.visit(visitor.get());
ar(m_properties);
}
private:
Any m_value;
AnyParameterProperties m_properties;
};
}
#endif