1 // Copyright 2020, 2021 PaGMO development team
2 //
3 // This file is part of the pygmo library.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla
6 // Public License v. 2.0. If a copy of the MPL was not distributed
7 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 #ifndef PYGMO_EXPOSE_S_POLICIES_HPP
10 #define PYGMO_EXPOSE_S_POLICIES_HPP
11
12 #include <pybind11/pybind11.h>
13
14 #include <pagmo/s_policy.hpp>
15
16 #include "common_utils.hpp"
17
18 namespace pygmo
19 {
20
21 namespace py = pybind11;
22
23 // Selection policies exposition function.
24 void expose_s_policies(py::module &, py::class_<pagmo::s_policy> &, py::module &);
25
26 // C++ UDSP exposition function.
27 template <typename SPol>
expose_s_policy(py::module & m,py::class_<pagmo::s_policy> & s_pol,py::module & s_module,const char * name,const char * descr)28 inline py::class_<SPol> expose_s_policy(py::module &m, py::class_<pagmo::s_policy> &s_pol, py::module &s_module,
29 const char *name, const char *descr)
30 {
31 py::class_<SPol> c(m, name, descr);
32
33 // We require all policies to be def-ctible at the bare minimum.
34 c.def(py::init<>());
35
36 // Mark it as a C++ policy.
37 c.attr("_pygmo_cpp_s_policy") = true;
38
39 // Expose the s_policy constructor from SPol.
40 s_pol.def(py::init<const SPol &>(), py::arg("udsp"));
41
42 // Expose extract.
43 s_pol.def("_cpp_extract", &generic_cpp_extract<pagmo::s_policy, SPol>, py::return_value_policy::reference_internal);
44
45 // Add the policy to the policies submodule.
46 s_module.attr(name) = c;
47
48 return c;
49 }
50
51 } // namespace pygmo
52
53 #endif
54