string.hpp
Go to the documentation of this file.
1 
16 /*****************************************************************************
17 ** Ifdefs
18 *****************************************************************************/
19 
20 #ifndef ECL_CONVERTERS_STRINGS_HPP_
21 #define ECL_CONVERTERS_STRINGS_HPP_
22 
23 /*****************************************************************************
24 ** Includes
25 *****************************************************************************/
26 
27 #include <string>
28 #include "converter.hpp"
29 #include "char.hpp"
30 
31 /*****************************************************************************
32 ** Namespaces
33 *****************************************************************************/
34 
35 namespace ecl {
36 
37 /*****************************************************************************
38 ** String Converter Interface
39 *****************************************************************************/
50 template <>
51 class Converter<std::string,int> : public converters::ConverterBase {
52 public:
63  std::string operator ()(const int &input)
64  {
65  std::string s;
66  bool negative = false;
67  char *char_string;
68  #if ECL_SIZE_OF_INT == 4
69  char_string = buffer + 11;
70  #elif ECL_SIZE_OF_INT == 8
71  char_string = buffer + 20;
72  #else // just make sure we have lots of space
73  char_string = buffer + 30;
74  #endif
75  *char_string = '\0';
76  int remaining = input;
77  unsigned int lsd;
78  /*********************
79  ** Fix sign
80  **********************/
81  if (remaining < 0 ) {
82  negative = true;
83  remaining *= -1;
84  }
85  do
86  {
87  --char_string;
88  lsd = remaining%10;
89  remaining /= 10;
90  *char_string = Converter<char,unsigned int>()(lsd);
91  } while ( remaining > 0);
92  if ( negative ) {
93  --char_string;
94  *char_string = '-';
95  }
96  return s.assign(char_string);
97  };
98 
99  virtual ~Converter() {}
100 
101 private:
102  #if ECL_SIZE_OF_INT == 4
103  char buffer[12];
104  #elif ECL_SIZE_OF_INT == 8
105  char buffer[21];
106  #else // just make sure we have lots of space
107  char buffer[31];
108 #endif
109 };
110 
120 template <>
121 class Converter<std::string,void> :
122  public Converter<std::string,int>
123 {
124 public:
125  virtual ~Converter() {}
126 
128 };
129 
130 
131 }; // Namespace ecl
132 
133 
134 #endif /*ECL_CONVERTERS_STRINGS_HPP_*/
Primary templates for the family of converter classes.
Type conversions to a readable (ascii) char type.
Primary template and general fallback for converter classes.
Definition: converter.hpp:56
Output operator()(const Input &input)
Definition: converter.hpp:91
Converts unsigned ints into readable (ascii) characters.
Definition: char.hpp:41


ecl_converters
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:22