Commit 935f5d8a authored by John Parent's avatar John Parent
Browse files

Merge branch 'dev/configure-cxx-compiler-version' into 'master'

Configure cxx compiler version

See merge request !175
parents 9cb9754f d46aa93d
......@@ -600,6 +600,11 @@ An example of the function invocation is here::
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
LINK_LIBRARIES wrapper_example)
The argument provided for the module <name> is exposed as a library target in the current CMake context.
This allows users to configure further options not directly specified in this project. Pybind11 supports compiler default
options, or the minimum standard required by the Pybind11 library itself, however, compiler and other options
can be further specified if required by a project. For more details see `Pybind11's Documentation`_ .
via Python
##########
This script assumes that Python 3.* is used.
......
......@@ -53,6 +53,7 @@ add_autopybind11_test(commented)
add_autopybind11_test(missing_classes)
add_autopybind11_test(complex_trampoline)
add_autopybind11_test(interface_library)
add_autopybind11_test(cxx11_compliant)
add_autopybind11_test(gen_only)
add_autopybind11_test(multi_module)
add_autopybind11_test(multi_namespace)
......
cmake_minimum_required(VERSION 3.15)
project(cxx-compliance CXX)
find_package(AutoPyBind11)
autopybind11_fetch_build_pybind11(PYBIND11_DIR ${PYBIND11_SRC_DIR})
add_library(holder_types shared_holder.cpp unique.cpp)
target_sources(holder_types INTERFACE shared_base.hpp)
target_include_directories(holder_types INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET holder_types PROPERTY POSITION_INDEPENDENT_CODE ON)
autopybind11_add_module(compliance_mod
YAML_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/holder_wrap.yml
CONFIG_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/config.yml
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
LINK_LIBRARIES holder_types)
private_members_as_fields: False
use_cxx_14: False
\ No newline at end of file
import os, sys, unittest
sys.path.append(os.getcwd())
from compliance_mod import (
Base,
UniqueHolder,
sharedHolder,
Base2,
take_your_ptr,
)
class TestBase(unittest.TestCase):
def test_constructor(self):
Base()
Base(4, 8)
Base(4)
def test_methods(self):
b = Base(4, 8)
self.assertEqual(b.size(), 4)
self.assertEqual(b.sum(), 8)
arr = b.get_arr()
self.assertEqual(8, arr)
class TestsharedHolder(unittest.TestCase):
def test_constructor(self):
sharedHolder()
sharedHolder(Base())
def test_methods(self):
b = Base(4, 8)
s = sharedHolder(b)
self.assertEqual(s.sum(), 8)
self.assertEqual(s.size(), 4)
def test_shared_base(self):
b = Base(4, 8)
s = sharedHolder(b)
b1 = s.base_arr()
self.assertIs(b1, b)
a = s.ret_int(1)
self.assertEqual(a, 1)
def test_shared_stl(self):
b = sharedHolder(Base(4, 8))
v = b.get_vec()
new_v = [1.0, 2.0, 3.0]
b.set_vec(new_v)
new_v[1] = 4.0
new_b = b.get_vec()
old_v = b.ret_vec(new_v)
class TestUniqueHolder(unittest.TestCase):
def test_constructor(self):
UniqueHolder()
UniqueHolder(3)
def test_methods(self):
u = UniqueHolder(999)
self.assertIsInstance(u.give(), Base2)
u.take(3)
a = 3
b = u.give_back_int(a)
self.assertEqual(a, b)
c = Base2()
d = u.give_back_u_base(c)
self.assertIsInstance(d, Base2)
class TestFreeFunction(unittest.TestCase):
def test_free_function(self):
take_your_ptr(1)
if __name__ == "__main__":
unittest.main()
files:
shared_holder.h:
classes:
sharedHolder:
customization:
custom_holder_type: "std::shared_ptr<{}>"
unique.h:
classes:
UniqueHolder:
Base2:
functions:
take_your_ptr:
shared_base.hpp:
classes:
Base:
#ifndef SHARED_BASE
#define SHARED_BASE
#include <memory>
class Base:
public std::enable_shared_from_this<Base>
{
public:
Base() {this->size_ = 1;
this->arr = new int[1]{0};}
Base(size_t size, int value = 0) : size_(size) { arr = new int[size]{value}; }
~Base() {delete arr;}
inline int const get_arr() const {return *arr;}
inline size_t const size() const {return size_;}
inline const int sum() const {int sum = 0;
for(int i = 0; i<this->size_;i++)
{
sum = sum + *(this->arr+i);
}
return sum;}
private:
int * arr;
size_t size_;
};
#endif // SHARED_BASE
\ No newline at end of file
#include "shared_holder.h"
sharedHolder::sharedHolder()
{
this->b = std::shared_ptr<Base>(new Base());
this->v = std::shared_ptr<std::vector<double>>(new std::vector<double>());
}
sharedHolder::sharedHolder(std::shared_ptr<Base> b)
{
this->b = b->shared_from_this();
this->v = std::shared_ptr<std::vector<double>>(new std::vector<double>());
}
const int sharedHolder::sum()
{
return this->b->sum();
}
const int sharedHolder::size()
{
return (int)this->b->size();
}
std::shared_ptr<Base> sharedHolder::base_arr()
{
return this->b;
}
std::shared_ptr<std::vector<double>> sharedHolder::get_vec()
{
return this->v;
}
void sharedHolder::set_vec(std::shared_ptr<std::vector<double>> new_v)
{
this->v = new_v;
}
#ifndef SHARED_HOLDER
#define SHARED_HOLDER
#include <memory>
#include <vector>
#include "shared_base.hpp"
typedef std::shared_ptr<std::vector<double>> sv_doub;
using s_int = std::shared_ptr<int>;
class sharedHolder
{
public:
sharedHolder();
sharedHolder(std::shared_ptr<Base> b);
const int sum();
const int size();
std::shared_ptr<Base> base_arr();
std::shared_ptr<std::vector<double>> get_vec();
void set_vec(std::shared_ptr<std::vector<double>> new_v);
sv_doub ret_vec(sv_doub a) {return a;}
s_int ret_int(s_int a) {return a;}
private:
std::shared_ptr<Base> b;
std::shared_ptr<std::vector<double>> v;
};
#endif // SHARED_HOLDER
\ No newline at end of file
#include "unique.h"
UniqueHolder::UniqueHolder()
{
this->b = std::unique_ptr<int>(new int());
}
UniqueHolder::UniqueHolder(std::unique_ptr<int> b)
{
this->b = std::move(b);
}
void UniqueHolder::take(std::unique_ptr<int> new_b)
{
this->b = std::move(new_b);
}
std::unique_ptr<Base2> UniqueHolder::give()
{
return std::unique_ptr<Base2>{new Base2()};
}
void take_your_ptr(std::unique_ptr<int> int_ptr)
{
auto mine = std::move(int_ptr);
}
\ No newline at end of file
#ifndef UNIQUE_H
#define UNIQUE_H
#include <memory>
#include "shared_base.hpp"
class Base2
{
public:
Base2() {}
};
typedef std::unique_ptr<int> u_int_ptr;
using ub2 = std::unique_ptr<Base2>;
class UniqueHolder
{
public:
UniqueHolder();
UniqueHolder(std::unique_ptr<int> b);
std::unique_ptr<Base2> give();
void take(std::unique_ptr<int> new_b);
u_int_ptr give_back_int(u_int_ptr a) {return a;}
ub2 give_back_u_base(ub2 a) {return a;}
private:
std::unique_ptr<int> b;
};
void take_your_ptr(std::unique_ptr<int> int_ptr);
#endif // UNIQUE_H
\ No newline at end of file
......@@ -1123,12 +1123,18 @@ class BindingsGenerator:
).decl_string
if dec.templates.is_instantiation(base_decl_string):
dec_type = dec.templates.split(base_decl_string)
if "unique_ptr" in dec_type[0]:
if "unique_ptr" in dec_type[0] and self.opts.cxx14_flag:
type_ = dec_type[1][0]
lambda_return += (
self.opts.make_unique_fmt.format(type=type_, var=name)
+ ","
)
elif "unique_ptr" in dec_type[0] and not self.opts.cxx14_flag:
type_ = dec_type[1][0]
lambda_return += (
self.opts.compose_unique.format(type=type_, var=name)
+ ","
)
else:
type_ = arg.decl_type.decl_string
lambda_return += name + ", "
......@@ -1195,12 +1201,17 @@ class BindingsGenerator:
dec_type = dec.templates.split(
dec.type_traits.base_type(arg.decl_type).decl_string
)
if "unique_ptr" in dec_type[0]:
if "unique_ptr" in dec_type[0] and self.opts.use_cxx_14:
type_ = dec_type[1][0]
lambda_return += (
self.opts.make_unique_fmt.format(type=type_, var=name)
+ ","
)
elif "unique_ptr" in dec_type[0] and not self.opts.use_cxx_14:
type_ = dec_type[1][0]
lambda_return += (
self.opts.compose_unique.format(type=type_, var=name) + ","
)
elif "shared_ptr" in dec_type[0] and (
self.holds_container_type(dec_type[1])
or dec_type[1][0] in dec.cpptypes.FUNDAMENTAL_TYPES
......@@ -3412,6 +3423,15 @@ def main(argv=None):
default=True,
help="Toggle use of return policy for returning Eigen types",
)
arg.add(
"--use_cxx_14",
"-cxx14",
dest="cxx14_flag",
action="store",
type=lambda x: bool(distutils.util.strtobool(x)),
default=True,
help="Enable cxx14 standard features, if turned off, cxx11 is assumed",
)
# The formatted strings that will write the pybind code are also configurable
arg.add(
"--common_cpp_body_fmt", required=False, default=tb.common_cpp_body
......@@ -3558,6 +3578,7 @@ def main(argv=None):
arg.add("--custom_ctor_fmt", required=False, default=tb.custom_constructor)
arg.add("--make_unique_fmt", required=False, default=tb.make_unique)
arg.add("--make_shared_fmt", required=False, default=tb.make_shared)
arg.add("--compose_unique", required=False, default=tb.compose_unique)
arg.add("--conversion", required=False, default=tb.conversion)
arg.add(
"--generic_lambda_def_fmt",
......
......@@ -7,7 +7,9 @@ These strings will be formatted in generator.py, then used to
generate valid pybind11 binding code
"""
submodule_signature = """py::module {mod_name} = m.def_submodule("{name}", "");"""
submodule_signature = (
"""py::module {mod_name} = m.def_submodule("{name}", "");"""
)
arg_val_cast = " = {type}({val})"
nullptr_arg_val = " = ({type}){val}"
common_cpp_body = """
......@@ -220,9 +222,7 @@ call_template = (
)
wrap_header = """{includes}\n{class_decs}\n{func_ptr_assigns}"""
enum_header = (
"""py::enum_<{class_name}>({module},\"{name}\",{type}{doc})\n"""
)
enum_header = """py::enum_<{class_name}>({module},\"{name}\",{type}{doc})\n"""
enum_val = """.value(\"{short_name}\", {scoped_name}, \"{doc}\")\n"""
keep_alive = """, py::keep_alive<{}, {}>()"""
......@@ -259,6 +259,7 @@ conversion = """auto wrap_{conv_name} = {converting_constr};"""
std_move = """std::move({})"""
make_shared = """std::make_shared<{type}>({var})"""
make_unique = """std::make_unique<{type}>({var})"""
compose_unique = """std::unique_ptr<{type}>(new {type}({var}))"""
make_tuple = """std::make_tuple({})"""
attribute = """{name}.attr("{attribute}") = {value}; """
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment