File: test_class_sh_unique_ptr_custom_deleter.cpp

package info (click to toggle)
pybind11 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,448 kB
  • sloc: cpp: 27,239; python: 13,512; ansic: 4,244; makefile: 204; sh: 36
file content (30 lines) | stat: -rw-r--r-- 721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "pybind11_tests.h"

#include <memory>

namespace pybind11_tests {
namespace class_sh_unique_ptr_custom_deleter {

// Reduced from a PyCLIF use case in the wild by @wangxf123456.
class Pet {
public:
    using Ptr = std::unique_ptr<Pet, std::function<void(Pet *)>>;

    std::string name;

    static Ptr New(const std::string &name) {
        return Ptr(new Pet(name), std::default_delete<Pet>());
    }

private:
    explicit Pet(const std::string &name) : name(name) {}
};

TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) {
    py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name);

    m.def("create", &Pet::New);
}

} // namespace class_sh_unique_ptr_custom_deleter
} // namespace pybind11_tests