Skip to content

Commit

Permalink
Make naming more consistent with C++17 (#4080)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jan 15, 2018
1 parent d48dc4a commit 521f217
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 90 deletions.
13 changes: 6 additions & 7 deletions src/shogun/base/SGObject.h
Expand Up @@ -342,7 +342,7 @@ class CSGObject
if(has<T>(_tag.name()))
{
ref_value(&value);
update_parameter(_tag, erase_type(value));
update_parameter(_tag, make_any(value));
}
else
{
Expand Down Expand Up @@ -382,15 +382,15 @@ class CSGObject
const Any value = get_parameter(_tag).get_value();
try
{
return recall_type<T>(value);
return any_cast<T>(value);
}
catch (const std::logic_error& exc)
{
SG_ERROR(
"Get \"%s\" failed: %s.\n", _tag.name().c_str(), exc.what());
}
// we won't be there
return recall_type<T>(value);
return any_cast<T>(value);
}

/** Getter for a class parameter, identified by a name.
Expand Down Expand Up @@ -477,7 +477,7 @@ class CSGObject
template <typename T>
void register_param(Tag<T>& _tag, const T& value)
{
create_parameter(_tag, AnyParameter(erase_type(value)));
create_parameter(_tag, AnyParameter(make_any(value)));
}

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

/** Puts a pointer to some parameter into the parameter map.
Expand All @@ -506,8 +506,7 @@ class CSGObject
const std::string& name, T* value, AnyParameterProperties properties)
{
BaseTag tag(name);
create_parameter(
tag, AnyParameter(erase_type_non_owning(value), properties));
create_parameter(tag, AnyParameter(make_any_ref(value), properties));
}

public:
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/evaluation/CrossValidation.cpp
Expand Up @@ -111,7 +111,7 @@ CEvaluationResult* CCrossValidation::evaluate_impl()

/* Emit the value*/
std::string obs_value_name{"cross_validation_run"};
ObservedValue cv_data{i, obs_value_name, erase_type(storage),
ObservedValue cv_data{i, obs_value_name, make_any(storage),
CROSSVALIDATION};
observe(cv_data);
SG_UNREF(storage)
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/io/TBOutputFormat.cpp
Expand Up @@ -49,7 +49,7 @@ using namespace shogun;
typeid(type).hash_code()) \
{ \
summaryValue->set_simple_value( \
recall_type<type>(value.first.get_value())); \
any_cast<type>(value.first.get_value())); \
}

#define CHECK_TYPE_HISTO(type) \
Expand All @@ -59,7 +59,7 @@ using namespace shogun;
{ \
tensorflow::histogram::Histogram h; \
tensorflow::HistogramProto* hp = new tensorflow::HistogramProto(); \
auto v = recall_type<type>(value.first.get_value()); \
auto v = any_cast<type>(value.first.get_value()); \
for (auto value_v : v) \
h.Add(value_v); \
h.EncodeToProto(hp, true); \
Expand Down Expand Up @@ -87,7 +87,7 @@ tensorflow::Event TBOutputFormat::convert_scalar(
typeid(int8_t).hash_code())
{
summaryValue->set_simple_value(
recall_type<int8_t>(value.first.get_value()));
any_cast<int8_t>(value.first.get_value()));
}
CHECK_TYPE(uint8_t)
CHECK_TYPE(int16_t)
Expand Down Expand Up @@ -127,7 +127,7 @@ tensorflow::Event TBOutputFormat::convert_vector(
{
tensorflow::histogram::Histogram h;
tensorflow::HistogramProto* hp = new tensorflow::HistogramProto();
auto v = recall_type<std::vector<int8_t>>(value.first.get_value());
auto v = any_cast<std::vector<int8_t>>(value.first.get_value());
for (auto value_v : v)
h.Add(value_v);
h.EncodeToProto(hp, true);
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/lib/any.h
Expand Up @@ -742,19 +742,19 @@ namespace shogun
/** Erases value type i.e. converts it to Any
* For input object of any type, it returns an Any object
* which stores the input object's raw value. It saves the type
* information internally to be recalled later by using recall_type().
* information internally to be cast back later by using any_cast().
*
* @param v value
* @return Any object with the input value
*/
template <typename T>
inline Any erase_type(const T& v)
inline Any make_any(const T& v)
{
return Any(v);
}

template <typename T>
inline Any erase_type_non_owning(T* v)
inline Any make_any_ref(T* v)
{
return Any(non_owning_policy<T>(), v);
}
Expand All @@ -768,7 +768,7 @@ namespace shogun
* @return type-casted value
*/
template <typename T>
inline T recall_type(const Any& any)
inline T any_cast(const Any& any)
{
return any.as<T>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/parameter_observers/ParameterObserverCV.cpp
Expand Up @@ -63,7 +63,7 @@ void CParameterObserverCV::on_next(const shogun::TimedObservedValue& value)
typeid(CrossValidationStorage*).hash_code())
{
CrossValidationStorage* recalled_value =
recall_type<CrossValidationStorage*>(value.first.get_value());
any_cast<CrossValidationStorage*>(value.first.get_value());
SG_REF(recalled_value);

/* Print information on screen if enabled*/
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/io/TBOutputFormat_unittest.cc
Expand Up @@ -60,7 +60,7 @@ void test_case_scalar(T value_val)

time_point timestamp;
std::string param_name = "test";
ObservedValue emitted_value{1, param_name, erase_type(v), TENSORBOARD};
ObservedValue emitted_value{1, param_name, make_any(v), TENSORBOARD};

std::string node_name = "node";
auto event_gen =
Expand All @@ -78,7 +78,7 @@ void test_case_scalar_error(T value_val)

time_point timestamp;
std::string param_name = "test";
ObservedValue emitted_value{1, param_name, erase_type(v), TENSORBOARD};
ObservedValue emitted_value{1, param_name, make_any(v), TENSORBOARD};

std::string node_name = "node";
EXPECT_THROW(
Expand Down Expand Up @@ -106,7 +106,7 @@ void test_case_vector(std::vector<T> v)

time_point timestamp;
std::string param_name = "test";
ObservedValue emitted_value{1, param_name, erase_type(v), TENSORBOARD};
ObservedValue emitted_value{1, param_name, make_any(v), TENSORBOARD};

std::string node_name = "node";
auto event_gen =
Expand All @@ -127,7 +127,7 @@ void test_case_vector_error(std::vector<T> v)

time_point timestamp;
std::string param_name = "test";
ObservedValue emitted_value{1, param_name, erase_type(v), TENSORBOARD};
ObservedValue emitted_value{1, param_name, make_any(v), TENSORBOARD};

std::string node_name = "node";
EXPECT_THROW(
Expand Down

0 comments on commit 521f217

Please sign in to comment.