Skip to content

Commit

Permalink
Implement parameter-based to_string (#4067)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jan 4, 2018
1 parent 3344e91 commit 79b3e76
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/interfaces/swig/SGBase.i
Expand Up @@ -229,7 +229,8 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
}

CSGObject *obj = reinterpret_cast<CSGObject*>(argp);
fprintf(f, "%s", obj->get_name());
std::string s = obj->to_string();
fprintf(f, "%s", s.c_str());
return 0;
}
%}
Expand Down Expand Up @@ -259,7 +260,8 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
return SWIG_ERROR;
}
CSGObject *obj = reinterpret_cast<CSGObject*>(argp);
fprintf(f, "%s", obj->get_name());
std::string s = obj->to_string();
fprintf(f, "%s", s.c_str());
return 0;
}
%}
Expand Down Expand Up @@ -332,14 +334,14 @@ namespace shogun

%extend CSGObject
{
const char* __str__() const
std::string __str__() const
{
return $self->get_name();
return $self->to_string();
}

const char* __repr__() const
std::string __repr__() const
{
return $self->get_name();
return $self->to_string();
}

PyObject* __reduce_ex__(int proto)
Expand Down
92 changes: 92 additions & 0 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -923,3 +923,95 @@ void CSGObject::ref_value(CSGObject* const* value)
void CSGObject::ref_value(...)
{
}

class ToStringVisitor : public AnyVisitor
{
public:
ToStringVisitor(std::stringstream* stream) : AnyVisitor(), m_stream(stream)
{
}

virtual void on(bool* v)
{
stream() << (*v ? "true" : "false");
}
virtual void on(int32_t* v)
{
stream() << *v;
}
virtual void on(int64_t* v)
{
stream() << *v;
}
virtual void on(float* v)
{
stream() << *v;
}
virtual void on(double* v)
{
stream() << *v;
}
virtual void on(CSGObject** v)
{
if (*v)
{
stream() << (*v)->get_name() << "(...)";
}
else
{
stream() << "null";
}
}
virtual void on(SGVector<int>*)
{
stream() << "[...]";
}
virtual void on(SGVector<float>*)
{
stream() << "[...]";
}
virtual void on(SGVector<double>*)
{
stream() << "[...]";
}
virtual void on(SGMatrix<int>*)
{
stream() << "[...]";
}
virtual void on(SGMatrix<float>*)
{
stream() << "[...]";
}
virtual void on(SGMatrix<double>*)
{
stream() << "[...]";
}

private:
std::stringstream& stream()
{
return *m_stream;
}

private:
std::stringstream* m_stream;
};

std::string CSGObject::to_string() const
{
std::stringstream ss;
auto visitor = std::make_unique<ToStringVisitor>(&ss);
ss << get_name();
ss << "(";
for (auto it = self->map.begin(); it != self->map.end(); ++it)
{
ss << it->first.name() << "=";
it->second.get_value().visit(visitor.get());
if (std::next(it) != (self->map.end()))
{
ss << ",";
}
}
ss << ")";
return ss.str();
}
6 changes: 6 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -406,6 +406,12 @@ class CSGObject
return get(tag);
}

/** Returns string representation of the object that contains
* its name and parameters.
*
*/
std::string to_string() const;

#ifndef SWIG
/**
* Get parameters observable
Expand Down
23 changes: 16 additions & 7 deletions src/shogun/lib/any.h
Expand Up @@ -220,10 +220,10 @@ namespace shogun

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

/** @brief This is one concrete implementation of policy that
Expand Down Expand Up @@ -303,9 +303,9 @@ namespace shogun
* @param storage pointer to a pointer to storage
* @param visitor abstract visitor to use
*/
virtual void visit(void** storage, AnyVisitor* visitor) const
virtual void visit(void* storage, AnyVisitor* visitor) const
{
visitor->on(reinterpret_cast<T*>(*storage));
visitor->on(reinterpret_cast<T*>(storage));
}
};

Expand Down Expand Up @@ -379,12 +379,12 @@ namespace shogun

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

Expand Down Expand Up @@ -568,6 +568,15 @@ namespace shogun
return policy->type_info();
}

/** Visitor pattern. Calls the appropriate 'on' method of AnyVisitor.
*
* @param visitor visitor object to use
*/
void visit(AnyVisitor* visitor) const
{
policy->visit(storage, visitor);
}

private:
void set_or_inherit(const Any& other)
{
Expand Down

0 comments on commit 79b3e76

Please sign in to comment.