<commit_before>#include <boost/bind.hpp>

#include <object/any-to-boost-function.hh>
#include <object/cxx-conversions.hh>
#include <object/cxx-helper.hh>
#include <object/cxx-primitive.hh>

namespace object
{
  template <typename M>
  struct MakePrimitive
  {};

  namespace
  {
    inline void
    check_args_count(unsigned formal, unsigned effective,
                    const libport::Symbol& name)
    {
      if (formal != effective)
        throw WrongArgumentCount(formal, effective, name);
    }
  }

# define BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3) \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   S                                            \
   COMMA(Run)  WHEN(Run, runner::Runner&)       \
   COMMA(Arg1) WHEN(Arg1, A1)                   \
   COMMA(Arg2) WHEN(Arg2, A2)                   \
   COMMA(Arg3) WHEN(Arg3, A3)                   \
   >                                            \

# define PRIMITIVE(Ret, ArgsC, Run, Arg1, Arg2, Arg3)                   \
  template                                                              \
  <WHEN(Ret, typename R) COMMA(Ret)                                     \
   typename S                                                           \
   COMMA(Arg1) WHEN(Arg1, typename A1)                                  \
    COMMA(Arg2) WHEN(Arg2, typename A2)                                 \
    COMMA(Arg3) WHEN(Arg3, typename A3)                                 \
    >                                                                   \
  struct MakePrimitive<BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3)>   \
  {                                                                     \
    static rObject primitive(                                           \
      runner::Runner& WHEN(Run, r),                                     \
      object::objects_type args,                                        \
      BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3) f,                  \
      const libport::Symbol& name)                                      \
    {                                                                   \
      check_args_count(ArgsC WHEN(Run, - 1), args.size(), name);        \
      WHEN(Ret, return) f(                                              \
        CxxConvert<S>::to(args[0], name)                                \
        COMMA(Run) WHEN(Run, r)                                         \
        COMMA(Arg1) WHEN(Arg1, CxxConvert<A1>::to(args[1], name))       \
        COMMA(Arg2) WHEN(Arg2, CxxConvert<A2>::to(args[2], name))       \
        COMMA(Arg3) WHEN(Arg3, CxxConvert<A3>::to(args[3], name))       \
        );                                                              \
      return object::void_class;                                        \
                                                                        \
    }                                                                   \
  };                                                                    \

# define BOOST_LIST_TYPE_MET(Ret, ArgsC, Run)   \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   S,                                           \
   WHEN(Run, runner::Runner&) COMMA(Run)        \
   object::objects_type                         \
   >                                            \

# define BOOST_LIST_TYPE(Ret, ArgsC, Run)       \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   WHEN(Run, runner::Runner&) COMMA(Run)        \
   object::objects_type                         \
   >                                            \

# define PRIMITIVE_LIST_MET(Ret, ArgsC, Run)                    \
  template <WHEN(Ret, typename R) COMMA(Ret) typename S>        \
  struct MakePrimitive<BOOST_LIST_TYPE_MET(Ret, ArgsC, Run)>    \
  {                                                             \
    static rObject primitive(                                   \
      runner::Runner& WHEN(Run, r),                             \
      object::objects_type args,                                \
      BOOST_LIST_TYPE_MET(Ret, ArgsC, Run) f,                   \
      const libport::Symbol& name)                              \
    {                                                           \
      S tgt =                                                   \
        CxxConvert<S>::to(args[0], name);                       \
      args.pop_front();                                         \
      WHEN(Ret, return) f(                                      \
        tgt,                                                    \
        WHEN(Run, r) COMMA(Run)                                 \
        args                                                    \
        );                                                      \
      return object::void_class;                                \
                                                                \
    }                                                           \
  };                                                            \

# define PRIMITIVE_LIST(Ret, ArgsC, Run)                        \
  template <WHEN(Ret, typename R)>                              \
  struct MakePrimitive<BOOST_LIST_TYPE(Ret, ArgsC, Run)>        \
  {                                                             \
    static rObject primitive(                                   \
      runner::Runner& WHEN(Run, r),                             \
      object::objects_type args,                                \
      BOOST_LIST_TYPE(Ret, ArgsC, Run) f,                       \
      const libport::Symbol&)                                   \
    {                                                           \
      WHEN(Ret, return) f(                                      \
        WHEN(Run, r) COMMA(Run)                                 \
        args                                                    \
        );                                                      \
      return object::void_class;                                \
                                                                \
    }                                                           \
  };                                                            \

  PRIMITIVE_LIST_MET(true, 2, false);
  PRIMITIVE_LIST_MET(true, 3, true);
  PRIMITIVE_LIST_MET(false, 2, false);
  PRIMITIVE_LIST_MET(false, 3, true);
  PRIMITIVE_LIST(true, 1, false);
  PRIMITIVE_LIST(true, 2, true);
  PRIMITIVE_LIST(false, 1, false);
  PRIMITIVE_LIST(false, 2, true);
  ALL_PRIMITIVE(PRIMITIVE);

# undef PRIMITIVE
# undef BOOST_TYPE
# undef PRIMITIVE_LIST
# undef BOOST_LIST_TYPE

  template<typename M>
  inline rPrimitive
  make_primitive(M f, const libport::Symbol& name)
  {
    typedef AnyToBoostFunction<M> C;
    // If primitive is unfound in MakePrimitive here, you gave an
    // unsupported type to make Primitive. AnyToBoostFunction must be
    // able to convert the given values. It handles:
    // * boost::functions
    // * function pointers
    // * method pointers
    return new Primitive(
      boost::bind(MakePrimitive<typename C::type>::primitive,
                  _1, _2, C::convert(f), name));
  }
}
<commit_msg>Improve CxxConversion usage.<commit_after>#include <boost/bind.hpp>
 #include <boost/tr1/type_traits.hpp>

#include <object/any-to-boost-function.hh>
#include <object/cxx-conversions.hh>
#include <object/cxx-helper.hh>
#include <object/cxx-primitive.hh>

namespace object
{
  template <typename M>
  struct MakePrimitive
  {};

  namespace
  {
    inline void
    check_args_count(unsigned formal, unsigned effective,
                    const libport::Symbol& name)
    {
      if (formal != effective)
        throw WrongArgumentCount(formal, effective, name);
    }

    // Remove const and reference
    template <typename T>
    struct Flatten
    {
      typedef typename boost::remove_const
      <typename boost::remove_reference<T>::type>::type type;
    };
  }

# define BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3) \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   S                                            \
   COMMA(Run)  WHEN(Run, runner::Runner&)       \
   COMMA(Arg1) WHEN(Arg1, A1)                   \
   COMMA(Arg2) WHEN(Arg2, A2)                   \
   COMMA(Arg3) WHEN(Arg3, A3)                   \
   >                                            \

# define PRIMITIVE(Ret, ArgsC, Run, Arg1, Arg2, Arg3)                   \
  template                                                              \
  <WHEN(Ret, typename R) COMMA(Ret)                                     \
   typename S                                                           \
   COMMA(Arg1) WHEN(Arg1, typename A1)                                  \
    COMMA(Arg2) WHEN(Arg2, typename A2)                                 \
    COMMA(Arg3) WHEN(Arg3, typename A3)                                 \
    >                                                                   \
  struct MakePrimitive<BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3)>   \
  {                                                                     \
    static rObject primitive(                                           \
      runner::Runner& WHEN(Run, r),                                     \
      object::objects_type args,                                        \
      BOOST_TYPE(Ret, ArgsC, Run, Arg1, Arg2, Arg3) f,                  \
      const libport::Symbol& name)                                      \
    {                                                                   \
      check_args_count(ArgsC WHEN(Run, - 1), args.size(), name);        \
      WHEN(Ret, R res =) f(                                             \
        CxxConvert<S>::to(args[0], name)                                \
        COMMA(Run) WHEN(Run, r)                                         \
        COMMA(Arg1)                                                     \
        WHEN(Arg1,                                                      \
             CxxConvert<typename Flatten<A1>::type>::to(args[1], name)) \
        COMMA(Arg2)                                                     \
        WHEN(Arg2,                                                      \
             CxxConvert<typename Flatten<A2>::type>::to(args[2], name)) \
        COMMA(Arg3)                                                     \
        WHEN(Arg3,                                                      \
             CxxConvert<typename Flatten<A3>::type>::to(args[3], name)) \
        );                                                              \
      IF(Ret,                                                           \
         return CxxConvert<R>::from(res, name),                         \
         return object::void_class);                                    \
    }                                                                   \
  };                                                                    \

# define BOOST_LIST_TYPE_MET(Ret, ArgsC, Run)   \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   S,                                           \
   WHEN(Run, runner::Runner&) COMMA(Run)        \
   object::objects_type                         \
   >                                            \

# define BOOST_LIST_TYPE(Ret, ArgsC, Run)       \
  boost::function##ArgsC                        \
  <IF(Ret, R, void),                            \
   WHEN(Run, runner::Runner&) COMMA(Run)        \
   object::objects_type                         \
   >                                            \

# define PRIMITIVE_LIST_MET(Ret, ArgsC, Run)                    \
  template <WHEN(Ret, typename R) COMMA(Ret) typename S>        \
  struct MakePrimitive<BOOST_LIST_TYPE_MET(Ret, ArgsC, Run)>    \
  {                                                             \
    static rObject primitive(                                   \
      runner::Runner& WHEN(Run, r),                             \
      object::objects_type args,                                \
      BOOST_LIST_TYPE_MET(Ret, ArgsC, Run) f,                   \
      const libport::Symbol& name)                              \
    {                                                           \
      S tgt =                                                   \
        CxxConvert<S>::to(args[0], name);                       \
      args.pop_front();                                         \
      WHEN(Ret, return) f(                                      \
        tgt,                                                    \
        WHEN(Run, r) COMMA(Run)                                 \
        args                                                    \
        );                                                      \
      return void_class;                                        \
    }                                                           \
  };                                                            \

# define PRIMITIVE_LIST(Ret, ArgsC, Run)                        \
  template <WHEN(Ret, typename R)>                              \
  struct MakePrimitive<BOOST_LIST_TYPE(Ret, ArgsC, Run)>        \
  {                                                             \
    static rObject primitive(                                   \
      runner::Runner& WHEN(Run, r),                             \
      object::objects_type args,                                \
      BOOST_LIST_TYPE(Ret, ArgsC, Run) f,                       \
      const libport::Symbol&)                                   \
    {                                                           \
      WHEN(Ret, return) f(                                      \
        WHEN(Run, r) COMMA(Run)                                 \
        args                                                    \
        );                                                      \
      return object::void_class;                                \
                                                                \
    }                                                           \
  };                                                            \

  PRIMITIVE_LIST_MET(true, 2, false);
  PRIMITIVE_LIST_MET(true, 3, true);
  PRIMITIVE_LIST_MET(false, 2, false);
  PRIMITIVE_LIST_MET(false, 3, true);
  PRIMITIVE_LIST(true, 1, false);
  PRIMITIVE_LIST(true, 2, true);
  PRIMITIVE_LIST(false, 1, false);
  PRIMITIVE_LIST(false, 2, true);
  ALL_PRIMITIVE(PRIMITIVE);

# undef PRIMITIVE
# undef BOOST_TYPE
# undef PRIMITIVE_LIST
# undef BOOST_LIST_TYPE

  template<typename M>
  inline rPrimitive
  make_primitive(M f, const libport::Symbol& name)
  {
    typedef AnyToBoostFunction<M> C;
    // If primitive is unfound in MakePrimitive here, you gave an
    // unsupported type to make Primitive. AnyToBoostFunction must be
    // able to convert the given values. It handles:
    // * boost::functions
    // * function pointers
    // * method pointers
    return new Primitive(
      boost::bind(MakePrimitive<typename C::type>::primitive,
                  _1, _2, C::convert(f), name));
  }
}

{'fraction_non_alphanumeric': 0.08470141932108051, 'fraction_numerical': 0.007260121656092616, 'mean_word_length': 0.7173986296753904, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 180, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4598834', 'n_tokens_mistral': 3547, 'n_tokens_neox': 3424, 'n_words': 679}