Skip to content

Commit

Permalink
add const version of as<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Mar 19, 2018
1 parent 1674d1b commit 82f8e3d
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/shogun/base/SGObject.h
Expand Up @@ -492,36 +492,51 @@ class CSGObject
*/
std::vector<std::string> parameter_names() const;

/**
* Utility method to specialize the feature to the required type.
/** Specializes a provided object to the specified type.
* Throws exception if the object cannot be specialized.
*
* @param sgo CSGObject base type
* @return The requested type if casting was successful.
* @param sgo object of CSGObject base type
* @return The requested type
*/
template<class T> static T* as(CSGObject* sgo)
{
REQUIRE(sgo, "No object provided!\n");
return sgo->as<T>();
}

/**
* Utility method to specialize the feature to the required type.
/** Specializes the object to the specified type.
* Throws exception if the object cannot be specialized.
*
* @param sgo CSGObject base type
* @return The requested type if casting was successful, or throws exception.
* @return The requested type
*/
template<class T> T* as()
{
T* c = dynamic_cast<T*>(this);
auto c = dynamic_cast<T*>(this);
if (c)
return c;

SG_SERROR("The object (%s) cannot be casted to the requested type %s!\n",
SG_SERROR("Object of type %s cannot be converted to type %s.\n",
demangled_type<std::remove_pointer_t<decltype(this)>>().c_str(),
demangled_type<T>().c_str());
return nullptr;
}

/** Specializes the object to the specified type.
* Throws exception if the object cannot be specialized.
*
* @return The requested type
*/
template<class T> const T* as() const
{
auto c = dynamic_cast<const T*>(this);
if (c)
return c;

SG_SERROR("Object of type %s cannot be converted to type %s.\n",
demangled_type<std::remove_pointer_t<decltype(this)>>().c_str(),
demangled_type<T>().c_str());
return nullptr;
}
#ifndef SWIG
/**
* Get parameters observable
Expand Down

0 comments on commit 82f8e3d

Please sign in to comment.