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_R_POLICIES_HPP
10 #define PYGMO_EXPOSE_R_POLICIES_HPP
11 
12 #include <pybind11/pybind11.h>
13 
14 #include <pagmo/r_policy.hpp>
15 
16 #include "common_utils.hpp"
17 
18 namespace pygmo
19 {
20 
21 namespace py = pybind11;
22 
23 // Replacement policies exposition function.
24 void expose_r_policies(py::module &, py::class_<pagmo::r_policy> &, py::module &);
25 
26 // C++ UDRP exposition function.
27 template <typename RPol>
expose_r_policy(py::module & m,py::class_<pagmo::r_policy> & r_pol,py::module & r_module,const char * name,const char * descr)28 inline py::class_<RPol> expose_r_policy(py::module &m, py::class_<pagmo::r_policy> &r_pol, py::module &r_module,
29                                         const char *name, const char *descr)
30 {
31     py::class_<RPol> 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_r_policy") = true;
38 
39     // Expose the r_policy constructor from RPol.
40     r_pol.def(py::init<const RPol &>(), py::arg("udrp"));
41 
42     // Expose extract.
43     r_pol.def("_cpp_extract", &generic_cpp_extract<pagmo::r_policy, RPol>, py::return_value_policy::reference_internal);
44 
45     // Add the policy to the policies submodule.
46     r_module.attr(name) = c;
47 
48     return c;
49 }
50 
51 } // namespace pygmo
52 
53 #endif
54