]> Creatis software - bbtk.git/blob - packages/std/src/bbstdVectorToString.h
3c28ff1583e05cce812b7a8e4653e4127484a697
[bbtk.git] / packages / std / src / bbstdVectorToString.h
1 #ifndef __bbstdVectorToString_INCLUDED_h__
2 #define __bbstdVectorToString_INCLUDED_h__
3
4 #include "bbtkUserBlackBox.h"
5
6 namespace bbstd
7 {
8
9   //=================================================================
10   // UserBlackBox declaration
11   template <class T>
12   class VectorToString : public bbtk::UserBlackBox
13   {  
14     BBTK_USER_BLACK_BOX_INTERFACE(VectorToString,bbtk::UserBlackBox);
15     BBTK_DECLARE_INPUT(In,std::vector<T>);
16     BBTK_DECLARE_INPUT(Separator,std::string);
17     BBTK_DECLARE_OUTPUT(Out,std::string);
18     BBTK_PROCESS(DoIt);
19     void DoIt(); 
20     virtual void bbUserConstructor();
21   };
22   //=================================================================
23   
24   
25   
26   //=================================================================
27   // UserBlackBox description
28   BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(VectorToString);
29   BBTK_NAME(bbtk::TypeName<std::vector<T> >()+"ToString");
30   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
31   BBTK_DEFAULT_ADAPTOR();
32   BBTK_DESCRIPTION("Converts the content of the input vector ("+bbtk::TypeName<std::vector<T> >()+") to a string in which each item is separated by the value of the input 'Separator'");
33   typedef std::vector<T> Tvector;
34   BBTK_TEMPLATE_INPUT(VectorToString, In,"Input",Tvector);
35   BBTK_TEMPLATE_INPUT(VectorToString, Separator,"Separator of item in the output string. Default is whitespace.",std::string);
36   BBTK_TEMPLATE_OUTPUT(VectorToString, Out,"Output",std::string);
37   BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(VectorToString);
38   //=================================================================
39   
40
41   
42   //=================================================================
43   template <class T>
44   void VectorToString<T>::DoIt()
45   {
46     std::string out;
47     typename std::vector<T>::const_iterator i;
48     for (i=bbmInputIn.begin();i!=bbmInputIn.end();++i)
49       {
50         if (i!=bbmInputIn.begin()) out += bbGetInputSeparator();
51         std::ostringstream s;                           
52         s <<  *i;
53         out += s.str();
54       }
55     bbSetOutputOut(out);
56   }
57   //=================================================================
58   
59   //=================================================================
60   template <class T>
61   void VectorToString<T>::bbUserConstructor()
62   {
63     bbSetInputSeparator(" ");
64   }
65   //=================================================================
66
67 } // namespace bbstd
68
69 #endif //__bbstdVectorToString_INCLUDED_h__
70