From 1fd2e4174cc3d4a3f099136f59447c4966913494 Mon Sep 17 00:00:00 2001 From: Yannik Stradmann Date: Tue, 2 Jul 2019 11:50:25 +0200 Subject: [PATCH] Add genpybind example --- playground/genpybind/example.cpp | 9 +++++++ playground/genpybind/example.h | 20 +++++++++++++++ playground/genpybind/example_test.py | 9 +++++++ playground/genpybind/wscript | 37 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 playground/genpybind/example.cpp create mode 100644 playground/genpybind/example.h create mode 100644 playground/genpybind/example_test.py create mode 100644 playground/genpybind/wscript diff --git a/playground/genpybind/example.cpp b/playground/genpybind/example.cpp new file mode 100644 index 00000000..a237e56f --- /dev/null +++ b/playground/genpybind/example.cpp @@ -0,0 +1,9 @@ +#include "example.h" + +constexpr int Example::not_exposed; + +int Example::calculate(int some_argument) const { return _value + some_argument; } + +int Example::getSomething() const { return _value; } + +void Example::setSomething(int value) { _value = value; } diff --git a/playground/genpybind/example.h b/playground/genpybind/example.h new file mode 100644 index 00000000..95636377 --- /dev/null +++ b/playground/genpybind/example.h @@ -0,0 +1,20 @@ +#pragma once + +#include "genpybind.h" + +class GENPYBIND(visible) Example { +public: + static constexpr int GENPYBIND(hidden) not_exposed = 10; + + /// \brief Do a complicated calculation. + int calculate(int some_argument = 5) const; + + GENPYBIND(getter_for(something)) + int getSomething() const; + + GENPYBIND(setter_for(something)) + void setSomething(int value); + +private: + int _value = 0; +}; diff --git a/playground/genpybind/example_test.py b/playground/genpybind/example_test.py new file mode 100644 index 00000000..13843909 --- /dev/null +++ b/playground/genpybind/example_test.py @@ -0,0 +1,9 @@ +import pyexample as m + + +def test_example(): + obj = m.Example() + obj.something = 42 + assert obj.something == 42 + assert obj.calculate() == 47 # with default argument + assert obj.calculate(2) == 44 diff --git a/playground/genpybind/wscript b/playground/genpybind/wscript new file mode 100644 index 00000000..1732ec83 --- /dev/null +++ b/playground/genpybind/wscript @@ -0,0 +1,37 @@ +#!/usr/bin/env python + + +def options(opt): + opt.load('python') + opt.load('compiler_cxx') + opt.load('genpybind') + + +def configure(cfg): + cfg.load('python') + cfg.load('compiler_cxx') + cfg.check_python_version((2, 7)) + cfg.check_python_headers() + cfg.load('genpybind') + + cfg.check(compiler='cxx', + features='cxx pyext', + uselib_store='PYBIND11GENPYBIND_EXAMPLE', + mandatory=True, + header_name='pybind11/pybind11.h') + + +def build(bld): + bld(target='example_inc', + export_includes='.') + + bld.shlib(target='example', + source='example.cpp', + features='use', + use='example_inc') + + bld(target='pyexample', + source='example.h', + genpybind_tags='genpybind_example', + features='use genpybind cxx cxxshlib pyext', + use=['PYBIND11GENPYBIND_EXAMPLE', 'example'])