// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/util/convert.h 2056 2019-07-31 10:58:28Z depa $

#pragma once

#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include <Eigen/Core>

namespace shins{
  namespace util{
    class BadConversion : public std::runtime_error
    {
    public:
      BadConversion(const std::string& _s)
        : std::runtime_error(_s)
      { }
    };
    class NotFound : public std::runtime_error
    {
    public:
      NotFound(const std::string& _s)
        : std::runtime_error(_s)
      { }
    };

    template <typename T>
    T convertStringToType(const std::string& _s)
    {
      std::istringstream i(_s);
      T x;
      if (!(i >> x))
        throw BadConversion("convertStringToType(\"" + _s + "\")");
      return x;
    };

    template <typename T>
    std::string convertTypeToString(const T& _t)
    {
      std::ostringstream o;
      // std::string s;

      if (!(o << _t))
        throw BadConversion("convertToString");
      return o.str();
    }

    template <typename Derived>
    Nice::SeqFloat convert_to_seqfloat(const Eigen::MatrixBase<Derived>& _emat)
    {
      Nice::SeqFloat outSeq;
      for (int i=0; i<_emat.cols(); i++)
        {
          for (int j=0; j<_emat.rows(); j++)
            outSeq.push_back(_emat(j,i));
        }
      return outSeq;
    }

  }
}
