Skip to content

Commit

Permalink
Merge pull request #410 from bluescarni/pr/runtime_types
Browse files Browse the repository at this point in the history
Add runtime type extraction to the UDx
  • Loading branch information
bluescarni authored Mar 30, 2020
2 parents 64f4365 + fdb8b4b commit 576607b
Show file tree
Hide file tree
Showing 46 changed files with 1,301 additions and 109 deletions.
16 changes: 8 additions & 8 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ jobs:
- run:
name: Build and test
command: bash ./tools/circleci_focal_gcc9_asan.sh
focal_clang9:
focal_gcc9_docs:
docker:
- image: circleci/buildpack-deps:focal
steps:
- checkout
- run:
name: Build and test
command: bash ./tools/circleci_focal_clang9.sh
bionic_clang6_release:
command: bash ./tools/circleci_focal_gcc9_docs.sh
focal_clang9:
docker:
- image: circleci/buildpack-deps:bionic
- image: circleci/buildpack-deps:focal
steps:
- checkout
- run:
name: Build and test
command: bash ./tools/circleci_bionic_clang6_release.sh
bionic_gcc7_conda_docs:
command: bash ./tools/circleci_focal_clang9.sh
bionic_clang6_release:
docker:
- image: circleci/buildpack-deps:bionic
steps:
- checkout
- run:
name: Build and test
command: bash ./tools/circleci_bionic_gcc7_conda_docs.sh
command: bash ./tools/circleci_bionic_clang6_release.sh

workflows:
version: 2
Expand All @@ -50,4 +50,4 @@ workflows:
- focal_gcc9_asan
- focal_clang9
- bionic_clang6_release
- bionic_gcc7_conda_docs
- focal_gcc9_docs
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ set(PAGMO_SRC_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/task_queue.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/prime_numbers.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/gte_getter.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/type_name.cpp"
)

# Some compilers choke on the cec2013/2014 data arrays.
Expand Down
2 changes: 1 addition & 1 deletion cmake_modules/yacma/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2019 Francesco Biscani
Copyright (c) 2016-2020 Francesco Biscani

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
37 changes: 27 additions & 10 deletions cmake_modules/yacma/YACMACompilerLinkerSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,32 @@ endfunction()

# Enable conditionally a CXX flag, if supported by the compiler.
# This is for flags intended to be enabled in all configurations.
# NOTE: we use macros and go through temporary private variables
# because it's apparently impossible to append to an internal
# NOTE: we use macros because it's apparently impossible to append to an internal
# CACHEd list.
macro(_YACMA_CHECK_ENABLE_CXX_FLAG flag)
set(CMAKE_REQUIRED_QUIET TRUE)
check_cxx_compiler_flag("${flag}" YACMA_CHECK_CXX_FLAG)
check_cxx_compiler_flag("${flag}" YACMA_CHECK_CXX_FLAG::${flag})
unset(CMAKE_REQUIRED_QUIET)
if(YACMA_CHECK_CXX_FLAG)
if(YACMA_CHECK_CXX_FLAG::${flag})
message(STATUS "'${flag}': flag is supported by the compiler, enabling.")
list(APPEND _YACMA_CXX_FLAGS "${flag}")
else()
message(STATUS "'${flag}': flag is not supported by the compiler.")
endif()
# NOTE: check_cxx_compiler stores variables in the cache.
unset(YACMA_CHECK_CXX_FLAG CACHE)
endmacro()

# Enable conditionally a debug CXX flag, is supported by the compiler.
# This is for flags intended to be enabled in debug mode.
macro(_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG flag)
set(CMAKE_REQUIRED_QUIET TRUE)
check_cxx_compiler_flag("${flag}" YACMA_CHECK_DEBUG_CXX_FLAG)
check_cxx_compiler_flag("${flag}" YACMA_CHECK_DEBUG_CXX_FLAG::${flag})
unset(CMAKE_REQUIRED_QUIET)
if(YACMA_CHECK_DEBUG_CXX_FLAG)
if(YACMA_CHECK_DEBUG_CXX_FLAG::${flag})
message(STATUS "'${flag}': debug flag is supported by the compiler, enabling.")
list(APPEND _YACMA_CXX_FLAGS_DEBUG "${flag}")
else()
message(STATUS "'${flag}': debug flag is not supported by the compiler.")
endif()
unset(YACMA_CHECK_DEBUG_CXX_FLAG CACHE)
endmacro()

# What we want to avoid is to re-run the expensive flag checks. We will set cache variables
Expand All @@ -86,6 +82,8 @@ if(NOT _YACMACompilerLinkerSettingsRun)
# Configuration bits specific for GCC.
if(YACMA_COMPILER_IS_GNUCXX)
_YACMA_CHECK_ENABLE_CXX_FLAG(-fdiagnostics-color=auto)
# New in GCC 9.
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Waddress-of-packed-member)
endif()

# Configuration bits specific for clang.
Expand All @@ -94,7 +92,26 @@ if(NOT _YACMACompilerLinkerSettingsRun)
# for the time being.
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wshadow)
# Clang is better at this flag than GCC.
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Werror)
# NOTE: enable unconditionally, as it seems like the CMake
# machinery for detecting this fails. Perhaps the source code
# used for checking the flag emits warnings?
list(APPEND _YACMA_CXX_FLAGS_DEBUG "-Werror")
# New warnings in clang 8.
# NOTE: a few issues with macros here, let's disable for now.
# _YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wextra-semi-stmt)
# New warnings in clang 10.
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wtautological-overlap-compare)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wtautological-compare)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wtautological-bitwise-compare)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wbitwise-conditional-parentheses)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wrange-loop-analysis)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wmisleading-indentation)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wc99-designator)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wreorder-init-list)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wsizeof-pointer-div)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wsizeof-array-div)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wxor-used-as-pow)
_YACMA_CHECK_ENABLE_DEBUG_CXX_FLAG(-Wfinal-dtor-non-final-class)
endif()

# Common configuration for GCC, clang and Intel.
Expand Down
13 changes: 13 additions & 0 deletions config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ see https://www.gnu.org/licenses/. */
#ifndef PAGMO_CONFIG_HPP
#define PAGMO_CONFIG_HPP

// NOTE: include this so that we can
// detect _LIBCPP_VERSION below.
#include <ciso646>

// Start of defines instantiated by CMake.
// clang-format off
#define PAGMO_VERSION "@pagmo_VERSION@"
Expand All @@ -54,4 +58,13 @@ see https://www.gnu.org/licenses/. */

#endif

#if defined(__clang__) && defined(_LIBCPP_VERSION)

// When using clang + libc++, prefer the name-based
// extract() implementation for UDx classes. See
// the explanation in typeid_name_extract.hpp.
#define PAGMO_PREFER_TYPEID_NAME_EXTRACT

#endif

#endif
24 changes: 23 additions & 1 deletion doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ Changelog
New
~~~

- The type-erased wrappers now have additional member functions
to interact at runtime with the contained user-defined objects.
Specifically, it is now possible to fetch ``void`` pointers to the
user-defined objects without knowing their type, and to query
at runtime the ``std::type_index`` of the user-defined objects
(`#410 <https://github.com/esa/pagmo2/pull/410>`__).

- The default ``get_name()`` implementations for the type-erased
wrappers now return the demangled C++ name on most platforms
(`#410 <https://github.com/esa/pagmo2/pull/410>`__).

- Add a :cpp:func:`pagmo::base_bgl_topology::get_edge_weight()`
function to fetch the weight of an edge in a BGL topology
(`#407 <https://github.com/esa/pagmo2/pull/407>`__).
Expand All @@ -23,8 +34,19 @@ New
Fix
~~~

- Introduce a workaround for an issue present on some
compiler/standard library combinations, where
the ``dynamic_cast`` used in the ``extract()``
implementations would fail when crossing the boundaries
between ``dlopen()``-ed libraries
(`#410 <https://github.com/esa/pagmo2/pull/410>`__).

- Build fixes for recent CMake versions
(`#410 <https://github.com/esa/pagmo2/pull/410>`__).

- Various doc fixes
(`#405 <https://github.com/esa/pagmo2/pull/405>`__).
(`#410 <https://github.com/esa/pagmo2/pull/410>`__,
`#405 <https://github.com/esa/pagmo2/pull/405>`__).

2.14.0 (2020-03-04)
-------------------
Expand Down
38 changes: 38 additions & 0 deletions doc/sphinx/docs/cpp/bfe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ Batch fitness evaluator

:return: ``false`` if *this* was moved from, ``true`` otherwise.

.. cpp:function:: std::type_index get_type_index() const

.. versionadded:: 2.15

Get the type of the UDBFE.

This function will return the type
of the UDBFE stored within this :cpp:class:`~pagmo::bfe`
instance.

:return: the type of the UDBFE.

.. cpp:function:: const void *get_ptr() const
.. cpp:function:: void *get_ptr()

.. versionadded:: 2.15

Get a pointer to the UDBFE.

These functions will return a raw (const) pointer
to the internal UDBFE instance. Differently from
the :cpp:func:`~pagmo::bfe::extract()` overloads, these functions
do not require to pass the correct type
in input. It is however the user's responsibility
to cast the returned void pointer to the correct type.

.. note::

The returned value is a raw non-owning pointer: the lifetime of the pointee is tied to the lifetime
of ``this``, and ``delete`` must never be called on the pointer.

.. note::

The ability to extract a mutable pointer is provided only in order to allow to call non-const
methods on the internal UDBFE instance. Assigning a new UDBFE via this pointer is undefined behaviour.

:return: a pointer to the internal UDBFE.

.. cpp:function:: template <typename Archive> void save(Archive &ar, unsigned) const
.. cpp:function:: template <typename Archive> void load(Archive &ar, unsigned)

Expand Down
38 changes: 38 additions & 0 deletions doc/sphinx/docs/cpp/r_policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,44 @@ Replacement policy

:return: ``false`` if *this* was moved from, ``true`` otherwise.

.. cpp:function:: std::type_index get_type_index() const

.. versionadded:: 2.15

Get the type of the UDRP.

This function will return the type
of the UDRP stored within this :cpp:class:`~pagmo::r_policy`
instance.

:return: the type of the UDRP.

.. cpp:function:: const void *get_ptr() const
.. cpp:function:: void *get_ptr()

.. versionadded:: 2.15

Get a pointer to the UDRP.

These functions will return a raw (const) pointer
to the internal UDRP instance. Differently from
the :cpp:func:`~pagmo::r_policy::extract()` overloads, these functions
do not require to pass the correct type
in input. It is however the user's responsibility
to cast the returned void pointer to the correct type.

.. note::

The returned value is a raw non-owning pointer: the lifetime of the pointee is tied to the lifetime
of ``this``, and ``delete`` must never be called on the pointer.

.. note::

The ability to extract a mutable pointer is provided only in order to allow to call non-const
methods on the internal UDRP instance. Assigning a new UDRP via this pointer is undefined behaviour.

:return: a pointer to the internal UDRP.

.. cpp:function:: template <typename Archive> void save(Archive &ar, unsigned) const
.. cpp:function:: template <typename Archive> void load(Archive &ar, unsigned)

Expand Down
38 changes: 38 additions & 0 deletions doc/sphinx/docs/cpp/s_policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,44 @@ Selection policy

:return: ``false`` if *this* was moved from, ``true`` otherwise.

.. cpp:function:: std::type_index get_type_index() const

.. versionadded:: 2.15

Get the type of the UDSP.

This function will return the type
of the UDSP stored within this :cpp:class:`~pagmo::s_policy`
instance.

:return: the type of the UDSP.

.. cpp:function:: const void *get_ptr() const
.. cpp:function:: void *get_ptr()

.. versionadded:: 2.15

Get a pointer to the UDSP.

These functions will return a raw (const) pointer
to the internal UDSP instance. Differently from
the :cpp:func:`~pagmo::s_policy::extract()` overloads, these functions
do not require to pass the correct type
in input. It is however the user's responsibility
to cast the returned void pointer to the correct type.

.. note::

The returned value is a raw non-owning pointer: the lifetime of the pointee is tied to the lifetime
of ``this``, and ``delete`` must never be called on the pointer.

.. note::

The ability to extract a mutable pointer is provided only in order to allow to call non-const
methods on the internal UDSP instance. Assigning a new UDSP via this pointer is undefined behaviour.

:return: a pointer to the internal UDSP.

.. cpp:function:: template <typename Archive> void save(Archive &ar, unsigned) const
.. cpp:function:: template <typename Archive> void load(Archive &ar, unsigned)

Expand Down
38 changes: 38 additions & 0 deletions doc/sphinx/docs/cpp/topology.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,44 @@ Topology

:return: ``false`` if *this* was moved from, ``true`` otherwise.

.. cpp:function:: std::type_index get_type_index() const

.. versionadded:: 2.15

Get the type of the UDT.

This function will return the type
of the UDT stored within this :cpp:class:`~pagmo::topology`
instance.

:return: the type of the UDT.

.. cpp:function:: const void *get_ptr() const
.. cpp:function:: void *get_ptr()

.. versionadded:: 2.15

Get a pointer to the UDT.

These functions will return a raw (const) pointer
to the internal UDT instance. Differently from
the :cpp:func:`~pagmo::topology::extract()` overloads, these functions
do not require to pass the correct type
in input. It is however the user's responsibility
to cast the returned void pointer to the correct type.

.. note::

The returned value is a raw non-owning pointer: the lifetime of the pointee is tied to the lifetime
of ``this``, and ``delete`` must never be called on the pointer.

.. note::

The ability to extract a mutable pointer is provided only in order to allow to call non-const
methods on the internal UDT instance. Assigning a new UDT via this pointer is undefined behaviour.

:return: a pointer to the internal UDT.

.. cpp:function:: template <typename Archive> void save(Archive &ar, unsigned) const
.. cpp:function:: template <typename Archive> void load(Archive &ar, unsigned)

Expand Down
1 change: 1 addition & 0 deletions doc/sphinx/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ to the channels, and then we can immediately install pagmo:
.. code-block:: console
$ conda config --add channels conda-forge
$ conda config --set channel_priority strict
$ conda install pagmo pagmo-devel
The conda packages for pagmo are maintained by the core development team,
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinx/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Exponential Evolution Strategies (xNES) :cpp:class:`pagmo::xn
Non-dominated Sorting GA (NSGA2) :cpp:class:`pagmo::nsga2` M-U-I
Multi-objective EA vith Decomposition (MOEA/D) :cpp:class:`pagmo::moead` M-U
Multi-objective Hypervolume-based ACO (MHACO) :cpp:class:`pagmo::maco` M-U-I
Non-dominated Sorting PSO (NSPSO) :cpp:class:`pagmo::nspso` M-U-I
Non-dominated Sorting PSO (NSPSO) :cpp:class:`pagmo::nspso` M-U
========================================================== ========================================= =========================

Local optimization
Expand Down
Loading

0 comments on commit 576607b

Please sign in to comment.