[go: nahoru, domu]

Skip to content

Commit

Permalink
Added constructors to adapt_class and create_class
Browse files Browse the repository at this point in the history
  • Loading branch information
felipealmeida committed Dec 7, 2012
1 parent 0fa236a commit de4725f
Show file tree
Hide file tree
Showing 28 changed files with 892 additions and 197 deletions.
27 changes: 21 additions & 6 deletions demo/bind_class/bind_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@
#include <jvb/adapt_class.hpp>
#include <jvb/create_class.hpp>

#include <boost/functional/value_factory.hpp>

#include <iostream>

JVB_ADAPT_CLASS((mypackage)(HelloWorld)
, (public)
, (methods (print1, void())(print2, void())))
, (methods (print1, void())(print2, void())
)
(constructors (HelloWorld()) // default constructor
)
)

struct hello_world
{
hello_world()
{
static std::size_t i = 0;
assert(++i == 1);
std::cout << "hello_world::hello_world" << std::endl;
}

Expand All @@ -35,13 +43,20 @@ int main()
jvb::jvm jvm;
jvb::environment e = jvm.environment();

using namespace jvb::bind_placeholders;

JVB_CREATE_CLASS(HelloWorld, hello_world, e
, (methods (print1, &hello_world::print<1>)
(print2, &hello_world::print<2>)));

HelloWorld hello_world = jvb::new_<HelloWorld>(e);
(print2, &hello_world::print<2>)
)
(factory_constructors (HelloWorld()
, boost::value_factory<hello_world>())
)
);

std::cout << "Instantiating HelloWorld java class" << std::endl;
HelloWorld hello_world(e);
std::cout << "Instantiated HelloWorld java class" << std::endl;
hello_world.print1()(e);
std::cout << "Called print1" << std::endl;
hello_world.print2()(e);
std::cout << "Called print2" << std::endl;
}
9 changes: 6 additions & 3 deletions demo/manually_attribute/manually_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
JVB_ADAPT_CLASS((ManuallyAttribute)
, public
, (attributes (x, jvb::int_, nil)
(y, jvb::long_, static)))
(y, jvb::long_, static)
)
(constructors (ManuallyAttribute())
)
)

int main()
{
Expand All @@ -33,8 +37,7 @@ int main()
assert(c != jvb::Class());
std::cout << "Loaded ManuallyAttribute" << std::endl;

ManuallyAttribute manually_attribute
= jvb::new_<ManuallyAttribute>(env);
ManuallyAttribute manually_attribute(env);

manually_attribute.x(env) = 5;
assert(manually_attribute.x(env) == jvb::int_(5));
Expand Down
9 changes: 6 additions & 3 deletions demo/manually_calling/manually_calling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

JVB_ADAPT_CLASS((ManuallyCallingHelloWorld)
, public
, (methods (sayHello, void())))
, (methods (sayHello, void())
)
(constructors (ManuallyCallingHelloWorld())
)
)

int main()
{
Expand All @@ -32,8 +36,7 @@ int main()
assert(c != jvb::Class());
std::cout << "Loaded ManuallyCallingHelloWorld" << std::endl;

ManuallyCallingHelloWorld manually_calling_hello_world
= jvb::new_<ManuallyCallingHelloWorld>(env);
ManuallyCallingHelloWorld manually_calling_hello_world(env);

manually_calling_hello_world.sayHello()(env);
}
62 changes: 52 additions & 10 deletions include/jvb/adapt_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#ifndef JVB_ADAPT_CLASS_HPP
#define JVB_ADAPT_CLASS_HPP

#include <jvb/jvb.hpp>
#include <jvb/function_definition.hpp>
#include <jvb/field.hpp>
#include <jvb/detail/preprocessor/seq_filler.hpp>
#include <jvb/detail/max_args.hpp>
#include <jvb/detail/overload_matches.hpp>
#include <jvb/adapt_class/attribute_def.hpp>
#include <jvb/adapt_class/overload_def.hpp>
#include <jvb/adapt_class/method_def.hpp>
Expand All @@ -25,20 +28,23 @@
#include <boost/preprocessor/facilities/intercept.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/punctuation/paren.hpp>
#include <boost/preprocessor/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>

#include <boost/mpl/identity.hpp>
#include <boost/mpl/copy_if.hpp>

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M(R, DATA, OVERLOAD) \
JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_DEF OVERLOAD

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_FOR_EACH(OVERLOADS) \
BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
, ~, OVERLOADS)
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_FOR_EACH(OVERLOADS) \
BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
, ~, OVERLOADS)

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_METHOD_M(r, DATA, ELEM) \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_METHOD_M(R, DATA, ELEM) \
JVB_ADAPT_CLASS_METHOD_DEF ELEM

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_METHODS_SEQ_M(r, DATA, I, ELEM) \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_METHODS_SEQ_M(R, DATA, I, ELEM) \
BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 0, ELEM), _definition)

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_METHODS_FOR_EACH(METHODS) \
Expand All @@ -50,9 +56,14 @@ BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTE_M(R, DATA, ATTRIBUTE) \
JVB_ADAPT_CLASS_ATTRIBUTE_DEF ATTRIBUTE

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTES_FOR_EACH(ATTRIBUTES) \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTES_FOR_EACH(ATTRIBUTES) \
BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTE_M, ~, ATTRIBUTES)

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_CONSTRUCTORS_FOR_EACH(CONSTRUCTORS) \
typedef boost::mpl::vector \
<BOOST_PP_SEQ_ENUM(CONSTRUCTORS)> \
all_constructors;

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS(OVERLOADS) \
JVB_PP_CALL_FILLED(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_FOR_EACH, OVERLOADS)

Expand All @@ -62,6 +73,9 @@ BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTES(ATTRIBUTES) \
JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTES_FOR_EACH(JVB_PP_CALL_FILL(3, ATTRIBUTES))

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_CONSTRUCTORS(SIGNATURES) \
JVB_ADAPT_CLASS_MEMBER_DEFINE_CONSTRUCTORS_FOR_EACH(SIGNATURES)

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_AUX_NAME_overloads \
JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS BOOST_PP_LPAREN()

Expand All @@ -71,6 +85,9 @@ BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
#define JVB_ADAPT_CLASS_MEMBER_DEFINE_AUX_NAME_attributes \
JVB_ADAPT_CLASS_MEMBER_DEFINE_ATTRIBUTES BOOST_PP_LPAREN()

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_AUX_NAME_constructors \
JVB_ADAPT_CLASS_MEMBER_DEFINE_CONSTRUCTORS BOOST_PP_LPAREN()

#define JVB_ADAPT_CLASS_NAME(c) \
BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(c)), c)

Expand All @@ -87,13 +104,33 @@ BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
#define JVB_ADAPT_CLASS_NO_EXPAND_MEMBER(I, MEMBERS) \
BOOST_PP_EMPTY BOOST_PP_LPAREN()

#define JVB_ADAPT_CLASS_CONSTRUCTORS_GENERIC(Z, N, DATA) \
template <typename E \
BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, typename A) \
> \
DATA ( E e BOOST_PP_ENUM_TRAILING_BINARY_PARAMS_Z(Z, N, A, a)) \
: base_type( ::jvb::detail::new_object_functor<base_type>() \
(e, ::jvb::class_(e, name()) \
, jvb::constructors<void(BOOST_PP_ENUM_PARAMS_Z(Z, N, A))> \
(e, ::jvb::class_(e, name())).raw())) \
{ \
typedef ::boost::mpl::copy_if<all_constructors \
, typename ::boost::mpl::lambda \
< ::jvb::detail::is_parameter_size_equal_to \
< ::boost::mpl::_1, ::boost::mpl::size_t<N> > >::type>::type overload_set; \
BOOST_MPL_ASSERT(( ::boost::mpl::and_ \
< ::boost::is_same<typename ::boost::remove_cv<E>::type, ::jvb::environment> \
, ::boost::mpl::not_< ::boost::mpl::equal_to< ::boost::mpl::size<overload_set> \
, ::boost::mpl::int_<0> > > >)); \
}

#define JVB_ADAPT_CLASS(C, modifiers, MEMBERS) \
struct JVB_ADAPT_CLASS_NAME(C) : jvb::object \
{ \
typedef JVB_ADAPT_CLASS_NAME(C) self_type; \
typedef jvb::object base_type; \
JVB_ADAPT_CLASS_NAME(C) ( ::jvb::environment e, ::jobject o) \
: base_type(e, o) {} \
JVB_ADAPT_CLASS_NAME(C) ( ::JNIEnv* env, ::jvb::detail::hidden_object o) \
: base_type(env, o) {} \
BOOST_PP_EXPAND \
(JVB_ADAPT_CLASS_EXPAND_MEMBER(0, MEMBERS) BOOST_PP_RPAREN() \
) \
Expand All @@ -112,14 +149,19 @@ BOOST_PP_SEQ_FOR_EACH(JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_SEQ_M \
(BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(MEMBERS), 3) \
, JVB_ADAPT_CLASS_EXPAND_MEMBER, JVB_ADAPT_CLASS_NO_EXPAND_MEMBER) \
(3, MEMBERS) BOOST_PP_RPAREN()) \
BOOST_PP_EXPAND \
(BOOST_PP_IIF \
(BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(MEMBERS), 4) \
, JVB_ADAPT_CLASS_EXPAND_MEMBER, JVB_ADAPT_CLASS_NO_EXPAND_MEMBER) \
(4, MEMBERS) BOOST_PP_RPAREN()) \
static const std::size_t name_size = \
sizeof(JVB_ADAPT_CLASS_PACKAGE_AND_NAME_STRING(C)) - 1; \
static const char* name() \
{ \
return JVB_ADAPT_CLASS_PACKAGE_AND_NAME_STRING(C); \
} \
BOOST_PP_REPEAT(JVB_MAX_ARGS, JVB_ADAPT_CLASS_CONSTRUCTORS_GENERIC \
, JVB_ADAPT_CLASS_NAME(C)) \
};



#endif
6 changes: 3 additions & 3 deletions include/jvb/adapt_class/method_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

#define JVB_ADAPT_CLASS_METHOD_DEF(NAME, SIGNATURE) \
struct BOOST_PP_CAT(NAME, _definition) \
: ::jvb::detail::overload_set \
: ::jvb::detail::function_set \
<typename boost::mpl::push_front \
<typename boost::function_types::parameter_types<SIGNATURE>::type \
, ::jvb::environment>::type \
, typename boost::function_types::result_type<SIGNATURE>::type \
, ::jvb::function_definition_object \
<BOOST_PP_CAT(NAME, _definition), SIGNATURE, self_type> > \
{ \
typedef ::jvb::detail::overload_set \
typedef ::jvb::detail::function_set \
<typename boost::mpl::push_front \
<typename boost::function_types::parameter_types<SIGNATURE>::type \
, ::jvb::environment>::type \
Expand All @@ -26,7 +26,7 @@
<BOOST_PP_CAT(NAME, _definition), SIGNATURE, self_type> > aux_type; \
BOOST_PP_CAT(NAME, _definition)(jobject obj) \
: aux_type(obj) {} \
typedef self_type this_type; \
typedef self_type this_type; \
typedef boost::mpl::identity<SIGNATURE>::type sig_type; \
static const std::size_t name_size = sizeof(BOOST_PP_STRINGIZE(NAME))-1; \
static const char* name() \
Expand Down
31 changes: 30 additions & 1 deletion include/jvb/adapt_class/overload_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,36 @@
#ifndef JVB_ADAPT_CLASS_OVERLOAD_DEF_HPP
#define JVB_ADAPT_CLASS_OVERLOAD_DEF_HPP

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_DEF(NAME, SIGNATURES)
#include <jvb/overload_definition.hpp>

#define JVB_ADAPT_CLASS_MEMBER_DEFINE_OVERLOADS_DEF(NAME, SIGNATURES) \
struct BOOST_PP_CAT(NAME, _definition) \
: ::jvb::detail::overload_set \
<boost::mpl::vector<BOOST_PP_SEQ_ENUM(SIGNATURES)> \
, void \
, ::jvb::overload_definition_object \
<BOOST_PP_CAT(NAME, _definition), self_type \
, BOOST_PP_SEQ_ENUM(SIGNATURES)> > \
{ \
typedef ::jvb::detail::overload_set \
<boost::mpl::vector<BOOST_PP_SEQ_ENUM(SIGNATURES)> \
, void \
, ::jvb::overload_definition_object \
<BOOST_PP_CAT(NAME, _definition), self_type \
, BOOST_PP_SEQ_ENUM(SIGNATURES)> > aux_type; \
BOOST_PP_CAT(NAME, _definition)(jobject obj) \
: aux_type(obj) {} \
typedef self_type this_type; \
static const std::size_t name_size = sizeof(BOOST_PP_STRINGIZE(NAME))-1; \
static const char* name() \
{ \
return BOOST_PP_STRINGIZE(NAME); \
} \
}; \
BOOST_PP_CAT(NAME, _definition) NAME() const \
{ \
return BOOST_PP_CAT(NAME, _definition)(raw()); \
}


#endif
Loading

0 comments on commit de4725f

Please sign in to comment.