From 022b0d4dfad96ead8839bfe38859489a7df4325a Mon Sep 17 00:00:00 2001
From: Mansard <nmansard@laas.fr>
Date: Fri, 2 Jul 2010 18:22:49 +0200
Subject: [PATCH] Added an example of caster construction for type vector
 (dummy cast, similar to the default one, just for the tuto).

---
 unitTesting/test_signalcast.cpp | 34 ++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/unitTesting/test_signalcast.cpp b/unitTesting/test_signalcast.cpp
index 1d1e949f..e3122caa 100644
--- a/unitTesting/test_signalcast.cpp
+++ b/unitTesting/test_signalcast.cpp
@@ -47,9 +47,37 @@ typedef boost::numeric::ublas::vector<double> Vector;
 // (this could be automated with macros)
 namespace {
 	DefaultCastRegisterer<bool> myBooleanCast;
-	DefaultCastRegisterer<Vector> myVectorCast;
 }
 
+#include <boost/numeric/ublas/vector.hpp>
+#include <boost/numeric/ublas/io.hpp>
+class BoostNumericsCastRegisterer
+  : public SignalCastRegisterer
+{
+  typedef boost::numeric::ublas::vector<double> bnuVector;
+
+  static boost::any castVector(std::istringstream& iss)
+  {  bnuVector res; iss >> res; return res; }
+  static void dispVector(const boost::any& object, std::ostream& os)
+  {
+    const bnuVector & v = boost::any_cast<bnuVector>(object);
+    os << "[ ";
+    for( unsigned int i=0;i<v.size();++i ) os << v(i) << " ";
+    os << " ];" << std::endl;
+  }
+  static void traceVector(const boost::any& object, std::ostream& os)
+  {
+    const bnuVector & v = boost::any_cast<bnuVector>(object);
+    for( unsigned int i=0;i<v.size();++i ) os << v(i) << " ";
+    os << std::endl;
+  }
+public:
+  BoostNumericsCastRegisterer(void)
+    : SignalCastRegisterer(typeid(bnuVector),dispVector,castVector,traceVector) {}
+};
+
+BoostNumericsCastRegisterer myVectorCast;
+
 int main() {
 	using namespace boost::numeric::ublas;
 
@@ -72,8 +100,8 @@ int main() {
 		unit_vector<double> v (5, i);
 		myVectorSignal.setConstant(v);
 		// print out signal value
-		cout << "  ";
-		myVectorSignal.get(cout);
+		try{myVectorSignal.get(cout);}
+		catch( const ExceptionAbstract & exp ) { cout << exp << std::endl; }
 		cout << endl;
 	}
 
-- 
GitLab