Skip to content

Commit

Permalink
Use separate methods for creating and updating parameters (#4062)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jan 1, 2018
1 parent 0bd36ee commit 99e083f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/shogun/base/AnyParameter.h
Expand Up @@ -78,6 +78,11 @@ namespace shogun
return m_value;
}

void set_value(const Any& value)
{
m_value = value;
}

AnyParameterProperties get_properties() const
{
return m_properties;
Expand Down
26 changes: 23 additions & 3 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -45,11 +45,26 @@ namespace shogun
class CSGObject::Self
{
public:
void put(const BaseTag& tag, const AnyParameter& parameter)
void create(const BaseTag& tag, const AnyParameter& parameter)
{
if (has(tag))
{
SG_SERROR("Can not register %s twice", tag.name().c_str())
}
map[tag] = parameter;
}

void update(const BaseTag& tag, const Any& value)
{
if (!has(tag))
{
SG_SERROR(
"Can not update unregistered parameter %s",
tag.name().c_str())
}
map.at(tag).set_value(value);
}

AnyParameter get(const BaseTag& tag) const
{
if(!has(tag))
Expand Down Expand Up @@ -788,10 +803,15 @@ bool CSGObject::clone_parameters(CSGObject* other)
return true;
}

void CSGObject::put_parameter(
void CSGObject::create_parameter(
const BaseTag& _tag, const AnyParameter& parameter)
{
self->put(_tag, parameter);
self->create(_tag, parameter);
}

void CSGObject::update_parameter(const BaseTag& _tag, const Any& value)
{
self->update(_tag, value);
}

AnyParameter CSGObject::get_parameter(const BaseTag& _tag) const
Expand Down
22 changes: 14 additions & 8 deletions src/shogun/base/SGObject.h
Expand Up @@ -340,7 +340,7 @@ class CSGObject
if (has_parameter(_tag))
{
if(has<T>(_tag.name()))
put_parameter(_tag, AnyParameter(erase_type(value)));
update_parameter(_tag, erase_type(value));
else
{
SG_ERROR("Type for parameter with name \"%s\" is not correct.\n",
Expand Down Expand Up @@ -468,7 +468,7 @@ class CSGObject
template <typename T>
void register_param(Tag<T>& _tag, const T& value)
{
put_parameter(_tag, AnyParameter(erase_type(value)));
create_parameter(_tag, AnyParameter(erase_type(value)));
}

/** Registers a class parameter which is identified by a name.
Expand All @@ -483,15 +483,15 @@ class CSGObject
void register_param(const std::string& name, const T& value)
{
BaseTag tag(name);
put_parameter(tag, AnyParameter(erase_type(value)));
create_parameter(tag, AnyParameter(erase_type(value)));
}

template <typename T>
void watch_param(
const std::string& name, T* value, AnyParameterProperties properties)
{
BaseTag tag(name);
put_parameter(
create_parameter(
tag, AnyParameter(erase_type_non_owning(value), properties));
}

Expand Down Expand Up @@ -552,13 +552,19 @@ class CSGObject
*/
bool has_parameter(const BaseTag& _tag) const;

/** Registers and modifies a class parameter, identified by a BaseTag.
* Throws an exception if the class does not have such a parameter.
/** Creates a parameter identified by a BaseTag.
*
* @param _tag name information of parameter
* @param parameter parameter to be created
*/
void create_parameter(const BaseTag& _tag, const AnyParameter& parameter);

/** Updates a parameter identified by a BaseTag.
*
* @param _tag name information of parameter
* @param any value without type information of the parameter
* @param value new value of parameter
*/
void put_parameter(const BaseTag& _tag, const AnyParameter& any);
void update_parameter(const BaseTag& _tag, const Any& value);

/** Getter for a class parameter, identified by a BaseTag.
* Throws an exception if the class does not have such a parameter.
Expand Down

0 comments on commit 99e083f

Please sign in to comment.