/
hartree_fock_solution_hack.hh
76 lines (65 loc) · 2.71 KB
/
hartree_fock_solution_hack.hh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Copyright (C) 2019 by the adcc authors
//
// This file is part of adcc.
//
// adcc is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// adcc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with adcc. If not, see <http://www.gnu.org/licenses/>.
//
#pragma once
#include <adcc/HartreeFockSolution_i.hh>
#include <pybind11/pybind11.h>
namespace pybind11 {
namespace detail {
using adcc::HartreeFockSolution_i;
namespace py = pybind11;
// This workaround is straight from
// https://github.com/pybind/pybind11/issues/1546 Should not be needed any more
// once the above is merged
template <>
struct type_caster<std::shared_ptr<const HartreeFockSolution_i>> {
PYBIND11_TYPE_CASTER(std::shared_ptr<const HartreeFockSolution_i>,
_("HartreeFockSolution_i"));
using HFSiCaster = copyable_holder_caster<const HartreeFockSolution_i,
std::shared_ptr<const HartreeFockSolution_i>>;
bool load(pybind11::handle src, bool b) {
HFSiCaster bc;
bool success = bc.load(src, b);
if (!success) {
return false;
}
auto py_obj = py::reinterpret_borrow<py::object>(src);
auto cpi_ptr = static_cast<std::shared_ptr<const HartreeFockSolution_i>>(bc);
// Construct a shared_ptr to the py::object
// For the deleter note, that it's possible that when the shared_ptr
// dies we won't have the GIL (global interpreter lock)
// (if the last holder is in a non-Python thread), so we make
// sure to acquire it in the deleter.
auto py_obj_ptr =
std::shared_ptr<py::object>{new object{py_obj}, [](py::object* py_object_ptr) {
gil_scoped_acquire gil;
delete py_object_ptr;
}};
value = std::shared_ptr<const HartreeFockSolution_i>(py_obj_ptr, cpi_ptr.get());
return true;
}
static handle cast(std::shared_ptr<const HartreeFockSolution_i> base,
return_value_policy rvp, handle h) {
return HFSiCaster::cast(base, rvp, h);
}
};
template <>
struct is_holder_type<const HartreeFockSolution_i,
std::shared_ptr<const HartreeFockSolution_i>> : std::true_type {};
} // namespace detail
} // namespace pybind11