Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kliegeois committed Sep 25, 2023
1 parent 017b9b3 commit 55bb0dd
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ void ClassBinder::bind(Context &context)
if( named_class ) {
if (Config::get().is_smart_holder_requested(qualified_name_without_template)) {
c += '\t' +
R"(PYBIND11_TYPE_CASTER_BASE_HOLDER({}, {})"_format(qualified_name, maybe_holder_type) +
R"(PYBIND11_TYPE_CASTER_BASE_HOLDER({} {}))"_format(qualified_name, maybe_holder_type) +
'\n';
}
c += '\t' +
Expand Down
2 changes: 2 additions & 0 deletions test/T61.smart_holder.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
+custom_shared my_shared_ptr
+smart_holder A
54 changes: 54 additions & 0 deletions test/T61.smart_holder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
// vi: set ts=2 noet:
//
// Copyright (c) 2016 Sergey Lyskov <[email protected]>
//
// All rights reserved. Use of this source code is governed by a
// MIT license that can be found in the LICENSE file.

/// @file binder/test/T01.enum.hpp
/// @brief Binder self-test file. Bindings of enum's functionality.
/// @author Sergey Lyskov

#ifndef _INCLUDED_T60_hpp_
#define _INCLUDED_T60_hpp_

#include <memory>

template<typename T>
using my_shared_ptr = std::shared_ptr<T>;

enum E1 { E1_V0, E1_V1 };

enum struct E2_struct { V0, V1 };
enum class E3_class { V0, V1 };


template<typename T>
class A
{
public:
enum AE1 { AE1_V0, AE1_V1 };
enum struct AE2_struct { AE3_V0, AE3_V1 };
enum class AE3_class { AE2_V0, AE2_V1 };

protected:
enum AE3_not_binded { AE3_V0_not_binded, AE3_V1_not_binded };
enum class AE4_not_binded { AE4_V0_not_binded, AE4_V1_not_binded };

private:
enum AE5_not_binded { AE5_V0_not_binded, AE5_V1_not_binded };
enum class AE6_not_binded { AE6_V0_not_binded, AE6_V1_not_binded };
};

void fi_instantiated_by_use_in_function(A<int>)
{
}
void fi(A<int> &)
{
}
void fi(A<int> *)
{
}

#endif // _INCLUDED_T01_enum_hpp_
126 changes: 126 additions & 0 deletions test/T61.smart_holder.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// File: T61_smart_holder.cpp
#include <T61.smart_holder.hpp> // A
#include <T61.smart_holder.hpp> // E1
#include <T61.smart_holder.hpp> // E2_struct
#include <T61.smart_holder.hpp> // E3_class
#include <T61.smart_holder.hpp> // fi
#include <T61.smart_holder.hpp> // fi_instantiated_by_use_in_function
#include <sstream> // __str__

#include <functional>
#include <pybind11/smart_holder.h>
#include <string>

#ifndef BINDER_PYBIND11_TYPE_CASTER
#define BINDER_PYBIND11_TYPE_CASTER
PYBIND11_DECLARE_HOLDER_TYPE(T, my_shared_ptr<T>)
PYBIND11_DECLARE_HOLDER_TYPE(T, T*)
PYBIND11_MAKE_OPAQUE(my_shared_ptr<void>)
#endif

void bind_T61_smart_holder(std::function< pybind11::module &(std::string const &namespace_) > &M)
{
// E1 file:T61.smart_holder.hpp line:21
pybind11::enum_<E1>(M(""), "E1", pybind11::arithmetic(), "")
.value("E1_V0", E1_V0)
.value("E1_V1", E1_V1)
.export_values();

;

// E2_struct file:T61.smart_holder.hpp line:23
pybind11::enum_<E2_struct>(M(""), "E2_struct", "")
.value("V0", E2_struct::V0)
.value("V1", E2_struct::V1)
.export_values();

;

// E3_class file:T61.smart_holder.hpp line:24
pybind11::enum_<E3_class>(M(""), "E3_class", "")
.value("V0", E3_class::V0)
.value("V1", E3_class::V1);

;

{ // A file:T61.smart_holder.hpp line:28
PYBIND11_TYPE_CASTER_BASE_HOLDER(A<int> , my_shared_ptr<A<int>>)
pybind11::class_<A<int>, my_shared_ptr<A<int>>> cl(M(""), "A_int_t", "");
cl.def( pybind11::init( [](){ return new A<int>(); } ) );

pybind11::enum_<A<int>::AE1>(cl, "AE1", pybind11::arithmetic(), "")
.value("AE1_V0", A<int>::AE1_V0)
.value("AE1_V1", A<int>::AE1_V1)
.export_values();


pybind11::enum_<A<int>::AE2_struct>(cl, "AE2_struct", "")
.export_values();


pybind11::enum_<A<int>::AE3_class>(cl, "AE3_class", "");

}
// fi_instantiated_by_use_in_function(class A<int>) file:T61.smart_holder.hpp line:44
M("").def("fi_instantiated_by_use_in_function", (void (*)(class A<int>)) &fi_instantiated_by_use_in_function, "C++: fi_instantiated_by_use_in_function(class A<int>) --> void", pybind11::arg(""));

// fi(class A<int> &) file:T61.smart_holder.hpp line:47
M("").def("fi", (void (*)(class A<int> &)) &fi, "C++: fi(class A<int> &) --> void", pybind11::arg(""));

// fi(class A<int> *) file:T61.smart_holder.hpp line:50
M("").def("fi", (void (*)(class A<int> *)) &fi, "C++: fi(class A<int> *) --> void", pybind11::arg(""));

}


#include <map>
#include <algorithm>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>

#include <pybind11/smart_holder.h>

typedef std::function< pybind11::module & (std::string const &) > ModuleGetter;

void bind_T61_smart_holder(std::function< pybind11::module &(std::string const &namespace_) > &M);


PYBIND11_MODULE(T61_smart_holder, root_module) {
root_module.doc() = "T61_smart_holder module";

std::map <std::string, pybind11::module> modules;
ModuleGetter M = [&](std::string const &namespace_) -> pybind11::module & {
auto it = modules.find(namespace_);
if( it == modules.end() ) throw std::runtime_error("Attempt to access pybind11::module for namespace " + namespace_ + " before it was created!!!");
return it->second;
};

modules[""] = root_module;

static std::vector<std::string> const reserved_python_words {"nonlocal", "global", };

auto mangle_namespace_name(
[](std::string const &ns) -> std::string {
if ( std::find(reserved_python_words.begin(), reserved_python_words.end(), ns) == reserved_python_words.end() ) return ns;
else return ns+'_';
}
);

std::vector< std::pair<std::string, std::string> > sub_modules {
};
for(auto &p : sub_modules ) modules[p.first.size() ? p.first+"::"+p.second : p.second] = modules[p.first].def_submodule( mangle_namespace_name(p.second).c_str(), ("Bindings for " + p.first + "::" + p.second + " namespace").c_str() );

//pybind11::class_<std::shared_ptr<void>>(M(""), "_encapsulated_data_");

bind_T61_smart_holder(M);

}

// Source list file: TEST/T61_smart_holder.sources
// T61_smart_holder.cpp
// T61_smart_holder.cpp

// Modules list file: TEST/T61_smart_holder.modules
//

0 comments on commit 55bb0dd

Please sign in to comment.