New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use visitors in put and get #5037
Conversation
@@ -298,30 +310,20 @@ class SGObject: public std::enable_shared_from_this<SGObject> | |||
*/ | |||
template <typename T, | |||
typename std::enable_if_t<!is_string<T>::value>* = nullptr> | |||
void put(const Tag<T>& _tag, const T& value) noexcept(false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed these as they don't do anything for us. This is just useful if the template type determines if it is noexcept
{ | ||
const Any value = get_parameter(_tag).get_value(); | ||
try | ||
if constexpr (is_string<T>::value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplified a bunch of overloads with if constexpr
} | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vigsterkr I adapted from your PR. Can then put all the visitor registrations in this function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -1065,15 +1087,70 @@ class SGObject: public std::enable_shared_from_this<SGObject> | |||
* @param _tag name information of parameter | |||
* @param value new value of parameter | |||
*/ | |||
void update_parameter(const BaseTag& _tag, const Any& value, bool do_checks = true); | |||
template <typename T> | |||
void update_parameter(const BaseTag& _tag, const T& value, bool do_checks = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made this template dependent so that we can have all these parameter handling branches in a single location
} | ||
if constexpr (std::is_same_v<T, Any>) | ||
{ | ||
param.set_value(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is needed right now for auto init, but I will refactor it in another PR and can then remove this
@@ -1229,7 +1225,7 @@ namespace shogun | |||
static void register_caster(std::function<To(From)> caster); | |||
|
|||
template <class Type, class State> | |||
static void register_visitor(std::function<void(Type, State*)> visitor); | |||
static void register_visitor(std::function<void(Type*, State*)> visitor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is more convenient to pass around pointers rather than values
src/shogun/base/SGObject.h
Outdated
@@ -1160,6 +1237,7 @@ class SGObject: public std::enable_shared_from_this<SGObject> | |||
{ | |||
auto param = this->get_parameter(BaseTag(name)); | |||
auto cloned = any_cast<T>(param.get_value()); | |||
param.get_properties().remove_property(ParameterProperties::FUNCTIONAL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit awkward, but the observers pass on properties as is, meaning the value of a function call still has a FUNCTIONAL property, eg cluster centres in KMeans unit test
ParameterProperties::READONLY | ParameterProperties::FUNCTIONAL); | ||
std::function<T()> bind_method = [this, method](){ | ||
return (static_cast<const S*>(this) ->* method)(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be more efficient than std::bind
READONLY = 1u << 11, | ||
|
||
// an executable function | ||
RUNFUNCTION = 1u << 12, | ||
// a class member function with side effects |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to allow this even? I'd prefer if we didnt ... landslide risk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used by some classes, like RandomMixin to set the random seed (set_random_seed
is not const). The safety net we have here is that you can only call these functions using run
, rather than get
.
03b211c
to
6b643aa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⛵
@@ -62,6 +62,7 @@ jobs: | |||
- job: swig | |||
displayName: Linux SWIG Interface | |||
dependsOn: libshogun | |||
timeoutInMinutes: 90 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as well? :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be unrelated. It's just with the python notebook job it can go just above 60 minutes, so just set it to 90
} | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This replaces the
make_any
input
with a visitor pattern (saves creating a new Any and replacing the old with the new one; instead we change the stored value directly with the visitor). For symmetry I also do the same withget
, but I don't think it makes any difference to usingany_cast
. Also had to change some of the getter signatures to have access to mutable and immutable references.This will be really useful to register things like
std::optional
andstd::variant
without changing the functionality inAny
.