integers.hpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Ifdefs
00012 *****************************************************************************/
00013 
00014 #ifndef ECL_CONVERTERS_CONVERTERS_INTEGERS_HPP_
00015 #define ECL_CONVERTERS_CONVERTERS_INTEGERS_HPP_
00016 
00017 /*****************************************************************************
00018 ** Includes
00019 *****************************************************************************/
00020 
00021 #include <string>
00022 #include <sstream>
00023 #include <ecl/exceptions/standard_exception.hpp>
00024 #include "converter.hpp"
00025 
00026 /*****************************************************************************
00027 ** Namespaces
00028 *****************************************************************************/
00029 
00030 namespace ecl {
00031 
00032 /*****************************************************************************
00033 ** Interfaces
00034 *****************************************************************************/
00035 
00042 template <>
00043 class Converter<int,std::string> : public converters::ConverterBase {
00044 public:
00045         virtual ~Converter() {};
00057         int operator()(const std::string &input) ecl_debug_throw_decl(StandardException) {
00058                 int i;
00059                 std::istringstream stream(input);
00060                 char c;
00061                 if ( !( stream >> i ) || (stream.get(c))) { // Second part checks that there isn't leftover characters
00062                         ecl_debug_throw(StandardException(LOC,ConversionError));
00063                         error_handler = ConversionError;
00064                 }
00065                 return i;
00066         }
00067 };
00075 template <>
00076 class Converter<int,char> : public converters::ConverterBase {
00077 public:
00088         int operator()(char c) ecl_debug_throw_decl(StandardException) {
00089                 if ( ( c < '0' ) || (c > '9') ) {
00090                         ecl_debug_throw(StandardException(LOC,ConversionError));
00091                         error_handler = ConversionError;
00092                 }
00093                 return static_cast<int>(c - '0');
00094         }
00095 };
00096 
00105 template <>
00106 class Converter<int,unsigned char*> {
00107 private:
00108         Converter() {
00109                 // This class is being depracated! Use the byte array converters instead (converters/from_byte_array.hpp)
00110         }
00111 public:
00112 //      /**
00113 //       * Converts a byte array to an integer type. The bytes are ordered from
00114 //       * least significant to most significant (little-endian). Warning: the
00115 //       * byte array must be the same size as the integer that it is being
00116 //       * converted to (i.e. usually 4 bytes).
00117 //       *
00118 //       * @param byte_array : the input string to be converted.
00119 //       * @return int : the integer representation of the digit.
00120 //       **/
00121 //      int operator()(const unsigned char* byte_array) {
00122 //              int value = 0;
00123 //              for (unsigned int i = 0; i < sizeof(int); ++i ) {
00124 //                      value |= *(byte_array+i) << 8*i;
00125 //              }
00126 //              return value;
00127 //      }
00128 };
00137 template <>
00138 class Converter<int,char*> : public converters::ConverterBase {
00139 private:
00140         Converter() {
00141                 // This class is being depracated! Use the byte array converters instead (converters/from_byte_array.hpp)
00142         }
00143 public:
00144 // NEW
00145 //        /**
00146 //         * Converts a c string to an integer type. This converts
00147 //         * ascii characters to an integer, not bytes to integer. For that
00148 //         * see the byte array converters in this package.
00149 //         *
00150 //         * @todo Add checks to ensure the range of int is not exceeded.
00151 //         *
00152 //         * @param c_str : the input string to be converted.
00153 //         * @return int : the integer representation of the digit.
00154 //         *
00155 //         * @exception : throws in debug mode if a non-digit char is found (debug mode only).
00156 //         **/
00157 //        int operator()(const char* c_str) ecl_debug_throw_decl(StandardException) {
00158 //              int value = 0;
00159 //              bool negative_flag = false;
00160 //              unsigned int starting_index = 0;
00161 //              if ( c_str[0] == '-' ) {
00162 //                      negative_flag = true;
00163 //                      starting_index = 1;
00164 //              }
00165 //              size_t length = ::strlen(c_str);
00166 //              int multiplier = 1;
00167 //              for ( unsigned int i = length-1; i >= starting_index; --i ) {
00168 //                if ( ( c_str[i] < '0' ) || (c_str[i] > '9') ) {
00169 //                    ecl_debug_throw(StandardException(LOC,ConversionError));
00170 //                    error_handler = ConversionError;
00171 //                }
00172 //                value += multiplier*static_cast<int>(c_str[i] - '0');
00173 //                multiplier *= 10;
00174 //              }
00175 //              if ( negative_flag) {
00176 //                      value *= -1;
00177 //              }
00178 //            return value;
00179 //        }
00180 // OLD
00181 //      /**
00182 //       * Converts a char array to an integer type. The bytes are ordered from
00183 //       * least significant to most significant (little-endian). Warning: the
00184 //       * byte array must be the same size as the integer that it is being
00185 //       * converted to (i.e. usually 4 bytes).
00186 //       *
00187 //       * @param byte_array : the input string to be converted.
00188 //       * @return int : the integer representation of the digit.
00189 //       **/
00190 //      int operator()(const char* byte_array) ecl_debug_throw_decl(StandardException) {
00191 //              if ( ::strlen(byte_array) != sizeof(int) ) {
00192 //                      ecl_debug_throw(StandardException(LOC,InvalidInputError,"Input char string length is too large/small for conversion to int."));
00193 //              }
00194 //              int value = 0;
00195 //              for (unsigned int i = 0; i < sizeof(int); ++i ) {
00196 //                      value |= static_cast<unsigned char>(*(byte_array+i)) << 8*i;
00197 //              }
00198 //      return value;
00199 //      }
00200 };
00201 
00211 template <>
00212 class Converter<int,void>  :
00213         public Converter<int,std::string>,
00214         public Converter<int,char>
00215 //        public Converter<int,unsigned char*>,
00216 //        public Converter<int,char*>
00217 {
00218     public:
00219         using Converter<int,std::string>::operator();
00220         using Converter<int,char>::operator();
00221 //        using Converter<int,unsigned char*>::operator();
00222 //        using Converter<int,char*>::operator();
00223 };
00224 
00225 
00226 } // namespace ecl
00227 
00228 #endif /* ECL_CONVERTERS_INTEGERS_HPP_ */


ecl_converters
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:24