Skip to content
Snippets Groups Projects
Commit 2612b806 authored by Francois Bleibel's avatar Francois Bleibel
Browse files

Made DefaultSignalCaster public (exported).

Added boost::ublas::vector example to test_signalcast.
parent 793da6bb
No related branches found
No related tags found
No related merge requests found
......@@ -66,7 +66,7 @@ private:
/// The library-wide instance of SignalCaster
extern DYNAMICGRAPH_EXPORT SignalCaster g_caster;
/*
/*!
* The SignalCast registerer class. Can be used to automatically register a cast when
* instanced somewhere in a cpp file. Pass the typeid() of the type you want to
* register a cast to as the first argument.
......@@ -80,6 +80,17 @@ public:
}
};
/*! This class can be used to register default casts, i.e. casts already supported by
* the object to an std::iostream through the operators >> and << .
*/
template<typename T> class DefaultCastRegisterer : public SignalCastRegisterer {
public:
DefaultCastRegisterer() : SignalCastRegisterer(typeid(T), disp, cast, trace) {}
static boost::any cast(std::istringstream& iss) { T inst; iss >> inst; return inst; }
static void disp(const boost::any& object, std::ostream& os) { os << boost::any_cast<T>(object) << std::endl;; }
static void trace(const boost::any& object, std::ostream& os) { disp(object,os); }
};
/*!
* Global signal cast template (helper) functions
*
......
......@@ -76,15 +76,6 @@ any SignalCaster::cast(const type_info& type, istringstream& iss) {
/// The global instance of the caster class.
SignalCaster g_caster;
/// Default casts, such as casts already supported by std::iostream
template<typename T> class DefaultCastRegisterer : public SignalCastRegisterer {
public:
DefaultCastRegisterer() : SignalCastRegisterer(typeid(T), disp, cast, trace) {}
static boost::any cast(istringstream& iss) { T inst; iss >> inst; return inst; }
static void disp(const any& object, ostream& os) { os << any_cast<T>(object) << endl;; }
static void trace(const any& object, ostream& os) { disp(object,os); }
};
/// Registers useful casts
namespace {
DefaultCastRegisterer<double> double_reg;
......
......@@ -25,19 +25,34 @@
#include <string>
#include <iostream>
#include <cstdlib>
#include <memory>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal.h>
#include <memory>
// only included because we will define new casts here. Not needed in general.
#include <dynamic-graph/signal-caster.h>
using namespace std;
using namespace dynamicgraph;
// define a new cast with a ublas vector
#include <boost/numeric/ublas/vector.hpp>
// this header is *needed* to define iostream& operators << and >> for a ublas vector
#include <boost/numeric/ublas/io.hpp>
typedef boost::numeric::ublas::vector<double> Vector;
// define a new cast with a type that supports streaming operators to and from it
// (this could be automated with macros)
namespace {
DefaultCastRegisterer<bool> myBooleanCast;
DefaultCastRegisterer<Vector> myVectorCast;
}
int main() {
using namespace boost::numeric::ublas;
Signal<double, int> mySignal("out");
istringstream value("42.0");
cout << "[cast] Setting signal value to " << value.str() << endl;
......@@ -47,5 +62,21 @@ int main() {
cout << "[trace] Printing out trace: ";
mySignal.trace(cout);
Signal<Vector, int> myVectorSignal("vector");
// print out signal name
cout << " " << myVectorSignal << endl;
cout << "[disp] Enumerating boost unit vectors" << endl;
for (int i = 0; i < 5; ++ i) {
unit_vector<double> v (5, i);
myVectorSignal.setConstant(v);
// print out signal value
cout << " ";
myVectorSignal.get(cout);
cout << endl;
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment