Created
May 1, 2009 21:09
-
-
Save ruediger/105264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <flusspferd.hpp> | |
class class_info : public flusspferd::class_info { | |
char const *full_name_; | |
flusspferd::object proto; | |
char const *constructor_name_; | |
template<typename T> | |
friend class class_builder; | |
public: | |
class_info(char const *full_name, | |
flusspferd::object const &proto = flusspferd::create_object(), | |
char const *constructor_name = 0x0) | |
: full_name_(full_name), proto(proto), | |
constructor_name_(constructor_name ? constructor_name : full_name) | |
{ } | |
char const *constructor_name() const { | |
return constructor_name_; | |
} | |
char const *full_name() const { | |
return full_name_; | |
} | |
flusspferd::object const &create_prototype() const { | |
return proto; | |
} | |
}; | |
template<typename T> | |
class class_builder { | |
class_info info; | |
class_builder(char const *full_name, | |
flusspferd::object const &proto = flusspferd::create_object(), | |
char const *constructor_name = 0x0) | |
: info(full_name, proto, constructor_name) | |
{ } | |
public: | |
template<typename U> | |
class_builder &def(std::string const &name, boost::function<U> const &ptr) { | |
create_native_method(info.proto, name, ptr); | |
return *this; | |
} | |
template<typename U> | |
class_builder &def(std::string const &name, U *ptr) { | |
create_native_method(info.proto, name, ptr); | |
return *this; | |
} | |
template<typename U> | |
class_builder &def(std::string const &name, | |
void (U::*ptr)(flusspferd::call_context&)) | |
{ | |
create_native_method(info.proto, name, ptr); | |
return *this; | |
} | |
template<typename R, typename U> | |
class_builder &def(std::string const &name, | |
R U::*ptr) | |
{ | |
create_native_method(info.proto, name, ptr); | |
return *this; | |
} | |
void done(flusspferd::object container = flusspferd::global()) { | |
flusspferd::load_class<T, class_info>(container, info); | |
} | |
template<typename U> | |
friend class_builder<U> class_(char const *name, | |
char const *constructor_name = 0x0); | |
}; | |
template<typename T> | |
class_builder<T> class_(char const *name, char const *constructor_name = 0x0) { | |
return class_builder<T>(name, flusspferd::create_object(), constructor_name); | |
} | |
// Example | |
class Foo { | |
int i; | |
public: | |
Foo() : i(0) { } | |
Foo(int i) : i(i) { } | |
int get() /*const*/ { return i; } | |
void set(int j) { i = j; } | |
}; | |
int main() { | |
class_<Foo>("Foo") | |
.def("get", &Foo::get) | |
.def("set", &Foo::set) | |
.done(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment