Skip to content

Commit

Permalink
Implement basic visitor for Any (shogun-toolbox#4065)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn authored and dgkim5360 committed Jan 7, 2018
1 parent ab4f0ec commit ddee385
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions src/shogun/lib/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,49 @@ namespace shogun
NON_OWNING
};

class CSGObject;
template <class T>
class SGVector;
template <class T>
class SGMatrix;

/** Used to denote an empty Any object */
struct Empty
{
/** Equality operator */
bool operator==(const Empty& other) const
{
return true;
}
};

class AnyVisitor
{
public:
virtual ~AnyVisitor() = default;

virtual void on(bool*) = 0;
virtual void on(int32_t*) = 0;
virtual void on(int64_t*) = 0;
virtual void on(float*) = 0;
virtual void on(double*) = 0;
virtual void on(CSGObject**) = 0;
virtual void on(SGVector<int>*) = 0;
virtual void on(SGVector<float>*) = 0;
virtual void on(SGVector<double>*) = 0;
virtual void on(SGMatrix<int>*) = 0;
virtual void on(SGMatrix<float>*) = 0;
virtual void on(SGMatrix<double>*) = 0;

void on(Empty*)
{
}

void on(...)
{
}
};

namespace any_detail
{

Expand Down Expand Up @@ -174,6 +217,13 @@ namespace shogun
* @return type of policy
*/
virtual PolicyType policy_type() const = 0;

/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to a pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void** storage, AnyVisitor* visitor) const = 0;
};

/** @brief This is one concrete implementation of policy that
Expand Down Expand Up @@ -247,6 +297,16 @@ namespace shogun
{
return PolicyType::OWNING;
}

/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to a pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void** storage, AnyVisitor* visitor) const
{
visitor->on(reinterpret_cast<T*>(*storage));
}
};

template <typename T>
Expand Down Expand Up @@ -316,6 +376,16 @@ namespace shogun
{
return PolicyType::NON_OWNING;
}

/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param storage pointer to a pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void** storage, AnyVisitor* visitor) const
{
visitor->on(reinterpret_cast<T*>(*storage));
}
};

template <typename T>
Expand Down Expand Up @@ -371,8 +441,6 @@ namespace shogun
class Any
{
public:
/** Used to denote an empty Any object */
struct Empty;

/** Empty value constructor */
Any() : Any(owning_policy<Empty>(), nullptr)
Expand Down Expand Up @@ -538,16 +606,6 @@ namespace shogun
return !(lhs == rhs);
}

/** Used to denote an empty Any object */
struct Any::Empty
{
/** Equality operator */
bool operator==(const Empty& other) const
{
return true;
}
};

/** 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
Expand Down

0 comments on commit ddee385

Please sign in to comment.