|
| 1 | +/* |
| 2 | + * Software License Agreement (BSD License) |
| 3 | + * |
| 4 | + * Copyright (c) 2025, Multi-robot Systems (MRS) group at Czech Technical University in Prague |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * * Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#ifndef CLASS_LOADER__INTERFACE_TRAITS_HPP_ |
| 33 | +#define CLASS_LOADER__INTERFACE_TRAITS_HPP_ |
| 34 | + |
| 35 | +#include <type_traits> |
| 36 | + |
| 37 | +namespace class_loader |
| 38 | +{ |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Customization point that allows setting additional properties of |
| 42 | + * an interface class. |
| 43 | + * |
| 44 | + * @tparam T Base class for which the traits are defined |
| 45 | + * |
| 46 | + * Users can crete specializations of this class for the base type for which |
| 47 | + * the properties should be set. Properties are optional and therefore might not |
| 48 | + * exist in a given specialization. To access these properties, use their |
| 49 | + * respective accessor traits which provide the default value when it is not |
| 50 | + * specified. |
| 51 | + * |
| 52 | + * Supported properties: |
| 53 | + * - `constructor_signature` (member type) |
| 54 | + * - accessor: interface_constructor_signature |
| 55 | + * - default: T() |
| 56 | + * - defines the signature with which will the class loader instantiate the |
| 57 | + * derived classes |
| 58 | + * - example: |
| 59 | + * \code |
| 60 | + * using constructor_signature = T(double, int); |
| 61 | + * \endcode |
| 62 | + * defines that derived classes of class T (replace with the base class type |
| 63 | + * used in this specialization) will be instantiated with `double` and `int` |
| 64 | + * as their constructor arguments |
| 65 | + * |
| 66 | + * Example: |
| 67 | + * \code |
| 68 | + * class BaseWithInterfaceCtor |
| 69 | + * { |
| 70 | + * public: |
| 71 | + * // constructor parameters for the base class do not need to match the derived classes |
| 72 | + * explicit BaseWithInterfaceCtor(std::string) {} |
| 73 | + * virtual ~BaseWithInterfaceCtor() = default; |
| 74 | + * |
| 75 | + * virtual int get_number() = 0; |
| 76 | + * }; |
| 77 | + * |
| 78 | + * // Specialize the class_loader::InterfaceTraits struct to define properties of your interface |
| 79 | + * template<> |
| 80 | + * struct class_loader::InterfaceTraits<BaseWithInterfaceCtor> |
| 81 | + * { |
| 82 | + * // derived classes will be instantiated with two parameters (string and unique_ptr<int>) |
| 83 | + * using constructor_signature = BaseWithInterfaceCtor(std::string, std::unique_ptr<int>); |
| 84 | + * }; |
| 85 | + * \endcode |
| 86 | + */ |
| 87 | +template<class T> |
| 88 | +struct InterfaceTraits |
| 89 | +{ |
| 90 | +}; |
| 91 | + |
| 92 | +namespace impl |
| 93 | +{ |
| 94 | + |
| 95 | +template<class T, class = void> |
| 96 | +struct interface_constructor_signature_impl |
| 97 | +{ |
| 98 | + using type = T(); |
| 99 | +}; |
| 100 | + |
| 101 | +template<class T> |
| 102 | +struct interface_constructor_signature_impl<T, |
| 103 | + std::void_t<typename InterfaceTraits<T>::constructor_signature>> |
| 104 | +{ |
| 105 | + using type = typename InterfaceTraits<T>::constructor_signature; |
| 106 | +}; |
| 107 | + |
| 108 | +} // namespace impl |
| 109 | + |
| 110 | +/** |
| 111 | + * @brief Type trait for extracting the `constructor_signature` of |
| 112 | + * InterfaceTraits. |
| 113 | + * |
| 114 | + * @tparam T same as in InterfaceTraits |
| 115 | + * |
| 116 | + * Helper type:\n |
| 117 | + * @ref interface_constructor_signature_t<T> = interface_constructor_signature<T>::type; |
| 118 | + */ |
| 119 | +template<class T> |
| 120 | +struct interface_constructor_signature |
| 121 | +{ |
| 122 | + /** |
| 123 | + * @brief InterfaceTraits<T>::constructor_signature if present, otherwise the |
| 124 | + * default value (see InterfaceTraits). |
| 125 | + */ |
| 126 | + using type = typename impl::interface_constructor_signature_impl<T>::type; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * @brief Helper type alias @ref interface_constructor_signature<T>::type |
| 131 | + * @see interface_constructor_signature |
| 132 | + */ |
| 133 | +template<class T> |
| 134 | +using interface_constructor_signature_t = |
| 135 | + typename interface_constructor_signature<T>::type; |
| 136 | + |
| 137 | +/** |
| 138 | + * @brief Type trait for checking whether plugins derived from T can be |
| 139 | + * constructed with specified arguments. |
| 140 | + * |
| 141 | + * @tparam Base same as in InterfaceTraits |
| 142 | + * @tparam Args list of arguments to check |
| 143 | + * |
| 144 | + * Contains static constexpr member variable `value` that is true if plugins |
| 145 | + * derived from `T` can be constructed using `Args`, false otherwise. |
| 146 | + * |
| 147 | + * Helper variable template:\n |
| 148 | + * @ref is_interface_constructible_v<T, Args...> = |
| 149 | + * @ref is_interface_constructible "is_interface_constructible<T, Args...>::value" |
| 150 | + */ |
| 151 | +template<class Base, class ... Args> |
| 152 | +struct is_interface_constructible |
| 153 | + : std::is_invocable<interface_constructor_signature_t<Base>, Args...> |
| 154 | +{ |
| 155 | +}; |
| 156 | + |
| 157 | +/** |
| 158 | + * @brief Helper variable template for @ref is_interface_constructible "is_interface_constructible<T, Args...>::value" |
| 159 | + * @see is_interface_constructible |
| 160 | + */ |
| 161 | +template<class Base, class ... Args> |
| 162 | +constexpr bool is_interface_constructible_v = |
| 163 | + is_interface_constructible<Base, Args...>::value; |
| 164 | + |
| 165 | +} // namespace class_loader |
| 166 | + |
| 167 | +#endif // CLASS_LOADER__INTERFACE_TRAITS_HPP_ |
0 commit comments