integers.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Ifdefs
12 *****************************************************************************/
13 
14 #ifndef ECL_CONVERTERS_CONVERTERS_INTEGERS_HPP_
15 #define ECL_CONVERTERS_CONVERTERS_INTEGERS_HPP_
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <string>
22 #include <sstream>
24 #include "converter.hpp"
25 
26 /*****************************************************************************
27 ** Namespaces
28 *****************************************************************************/
29 
30 namespace ecl {
31 
32 /*****************************************************************************
33 ** Interfaces
34 *****************************************************************************/
35 
42 template <>
43 class Converter<int,std::string> : public converters::ConverterBase {
44 public:
45  virtual ~Converter() {};
57  int operator()(const std::string &input) {
58  int i;
59  std::istringstream stream(input);
60  char c;
61  if ( !( stream >> i ) || (stream.get(c))) { // Second part checks that there isn't leftover characters
63  error_handler = ConversionError;
64  }
65  return i;
66  }
67 };
75 template <>
76 class Converter<int,char> : public converters::ConverterBase {
77 public:
88  int operator()(char c) {
89  if ( ( c < '0' ) || (c > '9') ) {
90  ecl_debug_throw(StandardException(LOC,ConversionError));
91  error_handler = ConversionError;
92  }
93  return static_cast<int>(c - '0');
94  }
95 };
96 
105 template <>
106 class Converter<int,unsigned char*> {
107 private:
108  Converter() {
109  // This class is being depracated! Use the byte array converters instead (converters/from_byte_array.hpp)
110  }
111 public:
112 // /**
113 // * Converts a byte array to an integer type. The bytes are ordered from
114 // * least significant to most significant (little-endian). Warning: the
115 // * byte array must be the same size as the integer that it is being
116 // * converted to (i.e. usually 4 bytes).
117 // *
118 // * @param byte_array : the input string to be converted.
119 // * @return int : the integer representation of the digit.
120 // **/
121 // int operator()(const unsigned char* byte_array) {
122 // int value = 0;
123 // for (unsigned int i = 0; i < sizeof(int); ++i ) {
124 // value |= *(byte_array+i) << 8*i;
125 // }
126 // return value;
127 // }
128 };
137 template <>
138 class Converter<int,char*> : public converters::ConverterBase {
139 private:
140  Converter() {
141  // This class is being depracated! Use the byte array converters instead (converters/from_byte_array.hpp)
142  }
143 public:
144 // NEW
145 // /**
146 // * Converts a c string to an integer type. This converts
147 // * ascii characters to an integer, not bytes to integer. For that
148 // * see the byte array converters in this package.
149 // *
150 // * @todo Add checks to ensure the range of int is not exceeded.
151 // *
152 // * @param c_str : the input string to be converted.
153 // * @return int : the integer representation of the digit.
154 // *
155 // * @exception : throws in debug mode if a non-digit char is found (debug mode only).
156 // **/
157 // int operator()(const char* c_str) {
158 // int value = 0;
159 // bool negative_flag = false;
160 // unsigned int starting_index = 0;
161 // if ( c_str[0] == '-' ) {
162 // negative_flag = true;
163 // starting_index = 1;
164 // }
165 // size_t length = ::strlen(c_str);
166 // int multiplier = 1;
167 // for ( unsigned int i = length-1; i >= starting_index; --i ) {
168 // if ( ( c_str[i] < '0' ) || (c_str[i] > '9') ) {
169 // ecl_debug_throw(StandardException(LOC,ConversionError));
170 // error_handler = ConversionError;
171 // }
172 // value += multiplier*static_cast<int>(c_str[i] - '0');
173 // multiplier *= 10;
174 // }
175 // if ( negative_flag) {
176 // value *= -1;
177 // }
178 // return value;
179 // }
180 // OLD
181 // /**
182 // * Converts a char array to an integer type. The bytes are ordered from
183 // * least significant to most significant (little-endian). Warning: the
184 // * byte array must be the same size as the integer that it is being
185 // * converted to (i.e. usually 4 bytes).
186 // *
187 // * @param byte_array : the input string to be converted.
188 // * @return int : the integer representation of the digit.
189 // **/
190 // int operator()(const char* byte_array) {
191 // if ( ::strlen(byte_array) != sizeof(int) ) {
192 // ecl_debug_throw(StandardException(LOC,InvalidInputError,"Input char string length is too large/small for conversion to int."));
193 // }
194 // int value = 0;
195 // for (unsigned int i = 0; i < sizeof(int); ++i ) {
196 // value |= static_cast<unsigned char>(*(byte_array+i)) << 8*i;
197 // }
198 // return value;
199 // }
200 };
201 
211 template <>
212 class Converter<int,void> :
213  public Converter<int,std::string>,
214  public Converter<int,char>
215 // public Converter<int,unsigned char*>,
216 // public Converter<int,char*>
217 {
218  public:
221 // using Converter<int,unsigned char*>::operator();
222 // using Converter<int,char*>::operator();
223 };
224 
225 
226 } // namespace ecl
227 
228 #endif /* ECL_CONVERTERS_INTEGERS_HPP_ */
ecl::Converter::operator()
Output operator()(const Input &input)
Definition: converter.hpp:97
converter.hpp
Primary templates for the family of converter classes.
ecl::StandardException
ecl::Converter::~Converter
virtual ~Converter()
Definition: converter.hpp:72
standard_exception.hpp
ecl::ConversionError
ConversionError
ecl
ecl::Converter
Primary template and general fallback for converter classes.
Definition: converter.hpp:62
ecl_debug_throw
#define ecl_debug_throw(exception)


ecl_converters
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:25