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_COMMON_BASE_HPP 10 #define PYGMO_COMMON_BASE_HPP 11 12 #include <pybind11/pybind11.h> 13 14 #include "common_utils.hpp" 15 16 namespace pygmo 17 { 18 19 namespace py = pybind11; 20 21 // A common base class with methods useful in the implementation of 22 // the pythonic problem, algorithm, etc. 23 struct common_base { 24 static void check_mandatory_method(const py::object &, const char *, const char *); 25 // A simple wrapper for getters. It will try to: 26 // - get the attribute "name" from the object o, 27 // - call it without arguments, 28 // - extract an instance from the ret value and return it. 29 // If the attribute is not there or it is not callable, the value "def_value" will be returned. 30 template <typename RetType> getter_wrapperpygmo::common_base31 static RetType getter_wrapper(const py::object &o, const char *name, const RetType &def_value) 32 { 33 auto a = callable_attribute(o, name); 34 if (a.is_none()) { 35 return def_value; 36 } 37 return py::cast<RetType>(a()); 38 } 39 static void check_not_type(const py::object &, const char *); 40 }; 41 42 } // namespace pygmo 43 44 #endif 45