2016-09-03 18:54:22 +00:00
|
|
|
#pragma once
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/pybind11.h>
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
namespace py = pybind11;
|
Support keyword arguments and generalized unpacking in C++
A Python function can be called with the syntax:
```python
foo(a1, a2, *args, ka=1, kb=2, **kwargs)
```
This commit adds support for the equivalent syntax in C++:
```c++
foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)
```
In addition, generalized unpacking is implemented, as per PEP 448,
which allows calls with multiple * and ** unpacking:
```python
bar(*args1, 99, *args2, 101, **kwargs1, kz=200, **kwargs2)
```
and
```c++
bar(*args1, 99, *args2, 101, **kwargs1, "kz"_a=200, **kwargs2)
```
2016-08-29 01:05:42 +00:00
|
|
|
using namespace pybind11::literals;
|
2016-09-03 18:54:22 +00:00
|
|
|
|
|
|
|
class test_initializer {
|
2017-06-08 22:44:49 +00:00
|
|
|
using Initializer = void (*)(py::module &);
|
|
|
|
|
|
|
|
public:
|
|
|
|
test_initializer(Initializer init);
|
|
|
|
test_initializer(const char *submodule_name, Initializer init);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TEST_SUBMODULE(name, variable) \
|
|
|
|
void test_submodule_##name(py::module &); \
|
|
|
|
test_initializer name(#name, test_submodule_##name); \
|
|
|
|
void test_submodule_##name(py::module &variable)
|
|
|
|
|
|
|
|
|
|
|
|
/// Dummy type which is not exported anywhere -- something to trigger a conversion error
|
|
|
|
struct UnregisteredType { };
|
|
|
|
|
|
|
|
/// A user-defined type which is exported and can be used by any test
|
|
|
|
class UserType {
|
|
|
|
public:
|
|
|
|
UserType() = default;
|
|
|
|
UserType(int i) : i(i) { }
|
|
|
|
|
|
|
|
int value() const { return i; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int i = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
|
|
|
|
class IncType : public UserType {
|
2016-09-03 18:54:22 +00:00
|
|
|
public:
|
2017-06-08 22:44:49 +00:00
|
|
|
using UserType::UserType;
|
|
|
|
IncType() = default;
|
|
|
|
IncType(const IncType &other) : IncType(other.value() + 1) { }
|
|
|
|
IncType(IncType &&) = delete;
|
|
|
|
IncType &operator=(const IncType &) = delete;
|
|
|
|
IncType &operator=(IncType &&) = delete;
|
2016-09-03 18:54:22 +00:00
|
|
|
};
|