Skip to content
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

Merged
merged 4 commits into from May 22, 2020
Merged

Conversation

gf712
Copy link
Member

@gf712 gf712 commented May 18, 2020

This replaces the make_any in put 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 with get, but I don't think it makes any difference to using any_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 and std::variant without changing the functionality in Any.

@@ -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)
Copy link
Member Author

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)
Copy link
Member Author

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

}
);
}
}
Copy link
Member Author

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

Copy link
Member

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)
Copy link
Member Author

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);
Copy link
Member Author

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);
Copy link
Member Author

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

@@ -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);
Copy link
Member Author

@gf712 gf712 May 19, 2020

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)();
};
Copy link
Member Author

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
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

@vigsterkr vigsterkr left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as well? :D

Copy link
Member Author

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

}
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@gf712 gf712 merged commit 93a649b into shogun-toolbox:develop May 22, 2020
@gf712 gf712 deleted the sg_put_get_visitor branch June 2, 2020 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants