Skip to content

Commit

Permalink
Add clone_utils::clone() method to clone automatically Shogun's objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
geektoni committed May 13, 2019
1 parent f94bedb commit c8b39fe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
32 changes: 5 additions & 27 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <unordered_map>
#include <utility>
#include <vector>
#include <shogun/util/clone.h>

/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
Expand Down Expand Up @@ -1004,16 +1005,7 @@ class CSGObject
* @param step step
* @param name tag's name
*/
template<class T,
typename std::enable_if_t<
!is_sg_vector<T>::value &&
!is_sg_matrix<T>::value>* = nullptr>
void observe(const int64_t step, const std::string& name) const;

template<class T,
typename std::enable_if_t<
is_sg_vector<T>::value ||
is_sg_matrix<T>::value>* = nullptr>
template <class T>
void observe(const int64_t step, const std::string& name) const;

/**
Expand Down Expand Up @@ -1315,27 +1307,13 @@ void CSGObject::observe(
this->observe(obs);
}

template<class T,
typename std::enable_if_t<
!is_sg_vector<T>::value &&
!is_sg_matrix<T>::value>*>
void CSGObject::observe(const int64_t step, const std::string& name) const
{
auto param = this->get_parameter(BaseTag(name));
auto obs = some<ObservedValueTemplated<T>>(
step, name, any_cast<T>(param.get_value()), param.get_properties());
this->observe(obs);
}

template<class T,
typename std::enable_if_t<
is_sg_vector<T>::value ||
is_sg_matrix<T>::value>*>
template <class T>
void CSGObject::observe(const int64_t step, const std::string& name) const
{
auto param = this->get_parameter(BaseTag(name));
auto cloned = any_cast<T>(param.get_value());
auto obs = some<ObservedValueTemplated<T>>(
step, name, any_cast<T>(param.get_value()).clone(), param.get_properties());
step, name, static_cast<T>(clone_utils::clone(cloned)), param.get_properties());
this->observe(obs);
}

Expand Down
63 changes: 63 additions & 0 deletions src/shogun/util/clone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Written (W) 2019 Giovanni De Toni
*/

#include <type_traits>

#ifndef SHOGUN_CLONE_H
#define SHOGUN_CLONE_H

namespace shogun {
namespace clone_utils
{

struct clone_by_cctor
{
};

struct clone_from_value : clone_by_cctor
{
};

struct clone_from_pointer : clone_from_value
{
};

template <class T, std::enable_if_t<std::is_copy_constructible<T>::value>* = nullptr>
inline T clone_impl(clone_by_cctor, const T& value)
{
return T(value);
}

template <class T>
inline auto clone_impl(clone_from_value, T& value)
-> decltype(value.clone())
{
return value.clone();
}

template <class T>
inline auto clone_impl(clone_from_pointer, T* value)
-> decltype(value->clone())
{
return value->clone();
}

/**
* Clone a Shogun object by calling its clone() method.
* It also works with non-Shogun object by using the copy
* constructor.
* @tparam T type
* @param value value we want to clone
* @return the cloned value
*/
template <class T>
inline auto clone(T& value)
-> decltype(clone_impl(clone_from_pointer(), value))
{
return clone_impl(clone_from_pointer(), value);
}
}
}

#endif //SHOGUN_CLONE_H

0 comments on commit c8b39fe

Please sign in to comment.