"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/include/fimex/String2Type.h" (30 Oct 2019, 3402 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "String2Type.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Fimex
    3  *
    4  * (C) Copyright 2008, met.no
    5  *
    6  * Project Info:  https://wiki.met.no/fimex/start
    7  *
    8  * This library is free software; you can redistribute it and/or modify it
    9  * under the terms of the GNU Lesser General Public License as published by
   10  * the Free Software Foundation; either version 2.1 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   16  * License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   21  * USA.
   22  */
   23 
   24 #ifndef FIMEX_STRING2TYPE_H_
   25 #define FIMEX_STRING2TYPE_H_
   26 
   27 #include <sstream>
   28 #include <stdexcept>
   29 #include <vector>
   30 
   31 namespace MetNoFimex {
   32 
   33 // based on https://stackoverflow.com/a/1055563
   34 template <typename T>
   35 struct TypeNameTraits;
   36 #define TYPENAMETRAITS2(X, Y) template <> struct TypeNameTraits<X> \
   37     { static std::string name() { return Y; } };
   38 #define TYPENAMETRAITS1(X) TYPENAMETRAITS2(X, #X)
   39 TYPENAMETRAITS1(char);
   40 TYPENAMETRAITS1(short);
   41 TYPENAMETRAITS1(int);
   42 TYPENAMETRAITS1(long);
   43 TYPENAMETRAITS1(long long);
   44 TYPENAMETRAITS1(unsigned char);
   45 TYPENAMETRAITS1(unsigned int);
   46 TYPENAMETRAITS1(unsigned short);
   47 TYPENAMETRAITS1(unsigned long);
   48 TYPENAMETRAITS1(unsigned long long);
   49 TYPENAMETRAITS1(double);
   50 TYPENAMETRAITS1(float);
   51 #undef TYPENAMETRAITS1
   52 #undef TYPENAMETRAITS2
   53 
   54 class string2type_error : public std::runtime_error {
   55 public:
   56     string2type_error(const std::string& t, const std::string& tn, const std::string& info = std::string())
   57         : std::runtime_error("could not parse all of '" + t + "' as " + tn + info)
   58         , text(t), type_name(tn) {}
   59     const std::string text, type_name;
   60 };
   61 
   62 template <typename T, typename R = T>
   63 T string2type(const std::string& s)
   64 {
   65     T retVal;
   66     bool ok = !s.empty();
   67 
   68     if (std::is_arithmetic<T>() && ok) {
   69         const char c = s[0];
   70         ok = (c == '-') || (std::is_floating_point<T>() && c == '.') || std::isdigit(c);
   71     }
   72     if (ok) {
   73         std::istringstream buffer(s);
   74         buffer >> retVal;
   75         ok = buffer.eof() && !buffer.fail();
   76     }
   77     if (!ok)
   78         throw string2type_error(s, TypeNameTraits<R>::name());
   79     return retVal;
   80 }
   81 
   82 //! convert char to digits, not bytes
   83 template <>
   84 inline char string2type(const std::string& s)
   85 {
   86     return static_cast<char>(string2type<int, char>(s));
   87 }
   88 
   89 //! convert unsigned char to digits, not bytes
   90 template <>
   91 inline unsigned char string2type(const std::string& s)
   92 {
   93     return static_cast<unsigned char>(string2type<unsigned int, unsigned char>(s));
   94 }
   95 
   96 //! no conversion for std::string
   97 template <>
   98 inline std::string string2type<std::string>(const std::string& s)
   99 {
  100     return s;
  101 }
  102 
  103 //! recognize on,true,1 as true and off,false,0 as false
  104 template <>
  105 bool string2type<bool>(const std::string& s);
  106 
  107 template <typename T, typename R = T>
  108 std::vector<T> strings2types(const std::vector<std::string>& values)
  109 {
  110     std::vector<T> t;
  111     t.reserve(values.size());
  112     for (const std::string& v : values)
  113         t.push_back(string2type<T, R>(v));
  114     return t;
  115 }
  116 
  117 } // namespace MetNoFimex
  118 
  119 #endif // FIMEX_STRING2TYPE_H_