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_ISLAND_HPP
10 #define PYGMO_ISLAND_HPP
11 
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 #include <typeindex>
16 
17 #include <pybind11/pybind11.h>
18 
19 #include <pagmo/island.hpp>
20 #include <pagmo/s11n.hpp>
21 
22 #include "common_base.hpp"
23 
24 namespace pagmo
25 {
26 
27 namespace detail
28 {
29 
30 namespace py = pybind11;
31 
32 // Disable the static UDI checks for py::object.
33 template <>
34 struct disable_udi_checks<py::object> : std::true_type {
35 };
36 
37 template <>
38 struct isl_inner<py::object> final : isl_inner_base, pygmo::common_base {
39     // Just need the def ctor, delete everything else.
40     isl_inner() = default;
41     isl_inner(const isl_inner &) = delete;
42     isl_inner(isl_inner &&) = delete;
43     isl_inner &operator=(const isl_inner &) = delete;
44     isl_inner &operator=(isl_inner &&) = delete;
45     explicit isl_inner(const py::object &);
46     std::unique_ptr<isl_inner_base> clone() const final;
47     // Mandatory methods.
48     void run_evolve(island &) const final;
49     // Optional methods.
50     std::string get_name() const final;
51     std::string get_extra_info() const final;
52 
53     std::type_index get_type_index() const final;
54     const void *get_ptr() const final;
55     void *get_ptr() final;
56 
57     template <typename Archive>
58     void save(Archive &, unsigned) const;
59     template <typename Archive>
60     void load(Archive &, unsigned);
61     BOOST_SERIALIZATION_SPLIT_MEMBER()
62 
63     py::object m_value;
64 };
65 
66 } // namespace detail
67 
68 } // namespace pagmo
69 
70 // Register the isl_inner specialisation for py::object.
71 PAGMO_S11N_ISLAND_EXPORT_KEY(pybind11::object)
72 
73 #endif
74