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