// file      : xsd/cxx/tree/traits.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_TREE_TRAITS_HXX
#define XSD_CXX_TREE_TRAITS_HXX

#include <sstream>

#include <xsd/cxx/tree/elements.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      // bool
      //
      template <>
      struct traits<bool>
      {
        typedef bool type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e);

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a);

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&);
      };


      // 8 bit
      //
      template <>
      struct traits<signed char>
      {
        typedef signed char type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> s (e.value ());

          short t;
          s >> t;

          return static_cast<type> (t);
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> s (a.value ());

          short t;
          s >> t;

          return static_cast<type> (t);
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          short t;
          is >> t;

          return static_cast<type> (t);
        }
      };

      template <>
      struct traits<unsigned char>
      {
        typedef unsigned char type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          unsigned short t;
          is >> t;

          return static_cast<type> (t);
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          unsigned short t;
          is >> t;

          return static_cast<type> (t);
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          unsigned short t;
          is >> t;

          return static_cast<type> (t);
        }
      };

      // 16 bit
      //
      template <>
      struct traits<short>
      {
        typedef short type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      template <>
      struct traits<unsigned short>
      {
        typedef unsigned short type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      // 32 bit
      //
      template <>
      struct traits<int>
      {
        typedef int type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      template <>
      struct traits<unsigned int>
      {
        typedef unsigned int type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      // 64 bit
      //
      template <>
      struct traits<long long>
      {
        typedef long long type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      template <>
      struct traits<unsigned long long>
      {
        typedef unsigned long long type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      // floating point
      //
      template <>
      struct traits<float>
      {
        typedef float type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      template <>
      struct traits<double>
      {
        typedef double type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };

      template <>
      struct traits<long double>
      {
        typedef long double type;

        static void
        set_container (type&, tree::type*)
        {
        }

        template <typename C>
        static type
        create (xml::dom::element<C> const& e)
        {
          std::basic_istringstream<C> is (e.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (xml::dom::attribute<C> const& a)
        {
          std::basic_istringstream<C> is (a.value ());

          type t;
          is >> t;

          return t;
        }

        template <typename C>
        static type
        create (std::basic_string<C> const& s,
                xml::dom::element<C> const&)
        {
          std::basic_istringstream<C> is (s);

          type t;
          is >> t;

          return t;
        }
      };
    }
  }
}

#endif  // XSD_CXX_TREE_TRAITS_HXX

#include <xsd/cxx/tree/traits.ixx>
