<commit_before>/*
 * Copyright (C) 2009-2011, <NAME>.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#ifndef CXX_CONVERSIONS_HXX
# define CXX_CONVERSIONS_HXX

# include <boost/optional.hpp>

# include <libport/cassert>
# include <libport/format.hh>
# include <libport/path.hh>
# include <libport/symbol.hh>

# include <object/urbi-exception.hh>

# include <urbi/object/cxx-object.hh>
# include <urbi/object/float.hh>
# include <urbi/object/list.hh>
# include <urbi/object/path.hh>
# include <urbi/object/string.hh>
# include <urbi/runner/raise.hh>

namespace urbi
{
  namespace object
  {
    /*--------.
    | Helpers |
    `--------*/

    /// Convert an rFloat to an integral value.
    template <typename T>
    T
    to_integer(const rObject& o)
    {
      libport::ufloat value = o->as<Float>()->value_get();
      try
      {
        return libport::numeric_cast<T>(value);
      }
      catch (libport::bad_numeric_cast& e)
      {
        std::string format = e.what();
        format += ": %s";
        runner::raise_bad_integer_error(value, format);
      }
    }


    /*----------.
    | Objects.  |
    `----------*/

    template <typename T>
    typename CxxConvert<T>::target_type
    CxxConvert<T>::to(rObject o)
    {
      type_check<T>(o);
      return *o->as<T>();
    }

    template <typename T>
    rObject
    CxxConvert<T>::from(source_type v)
    {
      return &v;
    }

    template <>
    struct CxxConvert<libport::intrusive_ptr<Object> >
    {
      typedef libport::intrusive_ptr<Object> target_type;

      static rObject
      to(const rObject& o)
      {
        return o;
      }

      static rObject
      from(rObject o)
      {
        if (!o)
          return void_class;
        return o;
      }
    };


    /*-------------.
    | Urbi types.  |
    `-------------*/

    template <typename Urbi>
    struct CxxConvert<libport::intrusive_ptr<Urbi> >
    {
      typedef libport::intrusive_ptr<Urbi> target_type;
      static target_type
      to(const rObject& o)
      {
        type_check<Urbi>(o);
        return o->as<Urbi>();
      }

      static rObject
      from(const target_type& v)
      {
        return v;
      }
    };


    /*----------------------.
    | Urbi types pointers.  |
    `----------------------*/

    template <typename Urbi>
    struct CxxConvert<Urbi*>
    {
      typedef Urbi* target_type;
      static target_type
      to(const rObject& o)
      {
        return type_check<Urbi>(o);
      }

      static rObject
      from(target_type v)
      {
        return v;
      }
    };

    /*------.
    | int.  |
    `------*/

    template<>
    struct CxxConvert<int><commit_msg>Add conversion for RefCounted.<commit_after>/*
 * Copyright (C) 2009-2011, <NAME>.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#ifndef CXX_CONVERSIONS_HXX
# define CXX_CONVERSIONS_HXX

# include <boost/optional.hpp>

# include <libport/cassert>
# include <libport/format.hh>
# include <libport/path.hh>
# include <libport/symbol.hh>

# include <object/urbi-exception.hh>

# include <urbi/object/cxx-object.hh>
# include <urbi/object/float.hh>
# include <urbi/object/list.hh>
# include <urbi/object/path.hh>
# include <urbi/object/string.hh>
# include <urbi/runner/raise.hh>

namespace urbi
{
  namespace object
  {
    /*--------.
    | Helpers |
    `--------*/

    /// Convert an rFloat to an integral value.
    template <typename T>
    T
    to_integer(const rObject& o)
    {
      libport::ufloat value = o->as<Float>()->value_get();
      try
      {
        return libport::numeric_cast<T>(value);
      }
      catch (libport::bad_numeric_cast& e)
      {
        std::string format = e.what();
        format += ": %s";
        runner::raise_bad_integer_error(value, format);
      }
    }


    /*----------.
    | Objects.  |
    `----------*/

    template <typename T>
    typename CxxConvert<T>::target_type
    CxxConvert<T>::to(rObject o)
    {
      type_check<T>(o);
      return *o->as<T>();
    }

    template <typename T>
    rObject
    CxxConvert<T>::from(source_type v)
    {
      return &v;
    }

    template <>
    struct CxxConvert<libport::intrusive_ptr<Object> >
    {
      typedef libport::intrusive_ptr<Object> target_type;

      static rObject
      to(const rObject& o)
      {
        return o;
      }

      static rObject
      from(rObject o)
      {
        if (!o)
          return void_class;
        return o;
      }
    };


    /*-------------.
    | Urbi types.  |
    `-------------*/

    template <typename Urbi>
    struct CxxConvert<libport::intrusive_ptr<Urbi> >
    {
      typedef libport::intrusive_ptr<Urbi> target_type;
      static target_type
      to(const rObject& o)
      {
        type_check<Urbi>(o);
        return o->as<Urbi>();
      }

      static rObject
      from(const target_type& v)
      {
        return v;
      }
    };

    /*-----------.
    | RefCounted |
    `-----------*/

    template <>
    struct CxxConvert<const libport::RefCounted*>
    {
      typedef libport::RefCounted* target_type;
      static target_type
      to(Object* o)
      {
        return o;
      }
    };

    /*----------------------.
    | Urbi types pointers.  |
    `----------------------*/

    template <typename Urbi>
    struct CxxConvert<Urbi*>
    {
      typedef Urbi* target_type;
      static target_type
      to(const rObject& o)
      {
        return type_check<Urbi>(o);
      }

      static rObject
      from(target_type v)
      {
        return v;
      }
    };

    /*------.
    | int.  |
    `------*/

    template<>
    struct CxxConvert<int>
{'fraction_non_alphanumeric': 0.16125811283075386, 'fraction_numerical': 0.0026626726576801465, 'mean_word_length': 1.8136704119850187, 'pattern_counts': {'":': 2, '<': 81, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '961708', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1944, 'n_words': 414}