]> Creatis software - bbtk.git/blob - packages/std/src/bbstdStringTo.cxx
Feature #1774
[bbtk.git] / packages / std / src / bbstdStringTo.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbstdStringTo.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:32 $
33   Version:   $Revision: 1.5 $
34 =========================================================================*/
35
36 #include "bbstdStringTo.h"
37 #include "bbstdPackage.h"
38
39
40 namespace bbstd 
41 {
42
43   //====================================================================
44   BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(StringTo,
45                                          bbtk::AtomicBlackBox);
46   //====================================================================
47
48   //====================================================================
49   // Template specialization of DoIt
50   template <> void StringTo<bool> ::DoIt()
51   {
52     if ( (bbGetInputIn()=="true") || 
53          (bbGetInputIn()=="TRUE") || 
54          (bbGetInputIn()=="True") || 
55          (bbGetInputIn()=="1")     )  
56     {
57        bbSetOutputOut(true);
58     }
59     else if ( (bbGetInputIn()=="false") || 
60               (bbGetInputIn()=="FALSE") ||
61               (bbGetInputIn()=="False") ||
62               (bbGetInputIn()=="0") )  
63     {   
64        bbSetOutputOut(false); 
65     }
66     else 
67     {
68        bbtkError("cannot convert '"<<bbGetInputIn()<<"' to a bool");
69     }
70   }
71   
72   // Template specialization of DoIt
73   template <> void StringTo<bbtk::Void> ::DoIt()
74   {
75   }
76   
77   template <> void StringTo<int8_t> ::DoIt()
78   {
79     bbSetOutputOut( (int8_t)atoi ( bbGetInputIn().c_str() ) );
80   }
81   
82   template <> void StringTo<uint8_t> ::DoIt()
83   {
84     bbSetOutputOut( (uint8_t)atoi ( bbGetInputIn().c_str() ) );
85   }
86   
87   template <> void StringTo<int16_t> ::DoIt()
88   {
89     bbSetOutputOut( (int16_t)atoi ( bbGetInputIn().c_str() ) );
90   }
91   
92   template <> void StringTo<uint16_t> ::DoIt()
93   {
94     bbSetOutputOut( (uint16_t)atoi ( bbGetInputIn().c_str() ) );
95   }
96   
97   template <> void StringTo<int32_t> ::DoIt()
98   {
99     bbSetOutputOut( (int32_t)atoi ( bbGetInputIn().c_str() ) );
100   }
101   
102   template <> void StringTo<uint32_t> ::DoIt()
103   {
104     bbSetOutputOut( (uint32_t)atoi ( bbGetInputIn().c_str() ) );
105   }
106   
107   template <> void StringTo<float> ::DoIt()
108   {
109     bbSetOutputOut( (float)atof ( bbGetInputIn().c_str() ) );
110   }
111   
112   template <> void StringTo<double> ::DoIt()
113   {
114     bbSetOutputOut( atof ( bbGetInputIn().c_str() ) );
115   }
116   
117   //====================================================================
118   
119   //====================================================================
120   // Add the specialized adaptors to the package
121   typedef bbtk::Void Void;
122   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,bool);
123   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,int8_t);
124   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,uint8_t);
125   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,int16_t);
126   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,uint16_t);
127   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,int32_t);
128   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,uint32_t);
129   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,float);
130   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,double);
131   BBTK_ADD_TEMPLATE_BLACK_BOX_TO_PACKAGE(std,StringTo,Void);
132   //====================================================================
133   
134 } // namespace bbstd