[go: nahoru, domu]

Skip to content

Commit

Permalink
Lots of goodies
Browse files Browse the repository at this point in the history
  • Loading branch information
felipealmeida committed Oct 10, 2012
1 parent 9493b90 commit cd847fe
Show file tree
Hide file tree
Showing 29 changed files with 805 additions and 143 deletions.
2 changes: 1 addition & 1 deletion include/javabind/advanced/get_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace javabind {
template <typename T>
T& get_peer(object obj, const char* class_name)
{
javabind::class_ class_ = env(obj.env).find_class(class_name);
javabind::class_ class_ = env(obj.environment()).find_class(class_name);
field<long_> peer_field = class_.find_field<long_>("peer");
long_ peer = peer_field.get(obj);
assert(peer != 0);
Expand Down
46 changes: 43 additions & 3 deletions include/javabind/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define JAVABIND_ARRAY_HPP

#include <javabind/primitives.hpp>
#include <javabind/object.hpp>

#include <cassert>
#include <stdexcept>
Expand Down Expand Up @@ -43,12 +44,16 @@ struct array_region
std::size_t size;
};

template <typename T>
struct array;

namespace array_detail {

template <typename JavaArray, typename JavaT, typename CxxT>
struct array_impl
{
typedef JavaArray java_type;
typedef array_impl<JavaArray, JavaT, CxxT> self_type;

array_impl() : ar(0), env(0) {}
array_impl(JavaArray ar, JNIEnv* env)
Expand All @@ -61,17 +66,40 @@ struct array_impl
JavaT* raw = env->GetCharArrayElements(ar, 0);
if(!raw)
throw std::runtime_error("A exception was thrown");
return region_type(raw, ar, env, env->GetArrayLength(ar));
return region_type(raw, ar, env, length());
}

JavaArray raw() const { return ar; }
std::size_t length() const
{
return env->GetArrayLength(ar);
}

typedef bool(self_type::*test_type)() const;
operator test_type() const
{
return test()? &self_type::test : test_type(0);
}
private:
bool test() const { return ar != 0; }

JavaArray ar;
JNIEnv* env;
};

}

template <typename T>
struct array;
template <>
struct array<byte> : array_detail::array_impl<jbyteArray, jbyte, byte>
{
typedef array_detail::array_impl<jbyteArray, jbyte, byte> base_type;

array() {}
array(jbyteArray ar, JNIEnv* env)
: base_type(ar, env)
{
}
};

template <>
struct array<char_> : array_detail::array_impl<jcharArray, jchar, char_>
Expand All @@ -85,6 +113,18 @@ struct array<char_> : array_detail::array_impl<jcharArray, jchar, char_>
}
};

template <>
struct array<object> : array_detail::array_impl<jobjectArray, jobject, object>
{
typedef array_detail::array_impl<jobjectArray, jobject, object> base_type;

array() {}
array(jobjectArray ar, JNIEnv* env)
: base_type(ar, env)
{
}
};

}

#endif
70 changes: 64 additions & 6 deletions include/javabind/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#define JAVABIND_CLASS_HPP

#include <javabind/method.hpp>
#include <javabind/static_method.hpp>
#include <javabind/descriptors.hpp>
#include <javabind/field_descriptor_traits.hpp>
#include <javabind/detail/get_static_field.hpp>
#include <javabind/detail/create_primitive_type_descriptor.hpp>
#include <javabind/detail/split_descriptors.hpp>

#include <boost/function_types/result_type.hpp>
#include <boost/function_types/function_arity.hpp>
Expand Down Expand Up @@ -55,6 +57,61 @@ struct class_

template <typename F, typename S>
javabind::method<F> method(const char* name, S s) const
{
typedef typename boost::function_types::parameter_types<F>::type
parameter_types;

typedef typename boost::function_types::result_type<F>::type result_type;
typedef boost::mpl::single_view<result_type> result_type_sequence;
typedef detail::create_primitive_type_descriptor
<typename boost::mpl::begin<result_type_sequence>::type
, typename boost::mpl::end<result_type_sequence>::type> result_type_descriptor;
typedef detail::split_descriptors<result_type_descriptor> split_descriptors_type;
typename split_descriptors_type::template my_result<S>::type
sequences = split_descriptors_type::split(s);

typedef detail::create_primitive_type_descriptor
<typename boost::mpl::begin<parameter_types>::type
, typename boost::mpl::end<parameter_types>::type> create_descriptor;

std::size_t parameters_length = create_descriptor::length
(boost::fusion::begin(sequences.second), boost::fusion::end(sequences.second));
std::cout << "parameters_length: " << parameters_length << std::endl;

std::vector<char> type
(parameters_length + result_type_descriptor::length
(boost::fusion::begin(sequences.first), boost::fusion::end(sequences.first))
+3);
type[0] = '(';
type[parameters_length+1] = ')';
type[type.size()-1] = 0;
create_descriptor::run(&type[1], boost::fusion::begin(sequences.second)
, boost::fusion::end(sequences.second));
result_type_descriptor::run(&type[parameters_length+2]
, boost::fusion::begin(sequences.first)
, boost::fusion::end(sequences.first));
assert(type[type.size()-1] == 0);
std::cout << "S Using as type: " << &type[0] << std::endl;
jmethodID id = env->GetMethodID(cls, name, &type[0]);
if(id == 0)
throw std::runtime_error("Couldn't find method");
return javabind::method<F>(id);
}

template <typename F>
javabind::method<F> method(const char* name, const char* d) const
{
return method<F>(name, javabind::descriptors(d));
}

template <typename F>
javabind::static_method<F> static_method(const char* name) const
{
return this->template static_method<F>(name, boost::fusion::vector0<>());
}

template <typename F, typename S>
javabind::static_method<F> static_method(const char* name, S s) const
{
typedef typename boost::function_types::parameter_types<F>::type
parameter_types;
Expand All @@ -72,17 +129,16 @@ struct class_
create_descriptor::run(&type[1], boost::fusion::begin(s), boost::fusion::end(s));
type[type.size()-1] = 0;
std::cout << "S Using as type: " << &type[0] << std::endl;
jmethodID id = env->GetMethodID(cls, name, &type[0]);
jmethodID id = env->GetStaticMethodID(cls, name, &type[0]);
if(id == 0)
throw std::runtime_error("Couldn't find method");
return javabind::method<F>
(id, env);
return javabind::static_method<F>(id, env);
}

template <typename F>
javabind::method<F> method(const char* name, const char* d) const
javabind::static_method<F> static_method(const char* name, const char* d) const
{
return method<F>(name, javabind::descriptors(d));
return static_method<F>(name, javabind::descriptors(d));
}

template <typename F>
Expand Down Expand Up @@ -112,7 +168,7 @@ struct class_
jmethodID id = env->GetMethodID(cls, "<init>", &type[0]);
if(id == 0)
throw std::runtime_error("Couldn't find method");
return javabind::constructor<F>(id, env);
return javabind::constructor<F>(id);
}

template <typename F>
Expand Down Expand Up @@ -158,7 +214,9 @@ struct class_
}

::jclass raw() const { return cls; }
JNIEnv* environment() const { return env; }

private:
::jclass cls;
JNIEnv* env;
};
Expand Down
4 changes: 2 additions & 2 deletions include/javabind/constructor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct constructor : detail::overload_set
, typename boost::function_types::result_type<F>::type
, detail::new_object_functor<typename boost::function_types::result_type<F>::type> const
> base_type;
constructor( ::jmethodID id, JNIEnv* env)
: base_type(functor_type(id, env)) {}
constructor( ::jmethodID id)
: base_type(functor_type(id)) {}
};

}
Expand Down
57 changes: 57 additions & 0 deletions include/javabind/detail/call_array_byte_method_functor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) Copyright 2012 Felipe Magno de Almeida
//
// All rights reserved

#if !defined(BOOST_PP_IS_ITERATING)

#ifndef JAVABIND_DETAIL_CALL_ARRAY_BYTE_METHOD_FUNCTOR_HPP
#define JAVABIND_DETAIL_CALL_ARRAY_BYTE_METHOD_FUNCTOR_HPP

#include <javabind/primitives.hpp>
#include <javabind/array.hpp>
#include <javabind/detail/max_args.hpp>
#include <javabind/detail/unwrap.hpp>

#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>

#include <stdexcept>

namespace javabind { namespace detail {

struct call_array_byte_method_functor
{
call_array_byte_method_functor(jmethodID id)
: id(id) {}
typedef javabind::array<javabind::byte> result_type;

#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_PP_DEC (JAVABIND_MAX_ARGS), "javabind/detail/call_array_byte_method_functor.hpp"))
#include BOOST_PP_ITERATE ()

jmethodID raw() const { return id; }

jmethodID id;
};

} }

#endif
#else

template <typename O BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), typename A)>
result_type operator()(O o BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), A, a)) const
{
jbyteArray r
= static_cast<jbyteArray>(static_cast<void*>
(o.environment()->CallObjectMethod(o.raw(), id
BOOST_PP_REPEAT(BOOST_PP_ITERATION()
, JAVABIND_TRAILING_UNWRAP, a))));
if(o.environment()->ExceptionCheck())
{
throw std::runtime_error("Exception was thrown");
}
return result_type(r, o.environment());
}

#endif
57 changes: 57 additions & 0 deletions include/javabind/detail/call_array_object_method_functor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) Copyright 2012 Felipe Magno de Almeida
//
// All rights reserved

#if !defined(BOOST_PP_IS_ITERATING)

#ifndef JAVABIND_DETAIL_CALL_ARRAY_OBJECT_METHOD_FUNCTOR_HPP
#define JAVABIND_DETAIL_CALL_ARRAY_OBJECT_METHOD_FUNCTOR_HPP

#include <javabind/primitives.hpp>
#include <javabind/array.hpp>
#include <javabind/detail/max_args.hpp>
#include <javabind/detail/unwrap.hpp>

#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>

#include <stdexcept>

namespace javabind { namespace detail {

struct call_array_object_method_functor
{
call_array_object_method_functor(jmethodID id)
: id(id) {}
typedef javabind::array<javabind::object> result_type;

#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_PP_DEC (JAVABIND_MAX_ARGS), "javabind/detail/call_array_object_method_functor.hpp"))
#include BOOST_PP_ITERATE ()

jmethodID raw() const { return id; }

jmethodID id;
};

} }

#endif
#else

template <typename O BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), typename A)>
result_type operator()(O o BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), A, a)) const
{
jobjectArray r
= static_cast<jobjectArray>(static_cast<void*>
(o.environment()->CallObjectMethod(o.raw(), id
BOOST_PP_REPEAT(BOOST_PP_ITERATION()
, JAVABIND_TRAILING_UNWRAP, a))));
if(o.environment()->ExceptionCheck())
{
throw std::runtime_error("Exception was thrown");
}
return result_type(r, o.environment());
}

#endif
14 changes: 7 additions & 7 deletions include/javabind/detail/call_boolean_method_functor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace javabind { namespace detail {

struct call_boolean_method_functor
{
call_boolean_method_functor(jmethodID id, JNIEnv* env)
: id(id), env(env) {}
call_boolean_method_functor(jmethodID id)
: id(id) {}
typedef bool result_type;

#define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_PP_DEC (JAVABIND_MAX_ARGS), "javabind/detail/call_boolean_method_functor.hpp"))
Expand All @@ -30,7 +30,6 @@ struct call_boolean_method_functor
jmethodID raw() const { return id; }

jmethodID id;
JNIEnv* env;
};

} }
Expand All @@ -39,12 +38,13 @@ struct call_boolean_method_functor
#else

template <typename O BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_ITERATION(), typename A)>
bool operator()(O o BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), A, a)) const
result_type operator()(O o BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(BOOST_PP_ITERATION(), A, a)) const
{
jboolean r
= env->CallBooleanMethod(o.raw(), id
BOOST_PP_REPEAT(BOOST_PP_ITERATION(), JAVABIND_TRAILING_UNWRAP, a)) != 0;
if(env->ExceptionCheck())
= o.environment()->CallBooleanMethod(o.raw(), id
BOOST_PP_REPEAT(BOOST_PP_ITERATION()
, JAVABIND_TRAILING_UNWRAP, a)) != 0;
if(o.environment()->ExceptionCheck())
{
throw std::runtime_error("Exception was thrown");
}
Expand Down
Loading

0 comments on commit cd847fe

Please sign in to comment.