Commit d46aa93d authored by John Parent's avatar John Parent
Browse files

cxx11 compliance full support unique ptr

parent 743c55b8
Pipeline #229223 passed with stages
in 30 minutes and 32 seconds
......@@ -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
......@@ -1201,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
......@@ -3423,8 +3428,8 @@ def main(argv=None):
"-cxx14",
dest="cxx14_flag",
action="store",
default=True,
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
......
Markdown is supported
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