#ifndef INCLUDED_BOBCAT_A2X_
#define INCLUDED_BOBCAT_A2X_

#include <sstream>
#include <string>

namespace FBB
{

class A2x: public std::istringstream
{
    static bool thread_local s_lastFail;

    public:
        A2x() = default;
        explicit A2x(char const *txt);          // 1.f initialize from text
        explicit A2x(std::string const &str);   // 2.f
        A2x(A2x const &other);                  // 3.f
        A2x(A2x &&tmp);                         // 4.f

        template <typename Type>
        operator Type();                        // .f

        template <typename Type>
        Type to();                              // .f

        A2x &operator=(char const *txt);        // 1.cc

        A2x &operator=(std::string const &str); // 1.f
        A2x &operator=(A2x const &other);       // 2.f

        static bool lastFail();                 // .f
};

inline A2x::A2x(char const *txt)                    // initialize from text
:
    std::istringstream(txt)
{}
inline A2x::A2x(std::string const &str)
:
    std::istringstream(str.c_str())
{}
inline A2x::A2x(A2x const &other)
:
    std::istringstream(other.str())
{}
inline A2x::A2x(A2x &&tmp)
:
    std::istringstream(std::move(tmp))
{}
template <typename Type>
inline Type A2x::to()
{
    Type t;

    return (s_lastFail = (*this >> t).fail()) ? Type() : t;
}
template <typename Type>
inline A2x::operator Type()
{
    return to<Type>();
}
inline A2x &A2x::operator=(std::string const &str)
{
    return operator=(str.c_str());
}
inline A2x &A2x::operator=(A2x const &other)
{
    return operator=(other.str());
}
inline bool A2x::lastFail()
{
    return s_lastFail;
}

} // FBB

#endif
