LCOV - code coverage report
Current view: top level - app/include/utils - stringify.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 15 46.7 %
Date: 2023-08-17 16:45:52 Functions: 2 3 66.7 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <list>
       4             : #include <sstream>
       5             : #include <string>
       6             : #include <vector>
       7             : 
       8             : namespace utils::stringify {
       9             : template<class T>
      10             : class stringify {
      11             :    public:
      12             :     static std::string toString(const T &t);
      13             :     static T           fromString(const std::string &s);
      14             : };
      15             : 
      16             : template<>
      17             : class stringify<int> {
      18             :    public:
      19             :     static std::string toString(const int &t);
      20             :     static int         fromString(const std::string &s);
      21             : };
      22             : 
      23             : template<>
      24             : class stringify<long> {
      25             :    public:
      26             :     static std::string toString(const long &t);
      27             :     static long        fromString(const std::string &s);
      28             : };
      29             : 
      30             : template<>
      31             : class stringify<unsigned long> {
      32             :    public:
      33             :     static std::string   toString(const unsigned long &t);
      34             :     static unsigned long fromString(const std::string &s);
      35             : };
      36             : 
      37             : template<>
      38             : class stringify<bool> {
      39             :    public:
      40             :     static std::string toString(const bool &t);
      41             :     static bool        fromString(const std::string &s);
      42             : };
      43             : 
      44             : template<>
      45             : class stringify<float> {
      46             :    public:
      47             :     static unsigned PRECISION;
      48             : 
      49             :     static std::string toString(const float &t);
      50             :     static float       fromString(const std::string &s);
      51             : };
      52             : 
      53             : template<>
      54             : class stringify<double> {
      55             :    public:
      56             :     static unsigned PRECISION;
      57             : 
      58             :     static std::string toString(const double &t);
      59             :     static double      fromString(const std::string &s);
      60             : };
      61             : 
      62             : template<>
      63             : class stringify<std::string> {
      64             :    public:
      65             :     static std::string toString(const std::string &t);
      66             :     static std::string fromString(const std::string &s);
      67             : };
      68             : 
      69             : template<class T>
      70             : class stringify<std::vector<T>> {
      71             :    public:
      72           0 :     static std::string toString(const std::vector<T> &t) {
      73           0 :         std::stringstream ss;
      74             : 
      75           0 :         bool first = true;
      76           0 :         for(const T &el: t) {
      77           0 :             if(first)
      78             :                 first = false;
      79             :             else
      80           0 :                 ss << " ";
      81             : 
      82           0 :             ss << stringify<T>::toString(el);
      83             :         }
      84             : 
      85           0 :         return ss.str();
      86             :     }
      87       81452 :     static std::vector<T> fromString(const std::string &s) {
      88       81452 :         std::vector<T> ret;
      89             : 
      90       81452 :         std::stringstream ss(s);
      91       81452 :         std::string       elStr;
      92      398864 :         while(ss >> elStr) {
      93      367189 :             ret.emplace_back(stringify<T>::fromString(elStr));
      94             :         }
      95             : 
      96       81452 :         return ret;
      97             :     }
      98             : };
      99             : 
     100             : template<class T>
     101             : class stringify<std::list<T>> {
     102             :    public:
     103             :     static std::string toString(const std::list<T> &t) {
     104             :         std::stringstream ss;
     105             : 
     106             :         bool first = true;
     107             :         for(const T &el: t) {
     108             :             if(first)
     109             :                 first = false;
     110             :             else
     111             :                 ss << " ";
     112             : 
     113             :             ss << stringify<T>::toString(el);
     114             :         }
     115             : 
     116             :         return ss.str();
     117             :     }
     118             :     static std::list<T> fromString(const std::string &s) {
     119             :         std::list<T> ret;
     120             : 
     121             :         std::stringstream ss(s);
     122             :         std::string       elStr;
     123             :         while(ss >> elStr) {
     124             :             ret.emplace_back(stringify<T>::fromString(elStr));
     125             :         }
     126             : 
     127             :         return ret;
     128             :     }
     129             : };
     130             : 
     131             : template<>
     132             : class stringify<std::vector<bool>> {
     133             :    public:
     134             :     static std::string       toString(const std::vector<bool> &t);
     135             :     static std::vector<bool> fromString(const std::string &s);
     136             : };
     137             : 
     138             : }  // namespace utils::stringify

Generated by: LCOV version 1.14