From 3041617260c7d240f0b9416cc009735f0b0cf41c Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Wed, 28 Apr 2021 22:39:20 +0200 Subject: [PATCH 01/15] (cpp17) fixed compile issue when using C++17 standard. --- CMakeLists.txt | 4 ++-- .../graphs-tools/numerical/detail/execDependenciesImpl.h | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f719548..2914cc4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,8 +140,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF) -# Setting C++ standard to C++11 -set(CMAKE_CXX_STANDARD 11) +# Setting C++ standard to C++17 +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # We use diff --git a/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h b/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h index e039c5c6..dd8d9718 100644 --- a/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h +++ b/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h @@ -95,7 +95,9 @@ namespace spider { template inline void apply(const DependencyInfo &dep, const Function &func, Args &&...args) { - func(dep, std::forward(args)...); + if constexpr(std::is_invocable_r_v) { + func(dep, std::forward(args)...); + } } template @@ -154,7 +156,7 @@ namespace spider { const auto uCons = ifSrcRate + end; count += computeExecDependency(innerEdge, lCons, uCons, ghdl, std::forward(args)...); } else { - apply(unresolved, std::forward(args)...); + impl::apply(unresolved, std::forward(args)...); } } return count; From 5d76c4e057d1702b97fd6a0216fbb87a00ea70a9 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:18:31 +0200 Subject: [PATCH 02/15] (cpp17) removing variant.h library as there is std::variant in cpp17 --- libspider/extra/variant.h | 2847 -------------------------------- libspider/graphs/pisdf/Param.h | 42 +- libspider/memory/memory.cpp | 8 + 3 files changed, 29 insertions(+), 2868 deletions(-) delete mode 100644 libspider/extra/variant.h diff --git a/libspider/extra/variant.h b/libspider/extra/variant.h deleted file mode 100644 index 8d26b2f5..00000000 --- a/libspider/extra/variant.h +++ /dev/null @@ -1,2847 +0,0 @@ -/** - * Copyright or © or Copr. IETR/INSA - Rennes (2020) : - * - * Florian Arrestier (2020) - * - * Spider 2.0 is a dataflow based runtime used to execute dynamic PiSDF - * applications. The Preesm tool may be used to design PiSDF applications. - * - * This software is governed by the CeCILL license under French law and - * abiding by the rules of distribution of free software. You can use, - * modify and/ or redistribute the software under the terms of the CeCILL - * license as circulated by CEA, CNRS and INRIA at the following URL - * "http://www.cecill.info". - * - * As a counterpart to the access to the source code and rights to copy, - * modify and redistribute granted by the license, users are provided only - * with a limited warranty and the software's author, the holder of the - * economic rights, and the successive licensors have only limited - * liability. - * - * In this respect, the user's attention is drawn to the risks associated - * with loading, using, modifying and/or developing or reproducing the - * software by the user in light of its specific status of free software, - * that may mean that it is complicated to manipulate, and that also - * therefore means that it is reserved for developers and experienced - * professionals having in-depth computer knowledge. Users are therefore - * encouraged to load and test the software's suitability as regards their - * requirements in conditions enabling the security of their systems and/or - * data to be ensured and, more generally, to use and operate it in the - * same conditions as regards security. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL license and that you accept its terms. - */ -// MPark.Variant -// -// Copyright Michael Park, 2015-2017 -// see: https://github.com/mpark/variant/tree/single-header/v1.4.0 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - -#ifndef MPARK_VARIANT_HPP -#define MPARK_VARIANT_HPP - -/* - variant synopsis - -namespace std { - - // 20.7.2, class template variant - template - class variant { - public: - - // 20.7.2.1, constructors - constexpr variant() noexcept(see below); - variant(const variant&); - variant(variant&&) noexcept(see below); - - template constexpr variant(T&&) noexcept(see below); - - template - constexpr explicit variant(in_place_type_t, Args&&...); - - template - constexpr explicit variant( - in_place_type_t, initializer_list, Args&&...); - - template - constexpr explicit variant(in_place_index_t, Args&&...); - - template - constexpr explicit variant( - in_place_index_t, initializer_list, Args&&...); - - // 20.7.2.2, destructor - ~variant(); - - // 20.7.2.3, assignment - variant& operator=(const variant&); - variant& operator=(variant&&) noexcept(see below); - - template variant& operator=(T&&) noexcept(see below); - - // 20.7.2.4, modifiers - template - T& emplace(Args&&...); - - template - T& emplace(initializer_list, Args&&...); - - template - variant_alternative& emplace(Args&&...); - - template - variant_alternative& emplace(initializer_list, Args&&...); - - // 20.7.2.5, value status - constexpr bool valueless_by_exception() const noexcept; - constexpr size_t index() const noexcept; - - // 20.7.2.6, swap - void swap(variant&) noexcept(see below); - }; - - // 20.7.3, variant helper classes - template struct variant_size; // undefined - - template - constexpr size_t variant_size_v = variant_size::value; - - template struct variant_size; - template struct variant_size; - template struct variant_size; - - template - struct variant_size>; - - template struct variant_alternative; // undefined - - template - using variant_alternative_t = typename variant_alternative::type; - - template struct variant_alternative; - template struct variant_alternative; - template struct variant_alternative; - - template - struct variant_alternative>; - - constexpr size_t variant_npos = -1; - - // 20.7.4, value access - template - constexpr bool holds_alternative(const variant&) noexcept; - - template - constexpr variant_alternative_t>& - get(variant&); - - template - constexpr variant_alternative_t>&& - get(variant&&); - - template - constexpr variant_alternative_t> const& - get(const variant&); - - template - constexpr variant_alternative_t> const&& - get(const variant&&); - - template - constexpr T& get(variant&); - - template - constexpr T&& get(variant&&); - - template - constexpr const T& get(const variant&); - - template - constexpr const T&& get(const variant&&); - - template - constexpr add_pointer_t>> - get_if(variant*) noexcept; - - template - constexpr add_pointer_t>> - get_if(const variant*) noexcept; - - template - constexpr add_pointer_t - get_if(variant*) noexcept; - - template - constexpr add_pointer_t - get_if(const variant*) noexcept; - - // 20.7.5, relational operators - template - constexpr bool operator==(const variant&, const variant&); - - template - constexpr bool operator!=(const variant&, const variant&); - - template - constexpr bool operator<(const variant&, const variant&); - - template - constexpr bool operator>(const variant&, const variant&); - - template - constexpr bool operator<=(const variant&, const variant&); - - template - constexpr bool operator>=(const variant&, const variant&); - - // 20.7.6, visitation - template - constexpr see below visit(Visitor&&, Variants&&...); - - // 20.7.7, class monostate - struct monostate; - - // 20.7.8, monostate relational operators - constexpr bool operator<(monostate, monostate) noexcept; - constexpr bool operator>(monostate, monostate) noexcept; - constexpr bool operator<=(monostate, monostate) noexcept; - constexpr bool operator>=(monostate, monostate) noexcept; - constexpr bool operator==(monostate, monostate) noexcept; - constexpr bool operator!=(monostate, monostate) noexcept; - - // 20.7.9, specialized algorithms - template - void swap(variant&, variant&) noexcept(see below); - - // 20.7.10, class bad_variant_access - class bad_variant_access; - - // 20.7.11, hash support - template struct hash; - template struct hash>; - template <> struct hash; - -} // namespace std - -*/ - -#include -#include -#include -#include -#include -#include -#include - -// MPark.Variant -// -// Copyright Michael Park, 2015-2017 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - -#ifndef MPARK_CONFIG_HPP -#define MPARK_CONFIG_HPP - -// MSVC 2015 Update 3. -#if __cplusplus < 201103L && (!defined(_MSC_VER) || _MSC_FULL_VER < 190024210) -#error "MPark.Variant requires C++11 support." -#endif - -#ifndef __has_attribute -#define __has_attribute(x) 0 -#endif - -#ifndef __has_builtin -#define __has_builtin(x) 0 -#endif - -#ifndef __has_include -#define __has_include(x) 0 -#endif - -#ifndef __has_feature -#define __has_feature(x) 0 -#endif - -#if __has_attribute(always_inline) || defined(__GNUC__) -#define MPARK_ALWAYS_INLINE __attribute__((__always_inline__)) inline -#elif defined(_MSC_VER) -#define MPARK_ALWAYS_INLINE __forceinline -#else -#define MPARK_ALWAYS_INLINE inline -#endif - -#if __has_builtin(__builtin_addressof) || \ - (defined(__GNUC__) && __GNUC__ >= 7) || defined(_MSC_VER) -#define MPARK_BUILTIN_ADDRESSOF -#endif - -#if __has_builtin(__builtin_unreachable) || defined(__GNUC__) -#define MPARK_BUILTIN_UNREACHABLE __builtin_unreachable() -#elif defined(_MSC_VER) -#define MPARK_BUILTIN_UNREACHABLE __assume(false) -#else -#define MPARK_BUILTIN_UNREACHABLE -#endif - -#if __has_builtin(__type_pack_element) -#define MPARK_TYPE_PACK_ELEMENT -#endif - -#if defined(__cpp_constexpr) && __cpp_constexpr >= 200704 && \ - !(defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 9) -#define MPARK_CPP11_CONSTEXPR -#endif - -#if defined(__cpp_constexpr) && __cpp_constexpr >= 201304 -#define MPARK_CPP14_CONSTEXPR -#endif - -#if __has_feature(cxx_exceptions) || defined(__cpp_exceptions) || \ - (defined(_MSC_VER) && defined(_CPPUNWIND)) -#define MPARK_EXCEPTIONS -#endif - -#if defined(__cpp_generic_lambdas) || defined(_MSC_VER) -#define MPARK_GENERIC_LAMBDAS -#endif - -#if defined(__cpp_lib_integer_sequence) -#define MPARK_INTEGER_SEQUENCE -#endif - -#if defined(__cpp_return_type_deduction) || defined(_MSC_VER) -#define MPARK_RETURN_TYPE_DEDUCTION -#endif - -#if defined(__cpp_lib_transparent_operators) || defined(_MSC_VER) -#define MPARK_TRANSPARENT_OPERATORS -#endif - -#if defined(__cpp_variable_templates) || defined(_MSC_VER) -#define MPARK_VARIABLE_TEMPLATES -#endif - -#if !defined(__GLIBCXX__) || __has_include() // >= libstdc++-5 -#define MPARK_TRIVIALITY_TYPE_TRAITS -#define MPARK_INCOMPLETE_TYPE_TRAITS -#endif - -#endif // MPARK_CONFIG_HPP - -// MPark.Variant -// -// Copyright Michael Park, 2015-2017 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - -#ifndef MPARK_IN_PLACE_HPP -#define MPARK_IN_PLACE_HPP - -#include - - -namespace mpark { - - struct in_place_t { explicit in_place_t() = default; }; - - template - struct in_place_index_t { explicit in_place_index_t() = default; }; - - template - struct in_place_type_t { explicit in_place_type_t() = default; }; - -#ifdef MPARK_VARIABLE_TEMPLATES - constexpr in_place_t in_place{}; - - template constexpr in_place_index_t in_place_index{}; - - template constexpr in_place_type_t in_place_type{}; -#endif - -} // namespace mpark - -#endif // MPARK_IN_PLACE_HPP - -// MPark.Variant -// -// Copyright Michael Park, 2015-2017 -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) - -#ifndef MPARK_LIB_HPP -#define MPARK_LIB_HPP - -#include -#include -#include -#include - - -#define MPARK_RETURN(...) \ - noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { return __VA_ARGS__; } - -namespace mpark { - namespace lib { - template - struct identity { using type = T; }; - - inline namespace cpp14 { - template - struct array { - constexpr const T &operator[](std::size_t index) const { - return data[index]; - } - - T data[N == 0 ? 1 : N]; - }; - - template - using add_pointer_t = typename std::add_pointer::type; - - template - using common_type_t = typename std::common_type::type; - - template - using decay_t = typename std::decay::type; - - template - using enable_if_t = typename std::enable_if::type; - - template - using remove_const_t = typename std::remove_const::type; - - template - using remove_reference_t = typename std::remove_reference::type; - - template - inline constexpr T &&forward(remove_reference_t &t) noexcept { - return static_cast(t); - } - - template - inline constexpr T &&forward(remove_reference_t &&t) noexcept { - static_assert(!std::is_lvalue_reference::value, - "can not forward an rvalue as an lvalue"); - return static_cast(t); - } - - template - inline constexpr remove_reference_t &&move(T &&t) noexcept { - return static_cast &&>(t); - } - -#ifdef MPARK_INTEGER_SEQUENCE - using std::integer_sequence; - using std::index_sequence; - using std::make_index_sequence; - using std::index_sequence_for; -#else - template - struct integer_sequence { - using value_type = T; - static constexpr std::size_t size() noexcept { return sizeof...(Is); } - }; - - template - using index_sequence = integer_sequence; - - template - struct make_index_sequence_concat; - - template - struct make_index_sequence_concat, - index_sequence> - : identity> {}; - - template - struct make_index_sequence_impl; - - template - using make_index_sequence = typename make_index_sequence_impl::type; - - template - struct make_index_sequence_impl - : make_index_sequence_concat, - make_index_sequence> {}; - - template <> - struct make_index_sequence_impl<0> : identity> {}; - - template <> - struct make_index_sequence_impl<1> : identity> {}; - - template - using index_sequence_for = make_index_sequence; -#endif - - // -#ifdef MPARK_TRANSPARENT_OPERATORS - using equal_to = std::equal_to<>; -#else - struct equal_to { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) == lib::forward(rhs)) - }; -#endif - -#ifdef MPARK_TRANSPARENT_OPERATORS - using not_equal_to = std::not_equal_to<>; -#else - struct not_equal_to { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) != lib::forward(rhs)) - }; -#endif - -#ifdef MPARK_TRANSPARENT_OPERATORS - using less = std::less<>; -#else - struct less { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) < lib::forward(rhs)) - }; -#endif - -#ifdef MPARK_TRANSPARENT_OPERATORS - using greater = std::greater<>; -#else - struct greater { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) > lib::forward(rhs)) - }; -#endif - -#ifdef MPARK_TRANSPARENT_OPERATORS - using less_equal = std::less_equal<>; -#else - struct less_equal { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) <= lib::forward(rhs)) - }; -#endif - -#ifdef MPARK_TRANSPARENT_OPERATORS - using greater_equal = std::greater_equal<>; -#else - struct greater_equal { - template - inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const - MPARK_RETURN(lib::forward(lhs) >= lib::forward(rhs)) - }; -#endif - } // namespace cpp14 - - inline namespace cpp17 { - - // - template - using bool_constant = std::integral_constant; - - template - struct voider : identity {}; - - template - using void_t = typename voider::type; - - namespace detail { - namespace swappable { - - using std::swap; - - template - struct is_swappable { - private: - template (), - std::declval()))> - inline static std::true_type test(int); - - template - inline static std::false_type test(...); - - public: - static constexpr bool value = decltype(test(0))::value; - }; - - template - struct is_nothrow_swappable { - static constexpr bool value = - noexcept(swap(std::declval(), std::declval())); - }; - - template - struct is_nothrow_swappable : std::false_type {}; - - } // namespace swappable - } // namespace detail - - using detail::swappable::is_swappable; - - template - using is_nothrow_swappable = - detail::swappable::is_nothrow_swappable::value, T>; - - // - namespace detail { - - template - struct is_reference_wrapper : std::false_type {}; - - template - struct is_reference_wrapper> - : std::true_type {}; - - template - struct Invoke; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args) - MPARK_RETURN((lib::forward(arg).*pmf)(lib::forward(args)...)) - }; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args) - MPARK_RETURN((lib::forward(arg).get().*pmf)(lib::forward(args)...)) - }; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args) - MPARK_RETURN(((*lib::forward(arg)).*pmf)(lib::forward(args)...)) - }; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmo, Arg &&arg) - MPARK_RETURN(lib::forward(arg).*pmo) - }; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmo, Arg &&arg) - MPARK_RETURN(lib::forward(arg).get().*pmo) - }; - - template <> - struct Invoke { - template - inline static constexpr auto invoke(R T::*pmo, Arg &&arg) - MPARK_RETURN((*lib::forward(arg)).*pmo) - }; - - template - inline constexpr auto invoke(R T::*f, Arg &&arg, Args &&... args) - MPARK_RETURN( - Invoke::value, - (std::is_base_of>::value - ? 0 - : is_reference_wrapper>::value - ? 1 - : 2)>::invoke(f, - lib::forward(arg), - lib::forward(args)...)) - -#ifdef _MSC_VER - #pragma warning(push) -#pragma warning(disable : 4100) -#endif - template - inline constexpr auto invoke(F &&f, Args &&... args) - MPARK_RETURN(lib::forward(f)(lib::forward(args)...)) -#ifdef _MSC_VER -#pragma warning(pop) -#endif - } // namespace detail - - template - inline constexpr auto invoke(F &&f, Args &&... args) - MPARK_RETURN(detail::invoke(lib::forward(f), - lib::forward(args)...)) - - namespace detail { - - template - struct invoke_result {}; - - template - struct invoke_result(), std::declval()...))>, - F, - Args...> - : identity(), std::declval()...))> {}; - - } // namespace detail - - template - using invoke_result = detail::invoke_result; - - template - using invoke_result_t = typename invoke_result::type; - - namespace detail { - - template - struct is_invocable : std::false_type {}; - - template - struct is_invocable>, F, Args...> - : std::true_type {}; - - template - struct is_invocable_r : std::false_type {}; - - template - struct is_invocable_r>, - R, - F, - Args...> - : std::is_convertible, R> {}; - - } // namespace detail - - template - using is_invocable = detail::is_invocable; - - template - using is_invocable_r = detail::is_invocable_r; - - namespace detail { - - template - struct is_nothrow_invocable { - static constexpr bool value = - noexcept(lib::invoke(std::declval(), std::declval()...)); - }; - - template - struct is_nothrow_invocable : std::false_type {}; - - template - struct is_nothrow_invocable_r { - private: - inline static R impl() { - return lib::invoke(std::declval(), std::declval()...); - } - - public: - static constexpr bool value = noexcept(impl()); - }; - - template - struct is_nothrow_invocable_r : std::false_type {}; - - } // namespace detail - - template - using is_nothrow_invocable = detail:: - is_nothrow_invocable::value, F, Args...>; - - template - using is_nothrow_invocable_r = - detail::is_nothrow_invocable_r::value, - R, - F, - Args...>; - - // -#ifdef MPARK_BUILTIN_ADDRESSOF - template - inline constexpr T *addressof(T &arg) noexcept { - return __builtin_addressof(arg); - } -#else - namespace detail { - - namespace has_addressof_impl { - - struct fail; - - template - inline fail operator&(T &&); - - template - inline static constexpr bool impl() { - return (std::is_class::value || std::is_union::value) && - !std::is_same()), fail>::value; - } - - } // namespace has_addressof_impl - - template - using has_addressof = bool_constant()>; - - template - inline constexpr T *addressof(T &arg, std::true_type) noexcept { - return std::addressof(arg); - } - - template - inline constexpr T *addressof(T &arg, std::false_type) noexcept { - return &arg; - } - - } // namespace detail - - template - inline constexpr T *addressof(T &arg) noexcept { - return detail::addressof(arg, detail::has_addressof{}); - } -#endif - - template - inline constexpr T *addressof(const T &&) = delete; - - } // namespace cpp17 - - template - struct remove_all_extents : identity {}; - - template - struct remove_all_extents> : remove_all_extents {}; - - template - using remove_all_extents_t = typename remove_all_extents::type; - - template - using size_constant = std::integral_constant; - - template - struct indexed_type : size_constant { using type = T; }; - - template - using all = std::is_same, - integer_sequence>; - -#ifdef MPARK_TYPE_PACK_ELEMENT - template - using type_pack_element_t = __type_pack_element; -#else - template - struct type_pack_element_impl { - private: - template - struct set; - - template - struct set> : indexed_type... {}; - - template - inline static std::enable_if impl(indexed_type); - - inline static std::enable_if impl(...); - - public: - using type = decltype(impl(set>{})); - }; - - template - using type_pack_element = typename type_pack_element_impl::type; - - template - using type_pack_element_t = typename type_pack_element::type; -#endif - -#ifdef MPARK_TRIVIALITY_TYPE_TRAITS - using std::is_trivially_copy_constructible; - using std::is_trivially_move_constructible; - using std::is_trivially_copy_assignable; - using std::is_trivially_move_assignable; -#else - template - struct is_trivially_copy_constructible - : bool_constant< - std::is_copy_constructible::value && __has_trivial_copy(T)> {}; - - template - struct is_trivially_move_constructible : bool_constant<__is_trivial(T)> {}; - - template - struct is_trivially_copy_assignable - : bool_constant< - std::is_copy_assignable::value && __has_trivial_assign(T)> {}; - - template - struct is_trivially_move_assignable : bool_constant<__is_trivial(T)> {}; -#endif - - template - struct dependent_type : T {}; - - template - struct push_back; - - template - using push_back_t = typename push_back::type; - - template - struct push_back, J> { - using type = index_sequence; - }; - - } // namespace lib -} // namespace mpark - -#undef MPARK_RETURN - -#endif // MPARK_LIB_HPP - - -namespace mpark { - -#ifdef MPARK_RETURN_TYPE_DEDUCTION - - #define AUTO auto -#define AUTO_RETURN(...) { return __VA_ARGS__; } - -#define AUTO_REFREF auto && -#define AUTO_REFREF_RETURN(...) { return __VA_ARGS__; } - -#define DECLTYPE_AUTO decltype(auto) -#define DECLTYPE_AUTO_RETURN(...) { return __VA_ARGS__; } - -#else - -#define AUTO auto -#define AUTO_RETURN(...) \ - -> lib::decay_t { return __VA_ARGS__; } - -#define AUTO_REFREF auto -#define AUTO_REFREF_RETURN(...) \ - -> decltype((__VA_ARGS__)) { \ - static_assert(std::is_reference::value, ""); \ - return __VA_ARGS__; \ - } - -#define DECLTYPE_AUTO auto -#define DECLTYPE_AUTO_RETURN(...) \ - -> decltype(__VA_ARGS__) { return __VA_ARGS__; } - -#endif - - class bad_variant_access : public std::exception { - public: - virtual const char *what() const noexcept override { return "bad_variant_access"; } - }; - - [[noreturn]] inline void throw_bad_variant_access() { -#ifdef MPARK_EXCEPTIONS - throw bad_variant_access{}; -#else - std::terminate(); - MPARK_BUILTIN_UNREACHABLE; -#endif - } - - template - class variant; - - template - struct variant_size; - -#ifdef MPARK_VARIABLE_TEMPLATES - template - constexpr std::size_t variant_size_v = variant_size::value; -#endif - - template - struct variant_size : variant_size {}; - - template - struct variant_size : variant_size {}; - - template - struct variant_size : variant_size {}; - - template - struct variant_size> : lib::size_constant {}; - - template - struct variant_alternative; - - template - using variant_alternative_t = typename variant_alternative::type; - - template - struct variant_alternative - : std::add_const> {}; - - template - struct variant_alternative - : std::add_volatile> {}; - - template - struct variant_alternative - : std::add_cv> {}; - - template - struct variant_alternative> { - static_assert(I < sizeof...(Ts), - "index out of bounds in `std::variant_alternative<>`"); - using type = lib::type_pack_element_t; - }; - - constexpr std::size_t variant_npos = static_cast(-1); - - namespace detail { - - constexpr std::size_t not_found = static_cast(-1); - constexpr std::size_t ambiguous = static_cast(-2); - -#ifdef MPARK_CPP14_CONSTEXPR - template - inline constexpr std::size_t find_index() { - constexpr lib::array matches = { - {std::is_same::value...} - }; - std::size_t result = not_found; - for (std::size_t i = 0; i < sizeof...(Ts); ++i) { - if (matches[i]) { - if (result != not_found) { - return ambiguous; - } - result = i; - } - } - return result; - } -#else - inline constexpr std::size_t find_index_impl(std::size_t result, - std::size_t) { - return result; - } - - template - inline constexpr std::size_t find_index_impl(std::size_t result, - std::size_t idx, - bool b, - Bs... bs) { - return b ? (result != not_found ? ambiguous - : find_index_impl(idx, idx + 1, bs...)) - : find_index_impl(result, idx + 1, bs...); - } - - template - inline constexpr std::size_t find_index() { - return find_index_impl(not_found, 0, std::is_same::value...); - } -#endif - - template - using find_index_sfinae_impl = - lib::enable_if_t>; - - template - using find_index_sfinae = find_index_sfinae_impl()>; - - template - struct find_index_checked_impl : lib::size_constant { - static_assert(I != not_found, "the specified type is not found."); - static_assert(I != ambiguous, "the specified type is ambiguous."); - }; - - template - using find_index_checked = find_index_checked_impl()>; - - struct valueless_t {}; - - enum class Trait { TriviallyAvailable, Available, Unavailable }; - - template class IsTriviallyAvailable, - template class IsAvailable> - inline constexpr Trait trait() { - return IsTriviallyAvailable::value - ? Trait::TriviallyAvailable - : IsAvailable::value ? Trait::Available - : Trait::Unavailable; - } - -#ifdef MPARK_CPP14_CONSTEXPR - template - inline constexpr Trait common_trait(Traits... traits_) { - Trait result = Trait::TriviallyAvailable; - lib::array traits = {{traits_...}}; - for (std::size_t i = 0; i < sizeof...(Traits); ++i) { - Trait t = traits[i]; - if (static_cast(t) > static_cast(result)) { - result = t; - } - } - return result; - } -#else - inline constexpr Trait common_trait_impl(Trait result) { return result; } - - template - inline constexpr Trait common_trait_impl(Trait result, - Trait t, - Traits... ts) { - return static_cast(t) > static_cast(result) - ? common_trait_impl(t, ts...) - : common_trait_impl(result, ts...); - } - - template - inline constexpr Trait common_trait(Traits... ts) { - return common_trait_impl(Trait::TriviallyAvailable, ts...); - } -#endif - - template - struct traits { - static constexpr Trait copy_constructible_trait = - common_trait(trait()...); - - static constexpr Trait move_constructible_trait = - common_trait(trait()...); - - static constexpr Trait copy_assignable_trait = - common_trait(copy_constructible_trait, - trait()...); - - static constexpr Trait move_assignable_trait = - common_trait(move_constructible_trait, - trait()...); - - static constexpr Trait destructible_trait = - common_trait(trait()...); - }; - - namespace access { - - struct recursive_union { -#ifdef MPARK_RETURN_TYPE_DEDUCTION - template - inline static constexpr auto &&get_alt(V &&v, in_place_index_t<0>) { - return lib::forward(v).head_; - } - - template - inline static constexpr auto &&get_alt(V &&v, in_place_index_t) { - return get_alt(lib::forward(v).tail_, in_place_index_t{}); - } -#else - template - struct get_alt_impl { - template - inline constexpr AUTO_REFREF operator()(V &&v) const - AUTO_REFREF_RETURN(get_alt_impl{}(lib::forward(v).tail_)) - }; - - template - struct get_alt_impl<0, Dummy> { - template - inline constexpr AUTO_REFREF operator()(V &&v) const - AUTO_REFREF_RETURN(lib::forward(v).head_) - }; - - template - inline static constexpr AUTO_REFREF get_alt(V &&v, in_place_index_t) - AUTO_REFREF_RETURN(get_alt_impl{}(lib::forward(v))) -#endif - }; - - struct base { - template - inline static constexpr AUTO_REFREF get_alt(V &&v) -#ifdef _MSC_VER - AUTO_REFREF_RETURN(recursive_union::get_alt( - lib::forward(v).data_, in_place_index_t{})) -#else - AUTO_REFREF_RETURN(recursive_union::get_alt( - data(lib::forward(v)), in_place_index_t{})) -#endif - }; - - struct variant { - template - inline static constexpr AUTO_REFREF get_alt(V &&v) - AUTO_REFREF_RETURN(base::get_alt(lib::forward(v).impl_)) - }; - - } // namespace access - - namespace visitation { - -#if defined(MPARK_CPP14_CONSTEXPR) && !defined(_MSC_VER) -#define MPARK_VARIANT_SWITCH_VISIT -#endif - - struct base { - template - using dispatch_result_t = decltype( - lib::invoke(std::declval(), - access::base::get_alt<0>(std::declval())...)); - - template - struct expected { - template - inline static constexpr bool but_got() { - return std::is_same::value; - } - }; - - template - struct visit_return_type_check { - static_assert( - expected::template but_got(), - "`visit` requires the visitor to have a single return type"); - - template - inline static constexpr DECLTYPE_AUTO invoke(Visitor &&visitor, - Alts &&... alts) - DECLTYPE_AUTO_RETURN(lib::invoke(lib::forward(visitor), - lib::forward(alts)...)) - }; - -#ifdef MPARK_VARIANT_SWITCH_VISIT - template - struct dispatcher; - - template - struct dispatcher { - template - MPARK_ALWAYS_INLINE static constexpr R dispatch( - F &&, typename ITs::type &&..., Vs &&...) { - MPARK_BUILTIN_UNREACHABLE; - } - - template - MPARK_ALWAYS_INLINE static constexpr R dispatch_case(F &&, Vs &&...) { - MPARK_BUILTIN_UNREACHABLE; - } - - template - MPARK_ALWAYS_INLINE static constexpr R dispatch_at(std::size_t, - F &&, - Vs &&...) { - MPARK_BUILTIN_UNREACHABLE; - } - }; - - template - struct dispatcher { - template - MPARK_ALWAYS_INLINE static constexpr R dispatch( - F &&f, typename ITs::type &&... visited_vs) { - using Expected = R; - using Actual = decltype(lib::invoke( - lib::forward(f), - access::base::get_alt( - lib::forward(visited_vs))...)); - return visit_return_type_check::invoke( - lib::forward(f), - access::base::get_alt( - lib::forward(visited_vs))...); - } - - template - MPARK_ALWAYS_INLINE static constexpr R dispatch( - F &&f, typename ITs::type &&... visited_vs, V &&v, Vs &&... vs) { -#define MPARK_DISPATCH(I) \ - dispatcher<(I < lib::decay_t::size()), \ - R, \ - ITs..., \ - lib::indexed_type>:: \ - template dispatch<0>(lib::forward(f), \ - lib::forward(visited_vs)..., \ - lib::forward(v), \ - lib::forward(vs)...) - -#define MPARK_DEFAULT(I) \ - dispatcher<(I < lib::decay_t::size()), R, ITs...>::template dispatch( \ - lib::forward(f), \ - lib::forward(visited_vs)..., \ - lib::forward(v), \ - lib::forward(vs)...) - - switch (v.index()) { - case B + 0: return MPARK_DISPATCH(B + 0); - case B + 1: return MPARK_DISPATCH(B + 1); - case B + 2: return MPARK_DISPATCH(B + 2); - case B + 3: return MPARK_DISPATCH(B + 3); - case B + 4: return MPARK_DISPATCH(B + 4); - case B + 5: return MPARK_DISPATCH(B + 5); - case B + 6: return MPARK_DISPATCH(B + 6); - case B + 7: return MPARK_DISPATCH(B + 7); - case B + 8: return MPARK_DISPATCH(B + 8); - case B + 9: return MPARK_DISPATCH(B + 9); - case B + 10: return MPARK_DISPATCH(B + 10); - case B + 11: return MPARK_DISPATCH(B + 11); - case B + 12: return MPARK_DISPATCH(B + 12); - case B + 13: return MPARK_DISPATCH(B + 13); - case B + 14: return MPARK_DISPATCH(B + 14); - case B + 15: return MPARK_DISPATCH(B + 15); - case B + 16: return MPARK_DISPATCH(B + 16); - case B + 17: return MPARK_DISPATCH(B + 17); - case B + 18: return MPARK_DISPATCH(B + 18); - case B + 19: return MPARK_DISPATCH(B + 19); - case B + 20: return MPARK_DISPATCH(B + 20); - case B + 21: return MPARK_DISPATCH(B + 21); - case B + 22: return MPARK_DISPATCH(B + 22); - case B + 23: return MPARK_DISPATCH(B + 23); - case B + 24: return MPARK_DISPATCH(B + 24); - case B + 25: return MPARK_DISPATCH(B + 25); - case B + 26: return MPARK_DISPATCH(B + 26); - case B + 27: return MPARK_DISPATCH(B + 27); - case B + 28: return MPARK_DISPATCH(B + 28); - case B + 29: return MPARK_DISPATCH(B + 29); - case B + 30: return MPARK_DISPATCH(B + 30); - case B + 31: return MPARK_DISPATCH(B + 31); - default: return MPARK_DEFAULT(B + 32); - } - -#undef MPARK_DEFAULT -#undef MPARK_DISPATCH - } - - template - MPARK_ALWAYS_INLINE static constexpr R dispatch_case(F &&f, - Vs &&... vs) { - using Expected = R; - using Actual = decltype( - lib::invoke(lib::forward(f), - access::base::get_alt(lib::forward(vs))...)); - return visit_return_type_check::invoke( - lib::forward(f), - access::base::get_alt(lib::forward(vs))...); - } - - template - MPARK_ALWAYS_INLINE static constexpr R dispatch_at(std::size_t index, - F &&f, - V &&v, - Vs &&... vs) { - static_assert(lib::all<(lib::decay_t::size() == - lib::decay_t::size())...>::value, - "all of the variants must be the same size."); -#define MPARK_DISPATCH_AT(I) \ - dispatcher<(I < lib::decay_t::size()), R>::template dispatch_case( \ - lib::forward(f), lib::forward(v), lib::forward(vs)...) - -#define MPARK_DEFAULT(I) \ - dispatcher<(I < lib::decay_t::size()), R>::template dispatch_at( \ - index, lib::forward(f), lib::forward(v), lib::forward(vs)...) - - switch (index) { - case B + 0: return MPARK_DISPATCH_AT(B + 0); - case B + 1: return MPARK_DISPATCH_AT(B + 1); - case B + 2: return MPARK_DISPATCH_AT(B + 2); - case B + 3: return MPARK_DISPATCH_AT(B + 3); - case B + 4: return MPARK_DISPATCH_AT(B + 4); - case B + 5: return MPARK_DISPATCH_AT(B + 5); - case B + 6: return MPARK_DISPATCH_AT(B + 6); - case B + 7: return MPARK_DISPATCH_AT(B + 7); - case B + 8: return MPARK_DISPATCH_AT(B + 8); - case B + 9: return MPARK_DISPATCH_AT(B + 9); - case B + 10: return MPARK_DISPATCH_AT(B + 10); - case B + 11: return MPARK_DISPATCH_AT(B + 11); - case B + 12: return MPARK_DISPATCH_AT(B + 12); - case B + 13: return MPARK_DISPATCH_AT(B + 13); - case B + 14: return MPARK_DISPATCH_AT(B + 14); - case B + 15: return MPARK_DISPATCH_AT(B + 15); - case B + 16: return MPARK_DISPATCH_AT(B + 16); - case B + 17: return MPARK_DISPATCH_AT(B + 17); - case B + 18: return MPARK_DISPATCH_AT(B + 18); - case B + 19: return MPARK_DISPATCH_AT(B + 19); - case B + 20: return MPARK_DISPATCH_AT(B + 20); - case B + 21: return MPARK_DISPATCH_AT(B + 21); - case B + 22: return MPARK_DISPATCH_AT(B + 22); - case B + 23: return MPARK_DISPATCH_AT(B + 23); - case B + 24: return MPARK_DISPATCH_AT(B + 24); - case B + 25: return MPARK_DISPATCH_AT(B + 25); - case B + 26: return MPARK_DISPATCH_AT(B + 26); - case B + 27: return MPARK_DISPATCH_AT(B + 27); - case B + 28: return MPARK_DISPATCH_AT(B + 28); - case B + 29: return MPARK_DISPATCH_AT(B + 29); - case B + 30: return MPARK_DISPATCH_AT(B + 30); - case B + 31: return MPARK_DISPATCH_AT(B + 31); - default: return MPARK_DEFAULT(B + 32); - } - -#undef MPARK_DEFAULT -#undef MPARK_DISPATCH_AT - } - }; -#else - template - inline static constexpr const T &at(const T &elem) noexcept { - return elem; - } - - template - inline static constexpr const lib::remove_all_extents_t &at( - const lib::array &elems, std::size_t i, Is... is) noexcept { - return at(elems[i], is...); - } - - template - inline static constexpr lib::array, sizeof...(Fs) + 1> - make_farray(F &&f, Fs &&... fs) { - return {{lib::forward(f), lib::forward(fs)...}}; - } - - template - struct make_fmatrix_impl { - - template - inline static constexpr dispatch_result_t dispatch( - F &&f, Vs &&... vs) { - using Expected = dispatch_result_t; - using Actual = decltype(lib::invoke( - lib::forward(f), - access::base::get_alt(lib::forward(vs))...)); - return visit_return_type_check::invoke( - lib::forward(f), - access::base::get_alt(lib::forward(vs))...); - } - -#ifdef MPARK_RETURN_TYPE_DEDUCTION - template - inline static constexpr auto impl(lib::index_sequence) { - return &dispatch; - } - - template - inline static constexpr auto impl(Is, - lib::index_sequence, - Ls... ls) { - return make_farray(impl(lib::push_back_t{}, ls...)...); - } -#else - template - struct impl; - - template - struct impl> { - inline constexpr AUTO operator()() const - AUTO_RETURN(&dispatch) - }; - - template - struct impl, Ls...> { - inline constexpr AUTO operator()() const - AUTO_RETURN( - make_farray(impl, Ls...>{}()...)) - }; -#endif - }; - -#ifdef MPARK_RETURN_TYPE_DEDUCTION - template - inline static constexpr auto make_fmatrix() { - return make_fmatrix_impl::impl( - lib::index_sequence<>{}, - lib::make_index_sequence::size()>{}...); - } -#else - template - inline static constexpr AUTO make_fmatrix() - AUTO_RETURN( - typename make_fmatrix_impl::template impl< - lib::index_sequence<>, - lib::make_index_sequence::size()>...>{}()) -#endif - - template - struct make_fdiagonal_impl { - template - inline static constexpr dispatch_result_t dispatch( - F &&f, Vs &&... vs) { - using Expected = dispatch_result_t; - using Actual = decltype( - lib::invoke(lib::forward(f), - access::base::get_alt(lib::forward(vs))...)); - return visit_return_type_check::invoke( - lib::forward(f), - access::base::get_alt(lib::forward(vs))...); - } - - template - inline static constexpr AUTO impl(lib::index_sequence) - AUTO_RETURN(make_farray(&dispatch...)) - }; - - template - inline static constexpr auto make_fdiagonal() - -> decltype(make_fdiagonal_impl::impl( - lib::make_index_sequence::size()>{})) { - static_assert(lib::all<(lib::decay_t::size() == - lib::decay_t::size())...>::value, - "all of the variants must be the same size."); - return make_fdiagonal_impl::impl( - lib::make_index_sequence::size()>{}); - } -#endif - }; - -#if !defined(MPARK_VARIANT_SWITCH_VISIT) && \ - (!defined(_MSC_VER) || _MSC_VER >= 1910) - template - using fmatrix_t = decltype(base::make_fmatrix()); - - template - struct fmatrix { - static constexpr fmatrix_t value = - base::make_fmatrix(); - }; - - template - constexpr fmatrix_t fmatrix::value; - - template - using fdiagonal_t = decltype(base::make_fdiagonal()); - - template - struct fdiagonal { - static constexpr fdiagonal_t value = - base::make_fdiagonal(); - }; - - template - constexpr fdiagonal_t fdiagonal::value; -#endif - - struct alt { - template - inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor, - Vs &&... vs) -#ifdef MPARK_VARIANT_SWITCH_VISIT - DECLTYPE_AUTO_RETURN( - base::dispatcher< - true, - base::dispatch_result_t(vs)))...>>:: - template dispatch<0>(lib::forward(visitor), - as_base(lib::forward(vs))...)) -#elif !defined(_MSC_VER) || _MSC_VER >= 1910 - DECLTYPE_AUTO_RETURN(base::at( - fmatrix(vs)))...>::value, - vs.index()...)(lib::forward(visitor), - as_base(lib::forward(vs))...)) -#else - DECLTYPE_AUTO_RETURN(base::at( - base::make_fmatrix(vs)))...>(), - vs.index()...)(lib::forward(visitor), - as_base(lib::forward(vs))...)) -#endif - - template - inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index, - Visitor &&visitor, - Vs &&... vs) -#ifdef MPARK_VARIANT_SWITCH_VISIT - DECLTYPE_AUTO_RETURN( - base::dispatcher< - true, - base::dispatch_result_t(vs)))...>>:: - template dispatch_at<0>(index, - lib::forward(visitor), - as_base(lib::forward(vs))...)) -#elif !defined(_MSC_VER) || _MSC_VER >= 1910 - DECLTYPE_AUTO_RETURN(base::at( - fdiagonal(vs)))...>::value, - index)(lib::forward(visitor), - as_base(lib::forward(vs))...)) -#else - DECLTYPE_AUTO_RETURN(base::at( - base::make_fdiagonal(vs)))...>(), - index)(lib::forward(visitor), - as_base(lib::forward(vs))...)) -#endif - }; - - struct variant { - private: - template - struct visitor { - template - inline static constexpr bool does_not_handle() { - return lib::is_invocable::value; - } - }; - - template - struct visit_exhaustiveness_check { - static_assert(visitor::template does_not_handle(), - "`visit` requires the visitor to be exhaustive."); - - inline static constexpr DECLTYPE_AUTO invoke(Visitor &&visitor, - Values &&... values) - DECLTYPE_AUTO_RETURN(lib::invoke(lib::forward(visitor), - lib::forward(values)...)) - }; - - template - struct value_visitor { - Visitor &&visitor_; - - template - inline constexpr DECLTYPE_AUTO operator()(Alts &&... alts) const - DECLTYPE_AUTO_RETURN( - visit_exhaustiveness_check< - Visitor, - decltype((lib::forward(alts).value))...>:: - invoke(lib::forward(visitor_), - lib::forward(alts).value...)) - }; - - template - inline static constexpr AUTO make_value_visitor(Visitor &&visitor) - AUTO_RETURN(value_visitor{lib::forward(visitor)}) - - public: - template - inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor, - Vs &&... vs) - DECLTYPE_AUTO_RETURN(alt::visit_alt(lib::forward(visitor), - lib::forward(vs).impl_...)) - - template - inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index, - Visitor &&visitor, - Vs &&... vs) - DECLTYPE_AUTO_RETURN( - alt::visit_alt_at(index, - lib::forward(visitor), - lib::forward(vs).impl_...)) - - template - inline static constexpr DECLTYPE_AUTO visit_value(Visitor &&visitor, - Vs &&... vs) - DECLTYPE_AUTO_RETURN( - visit_alt(make_value_visitor(lib::forward(visitor)), - lib::forward(vs)...)) - - template - inline static constexpr DECLTYPE_AUTO visit_value_at(std::size_t index, - Visitor &&visitor, - Vs &&... vs) - DECLTYPE_AUTO_RETURN( - visit_alt_at(index, - make_value_visitor(lib::forward(visitor)), - lib::forward(vs)...)) - }; - - } // namespace visitation - - template - struct alt { - using value_type = T; - -#ifdef _MSC_VER - #pragma warning(push) -#pragma warning(disable : 4244) -#endif - template - inline explicit constexpr alt(in_place_t, Args &&... args) - : value(lib::forward(args)...) {} -#ifdef _MSC_VER -#pragma warning(pop) -#endif - - T value; - }; - - template - union recursive_union; - - template - union recursive_union {}; - -#define MPARK_VARIANT_RECURSIVE_UNION(destructible_trait, destructor) \ - template \ - union recursive_union { \ - public: \ - inline explicit constexpr recursive_union(valueless_t) noexcept \ - : dummy_{} {} \ - \ - template \ - inline explicit constexpr recursive_union(in_place_index_t<0>, \ - Args &&... args) \ - : head_(in_place_t{}, lib::forward(args)...) {} \ - \ - template \ - inline explicit constexpr recursive_union(in_place_index_t, \ - Args &&... args) \ - : tail_(in_place_index_t{}, lib::forward(args)...) {} \ - \ - recursive_union(const recursive_union &) = default; \ - recursive_union(recursive_union &&) = default; \ - \ - destructor \ - \ - recursive_union &operator=(const recursive_union &) = default; \ - recursive_union &operator=(recursive_union &&) = default; \ - \ - private: \ - char dummy_; \ - alt head_; \ - recursive_union tail_; \ - \ - friend struct access::recursive_union; \ - } - - MPARK_VARIANT_RECURSIVE_UNION(Trait::TriviallyAvailable, - ~recursive_union() = default;); - MPARK_VARIANT_RECURSIVE_UNION(Trait::Available, - ~recursive_union() {}); - MPARK_VARIANT_RECURSIVE_UNION(Trait::Unavailable, - ~recursive_union() = delete;); - -#undef MPARK_VARIANT_RECURSIVE_UNION - - using index_t = unsigned int; - - template - class base { - public: - inline explicit constexpr base(valueless_t tag) noexcept - : data_(tag), index_(static_cast(-1)) {} - - template - inline explicit constexpr base(in_place_index_t, Args &&... args) - : data_(in_place_index_t{}, lib::forward(args)...), - index_(I) {} - - inline constexpr bool valueless_by_exception() const noexcept { - return index_ == static_cast(-1); - } - - inline constexpr std::size_t index() const noexcept { - return valueless_by_exception() ? variant_npos : index_; - } - - protected: - using data_t = recursive_union; - - friend inline constexpr base &as_base(base &b) { return b; } - friend inline constexpr const base &as_base(const base &b) { return b; } - friend inline constexpr base &&as_base(base &&b) { return lib::move(b); } - friend inline constexpr const base &&as_base(const base &&b) { return lib::move(b); } - - friend inline constexpr data_t &data(base &b) { return b.data_; } - friend inline constexpr const data_t &data(const base &b) { return b.data_; } - friend inline constexpr data_t &&data(base &&b) { return lib::move(b).data_; } - friend inline constexpr const data_t &&data(const base &&b) { return lib::move(b).data_; } - - inline static constexpr std::size_t size() { return sizeof...(Ts); } - - data_t data_; - index_t index_; - - friend struct access::base; - friend struct visitation::base; - }; - - struct dtor { -#ifdef _MSC_VER - #pragma warning(push) -#pragma warning(disable : 4100) -#endif - template - inline void operator()(Alt &alt) const noexcept { alt.~Alt(); } -#ifdef _MSC_VER -#pragma warning(pop) -#endif - }; - -#if !defined(_MSC_VER) || _MSC_VER >= 1910 -#define MPARK_INHERITING_CTOR(type, base) using base::base; -#else - #define MPARK_INHERITING_CTOR(type, base) \ - template \ - inline explicit constexpr type(Args &&... args) \ - : base(lib::forward(args)...) {} -#endif - - template - class destructor; - -#define MPARK_VARIANT_DESTRUCTOR(destructible_trait, definition, destroy) \ - template \ - class destructor, destructible_trait> \ - : public base { \ - using super = base; \ - \ - public: \ - MPARK_INHERITING_CTOR(destructor, super) \ - using super::operator=; \ - \ - destructor(const destructor &) = default; \ - destructor(destructor &&) = default; \ - definition \ - destructor &operator=(const destructor &) = default; \ - destructor &operator=(destructor &&) = default; \ - \ - protected: \ - destroy \ - } - - MPARK_VARIANT_DESTRUCTOR( - Trait::TriviallyAvailable, - ~destructor() = default;, - inline void destroy() noexcept { - this->index_ = static_cast(-1); - }); - - MPARK_VARIANT_DESTRUCTOR( - Trait::Available, - ~destructor() { destroy(); }, - inline void destroy() noexcept { - if (!this->valueless_by_exception()) { - visitation::alt::visit_alt(dtor{}, *this); - } - this->index_ = static_cast(-1); - }); - - MPARK_VARIANT_DESTRUCTOR( - Trait::Unavailable, - ~destructor() = delete;, - inline void destroy() noexcept = delete;); - -#undef MPARK_VARIANT_DESTRUCTOR - - template - class constructor : public destructor { - using super = destructor; - - public: - MPARK_INHERITING_CTOR(constructor, super) - using super::operator=; - - protected: -#ifndef MPARK_GENERIC_LAMBDAS - struct ctor { - template - inline void operator()(LhsAlt &lhs_alt, RhsAlt &&rhs_alt) const { - constructor::construct_alt(lhs_alt, - lib::forward(rhs_alt).value); - } - }; -#endif - - template - inline static T &construct_alt(alt &a, Args &&... args) { - auto *result = ::new (static_cast(lib::addressof(a))) - alt(in_place_t{}, lib::forward(args)...); - return result->value; - } - - template - inline static void generic_construct(constructor &lhs, Rhs &&rhs) { - lhs.destroy(); - if (!rhs.valueless_by_exception()) { - visitation::alt::visit_alt_at( - rhs.index(), -#ifdef MPARK_GENERIC_LAMBDAS - [](auto &lhs_alt, auto &&rhs_alt) { - constructor::construct_alt( - lhs_alt, lib::forward(rhs_alt).value); - } -#else - ctor{} -#endif - , - lhs, - lib::forward(rhs)); - lhs.index_ = rhs.index_; - } - } - }; - - template - class move_constructor; - -#define MPARK_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, definition) \ - template \ - class move_constructor, move_constructible_trait> \ - : public constructor> { \ - using super = constructor>; \ - \ - public: \ - MPARK_INHERITING_CTOR(move_constructor, super) \ - using super::operator=; \ - \ - move_constructor(const move_constructor &) = default; \ - definition \ - ~move_constructor() = default; \ - move_constructor &operator=(const move_constructor &) = default; \ - move_constructor &operator=(move_constructor &&) = default; \ - } - - MPARK_VARIANT_MOVE_CONSTRUCTOR( - Trait::TriviallyAvailable, - move_constructor(move_constructor &&that) = default;); - - MPARK_VARIANT_MOVE_CONSTRUCTOR( - Trait::Available, - move_constructor(move_constructor &&that) noexcept( - lib::all::value...>::value) - : move_constructor(valueless_t{}) { - this->generic_construct(*this, lib::move(that)); - }); - - MPARK_VARIANT_MOVE_CONSTRUCTOR( - Trait::Unavailable, - move_constructor(move_constructor &&) = delete;); - -#undef MPARK_VARIANT_MOVE_CONSTRUCTOR - - template - class copy_constructor; - -#define MPARK_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, definition) \ - template \ - class copy_constructor, copy_constructible_trait> \ - : public move_constructor> { \ - using super = move_constructor>; \ - \ - public: \ - MPARK_INHERITING_CTOR(copy_constructor, super) \ - using super::operator=; \ - \ - definition \ - copy_constructor(copy_constructor &&) = default; \ - ~copy_constructor() = default; \ - copy_constructor &operator=(const copy_constructor &) = default; \ - copy_constructor &operator=(copy_constructor &&) = default; \ - } - - MPARK_VARIANT_COPY_CONSTRUCTOR( - Trait::TriviallyAvailable, - copy_constructor(const copy_constructor &that) = default;); - - MPARK_VARIANT_COPY_CONSTRUCTOR( - Trait::Available, - copy_constructor(const copy_constructor &that) - : copy_constructor(valueless_t{}) { - this->generic_construct(*this, that); - }); - - MPARK_VARIANT_COPY_CONSTRUCTOR( - Trait::Unavailable, - copy_constructor(const copy_constructor &) = delete;); - -#undef MPARK_VARIANT_COPY_CONSTRUCTOR - - template - class assignment : public copy_constructor { - using super = copy_constructor; - - public: - MPARK_INHERITING_CTOR(assignment, super) - using super::operator=; - - template - inline /* auto & */ auto emplace(Args &&... args) - -> decltype(this->construct_alt(access::base::get_alt(*this), - lib::forward(args)...)) { - this->destroy(); - auto &result = this->construct_alt(access::base::get_alt(*this), - lib::forward(args)...); - this->index_ = I; - return result; - } - - protected: -#ifndef MPARK_GENERIC_LAMBDAS - template - struct assigner { - template - inline void operator()(ThisAlt &this_alt, ThatAlt &&that_alt) const { - self->assign_alt(this_alt, lib::forward(that_alt).value); - } - assignment *self; - }; -#endif - - template - inline void assign_alt(alt &a, Arg &&arg) { - if (this->index() == I) { -#ifdef _MSC_VER - #pragma warning(push) -#pragma warning(disable : 4244) -#endif - a.value = lib::forward(arg); -#ifdef _MSC_VER -#pragma warning(pop) -#endif - } else { - struct { - void operator()(std::true_type) const { - this_->emplace(lib::forward(arg_)); - } - void operator()(std::false_type) const { - this_->emplace(T(lib::forward(arg_))); - } - assignment *this_; - Arg &&arg_; - } impl{this, lib::forward(arg)}; - impl(lib::bool_constant< - std::is_nothrow_constructible::value || - !std::is_nothrow_move_constructible::value>{}); - } - } - - template - inline void generic_assign(That &&that) { - if (this->valueless_by_exception() && that.valueless_by_exception()) { - // do nothing. - } else if (that.valueless_by_exception()) { - this->destroy(); - } else { - visitation::alt::visit_alt_at( - that.index(), -#ifdef MPARK_GENERIC_LAMBDAS - [this](auto &this_alt, auto &&that_alt) { - this->assign_alt( - this_alt, lib::forward(that_alt).value); - } -#else - assigner{this} -#endif - , - *this, - lib::forward(that)); - } - } - }; - - template - class move_assignment; - -#define MPARK_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, definition) \ - template \ - class move_assignment, move_assignable_trait> \ - : public assignment> { \ - using super = assignment>; \ - \ - public: \ - MPARK_INHERITING_CTOR(move_assignment, super) \ - using super::operator=; \ - \ - move_assignment(const move_assignment &) = default; \ - move_assignment(move_assignment &&) = default; \ - ~move_assignment() = default; \ - move_assignment &operator=(const move_assignment &) = default; \ - definition \ - } - - MPARK_VARIANT_MOVE_ASSIGNMENT( - Trait::TriviallyAvailable, - move_assignment &operator=(move_assignment &&that) = default;); - - MPARK_VARIANT_MOVE_ASSIGNMENT( - Trait::Available, - move_assignment & - operator=(move_assignment &&that) noexcept( - lib::all<(std::is_nothrow_move_constructible::value && - std::is_nothrow_move_assignable::value)...>::value) { - this->generic_assign(lib::move(that)); - return *this; - }); - - MPARK_VARIANT_MOVE_ASSIGNMENT( - Trait::Unavailable, - move_assignment &operator=(move_assignment &&) = delete;); - -#undef MPARK_VARIANT_MOVE_ASSIGNMENT - - template - class copy_assignment; - -#define MPARK_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, definition) \ - template \ - class copy_assignment, copy_assignable_trait> \ - : public move_assignment> { \ - using super = move_assignment>; \ - \ - public: \ - MPARK_INHERITING_CTOR(copy_assignment, super) \ - using super::operator=; \ - \ - copy_assignment(const copy_assignment &) = default; \ - copy_assignment(copy_assignment &&) = default; \ - ~copy_assignment() = default; \ - definition \ - copy_assignment &operator=(copy_assignment &&) = default; \ - } - - MPARK_VARIANT_COPY_ASSIGNMENT( - Trait::TriviallyAvailable, - copy_assignment &operator=(const copy_assignment &that) = default;); - - MPARK_VARIANT_COPY_ASSIGNMENT( - Trait::Available, - copy_assignment &operator=(const copy_assignment &that) { - this->generic_assign(that); - return *this; - }); - - MPARK_VARIANT_COPY_ASSIGNMENT( - Trait::Unavailable, - copy_assignment &operator=(const copy_assignment &) = delete;); - -#undef MPARK_VARIANT_COPY_ASSIGNMENT - - template - class impl : public copy_assignment> { - using super = copy_assignment>; - - public: - MPARK_INHERITING_CTOR(impl, super) - using super::operator=; - - template - inline void assign(Arg &&arg) { - this->assign_alt(access::base::get_alt(*this), - lib::forward(arg)); - } - - inline void swap(impl &that) { - if (this->valueless_by_exception() && that.valueless_by_exception()) { - // do nothing. - } else if (this->index() == that.index()) { - visitation::alt::visit_alt_at(this->index(), -#ifdef MPARK_GENERIC_LAMBDAS - [](auto &this_alt, auto &that_alt) { - using std::swap; - swap(this_alt.value, - that_alt.value); - } -#else - swapper{} -#endif - , - *this, - that); - } else { - impl *lhs = this; - impl *rhs = lib::addressof(that); - if (lhs->move_nothrow() && !rhs->move_nothrow()) { - std::swap(lhs, rhs); - } - impl tmp(lib::move(*rhs)); -#ifdef MPARK_EXCEPTIONS - // EXTENSION: When the move construction of `lhs` into `rhs` throws - // and `tmp` is nothrow move constructible then we move `tmp` back - // into `rhs` and provide the strong exception safety guarantee. - try { - this->generic_construct(*rhs, lib::move(*lhs)); - } catch (...) { - if (tmp.move_nothrow()) { - this->generic_construct(*rhs, lib::move(tmp)); - } - throw; - } -#else - this->generic_construct(*rhs, lib::move(*lhs)); -#endif - this->generic_construct(*lhs, lib::move(tmp)); - } - } - - private: -#ifndef MPARK_GENERIC_LAMBDAS - struct swapper { - template - inline void operator()(ThisAlt &this_alt, ThatAlt &that_alt) const { - using std::swap; - swap(this_alt.value, that_alt.value); - } - }; -#endif - - inline constexpr bool move_nothrow() const { - return this->valueless_by_exception() || - lib::array{ - {std::is_nothrow_move_constructible::value...} - }[this->index()]; - } - }; - -#undef MPARK_INHERITING_CTOR - - template - struct overload_leaf { - using F = lib::size_constant (*)(T); - operator F() const { return nullptr; } - }; - - template - struct overload_impl { - private: - template - struct impl; - - template - struct impl> : overload_leaf... {}; - - public: - using type = impl>; - }; - - template - using overload = typename overload_impl::type; - - template - using best_match = lib::invoke_result_t, T &&>; - - template - struct is_in_place_index : std::false_type {}; - - template - struct is_in_place_index> : std::true_type {}; - - template - struct is_in_place_type : std::false_type {}; - - template - struct is_in_place_type> : std::true_type {}; - - } // detail - - template - class variant { - static_assert(0 < sizeof...(Ts), - "variant must consist of at least one alternative."); - - static_assert(lib::all::value...>::value, - "variant can not have an array type as an alternative."); - - static_assert(lib::all::value...>::value, - "variant can not have a reference type as an alternative."); - - static_assert(lib::all::value...>::value, - "variant can not have a void type as an alternative."); - - public: - template < - typename Front = lib::type_pack_element_t<0, Ts...>, - lib::enable_if_t::value, int> = 0> - inline constexpr variant() noexcept( - std::is_nothrow_default_constructible::value) - : impl_(in_place_index_t<0>{}) {} - - variant(const variant &) = default; - variant(variant &&) = default; - - template < - typename Arg, - typename Decayed = lib::decay_t, - lib::enable_if_t::value, int> = 0, - lib::enable_if_t::value, int> = 0, - lib::enable_if_t::value, int> = 0, - std::size_t I = detail::best_match::value, - typename T = lib::type_pack_element_t, - lib::enable_if_t::value, int> = 0> - inline constexpr variant(Arg &&arg) noexcept( - std::is_nothrow_constructible::value) - : impl_(in_place_index_t{}, lib::forward(arg)) {} - - template < - std::size_t I, - typename... Args, - typename T = lib::type_pack_element_t, - lib::enable_if_t::value, int> = 0> - inline explicit constexpr variant( - in_place_index_t, - Args &&... args) noexcept(std::is_nothrow_constructible::value) - : impl_(in_place_index_t{}, lib::forward(args)...) {} - - template < - std::size_t I, - typename Up, - typename... Args, - typename T = lib::type_pack_element_t, - lib::enable_if_t &, - Args...>::value, - int> = 0> - inline explicit constexpr variant( - in_place_index_t, - std::initializer_list il, - Args &&... args) noexcept(std:: - is_nothrow_constructible< - T, - std::initializer_list &, - Args...>::value) - : impl_(in_place_index_t{}, il, lib::forward(args)...) {} - - template < - typename T, - typename... Args, - std::size_t I = detail::find_index_sfinae::value, - lib::enable_if_t::value, int> = 0> - inline explicit constexpr variant( - in_place_type_t, - Args &&... args) noexcept(std::is_nothrow_constructible::value) - : impl_(in_place_index_t{}, lib::forward(args)...) {} - - template < - typename T, - typename Up, - typename... Args, - std::size_t I = detail::find_index_sfinae::value, - lib::enable_if_t &, - Args...>::value, - int> = 0> - inline explicit constexpr variant( - in_place_type_t, - std::initializer_list il, - Args &&... args) noexcept(std:: - is_nothrow_constructible< - T, - std::initializer_list &, - Args...>::value) - : impl_(in_place_index_t{}, il, lib::forward(args)...) {} - - ~variant() = default; - - variant &operator=(const variant &) = default; - variant &operator=(variant &&) = default; - - template , variant>::value, - int> = 0, - std::size_t I = detail::best_match::value, - typename T = lib::type_pack_element_t, - lib::enable_if_t<(std::is_assignable::value && - std::is_constructible::value), - int> = 0> - inline variant &operator=(Arg &&arg) noexcept( - (std::is_nothrow_assignable::value && - std::is_nothrow_constructible::value)) { - impl_.template assign(lib::forward(arg)); - return *this; - } - - template < - std::size_t I, - typename... Args, - typename T = lib::type_pack_element_t, - lib::enable_if_t::value, int> = 0> - inline T &emplace(Args &&... args) { - return impl_.template emplace(lib::forward(args)...); - } - - template < - std::size_t I, - typename Up, - typename... Args, - typename T = lib::type_pack_element_t, - lib::enable_if_t &, - Args...>::value, - int> = 0> - inline T &emplace(std::initializer_list il, Args &&... args) { - return impl_.template emplace(il, lib::forward(args)...); - } - - template < - typename T, - typename... Args, - std::size_t I = detail::find_index_sfinae::value, - lib::enable_if_t::value, int> = 0> - inline T &emplace(Args &&... args) { - return impl_.template emplace(lib::forward(args)...); - } - - template < - typename T, - typename Up, - typename... Args, - std::size_t I = detail::find_index_sfinae::value, - lib::enable_if_t &, - Args...>::value, - int> = 0> - inline T &emplace(std::initializer_list il, Args &&... args) { - return impl_.template emplace(il, lib::forward(args)...); - } - - inline constexpr bool valueless_by_exception() const noexcept { - return impl_.valueless_by_exception(); - } - - inline constexpr std::size_t index() const noexcept { - return impl_.index(); - } - - template , - Dummy>::value && - lib::dependent_type, - Dummy>::value)...>::value, - int> = 0> - inline void swap(variant &that) noexcept( - lib::all<(std::is_nothrow_move_constructible::value && - lib::is_nothrow_swappable::value)...>::value) { - impl_.swap(that.impl_); - } - - private: - detail::impl impl_; - - friend struct detail::access::variant; - friend struct detail::visitation::variant; - }; - - template - inline constexpr bool holds_alternative(const variant &v) noexcept { - return v.index() == I; - } - - template - inline constexpr bool holds_alternative(const variant &v) noexcept { - return holds_alternative::value>(v); - } - - namespace detail { - template - struct generic_get_impl { - constexpr generic_get_impl(int) noexcept {} - - constexpr AUTO_REFREF operator()(V &&v) const - AUTO_REFREF_RETURN( - access::variant::get_alt(lib::forward(v)).value) - }; - - template - inline constexpr AUTO_REFREF generic_get(V &&v) - AUTO_REFREF_RETURN(generic_get_impl( - holds_alternative(v) ? 0 : (throw_bad_variant_access(), 0))( - lib::forward(v))) - } // namespace detail - - template - inline constexpr variant_alternative_t> &get( - variant &v) { - return detail::generic_get(v); - } - - template - inline constexpr variant_alternative_t> &&get( - variant &&v) { - return detail::generic_get(lib::move(v)); - } - - template - inline constexpr const variant_alternative_t> &get( - const variant &v) { - return detail::generic_get(v); - } - - template - inline constexpr const variant_alternative_t> &&get( - const variant &&v) { - return detail::generic_get(lib::move(v)); - } - - template - inline constexpr T &get(variant &v) { - return get::value>(v); - } - - template - inline constexpr T &&get(variant &&v) { - return get::value>(lib::move(v)); - } - - template - inline constexpr const T &get(const variant &v) { - return get::value>(v); - } - - template - inline constexpr const T &&get(const variant &&v) { - return get::value>(lib::move(v)); - } - - namespace detail { - - template - inline constexpr /* auto * */ AUTO generic_get_if(V *v) noexcept - AUTO_RETURN(v && holds_alternative(*v) - ? lib::addressof(access::variant::get_alt(*v).value) - : nullptr) - - } // namespace detail - - template - inline constexpr lib::add_pointer_t>> - get_if(variant *v) noexcept { - return detail::generic_get_if(v); - } - - template - inline constexpr lib::add_pointer_t< - const variant_alternative_t>> - get_if(const variant *v) noexcept { - return detail::generic_get_if(v); - } - - template - inline constexpr lib::add_pointer_t - get_if(variant *v) noexcept { - return get_if::value>(v); - } - - template - inline constexpr lib::add_pointer_t - get_if(const variant *v) noexcept { - return get_if::value>(v); - } - - namespace detail { - template - struct convert_to_bool { - template - inline constexpr bool operator()(Lhs &&lhs, Rhs &&rhs) const { - static_assert(std::is_convertible, - bool>::value, - "relational operators must return a type" - " implicitly convertible to bool"); - return lib::invoke( - RelOp{}, lib::forward(lhs), lib::forward(rhs)); - } - }; - } // namespace detail - - template - inline constexpr bool operator==(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using equal_to = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (lhs.index() != rhs.index()) return false; - if (lhs.valueless_by_exception()) return true; - return variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs); -#else - return lhs.index() == rhs.index() && - (lhs.valueless_by_exception() || - variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs)); -#endif - } - - template - inline constexpr bool operator!=(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using not_equal_to = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (lhs.index() != rhs.index()) return true; - if (lhs.valueless_by_exception()) return false; - return variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs); -#else - return lhs.index() != rhs.index() || - (!lhs.valueless_by_exception() && - variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs)); -#endif - } - - template - inline constexpr bool operator<(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using less = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (rhs.valueless_by_exception()) return false; - if (lhs.valueless_by_exception()) return true; - if (lhs.index() < rhs.index()) return true; - if (lhs.index() > rhs.index()) return false; - return variant::visit_value_at(lhs.index(), less{}, lhs, rhs); -#else - return !rhs.valueless_by_exception() && - (lhs.valueless_by_exception() || lhs.index() < rhs.index() || - (lhs.index() == rhs.index() && - variant::visit_value_at(lhs.index(), less{}, lhs, rhs))); -#endif - } - - template - inline constexpr bool operator>(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using greater = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (lhs.valueless_by_exception()) return false; - if (rhs.valueless_by_exception()) return true; - if (lhs.index() > rhs.index()) return true; - if (lhs.index() < rhs.index()) return false; - return variant::visit_value_at(lhs.index(), greater{}, lhs, rhs); -#else - return !lhs.valueless_by_exception() && - (rhs.valueless_by_exception() || lhs.index() > rhs.index() || - (lhs.index() == rhs.index() && - variant::visit_value_at(lhs.index(), greater{}, lhs, rhs))); -#endif - } - - template - inline constexpr bool operator<=(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using less_equal = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (lhs.valueless_by_exception()) return true; - if (rhs.valueless_by_exception()) return false; - if (lhs.index() < rhs.index()) return true; - if (lhs.index() > rhs.index()) return false; - return variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs); -#else - return lhs.valueless_by_exception() || - (!rhs.valueless_by_exception() && - (lhs.index() < rhs.index() || - (lhs.index() == rhs.index() && - variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs)))); -#endif - } - - template - inline constexpr bool operator>=(const variant &lhs, - const variant &rhs) { - using detail::visitation::variant; - using greater_equal = detail::convert_to_bool; -#ifdef MPARK_CPP14_CONSTEXPR - if (rhs.valueless_by_exception()) return true; - if (lhs.valueless_by_exception()) return false; - if (lhs.index() > rhs.index()) return true; - if (lhs.index() < rhs.index()) return false; - return variant::visit_value_at(lhs.index(), greater_equal{}, lhs, rhs); -#else - return rhs.valueless_by_exception() || - (!lhs.valueless_by_exception() && - (lhs.index() > rhs.index() || - (lhs.index() == rhs.index() && - variant::visit_value_at( - lhs.index(), greater_equal{}, lhs, rhs)))); -#endif - } - - struct monostate {}; - - inline constexpr bool operator<(monostate, monostate) noexcept { - return false; - } - - inline constexpr bool operator>(monostate, monostate) noexcept { - return false; - } - - inline constexpr bool operator<=(monostate, monostate) noexcept { - return true; - } - - inline constexpr bool operator>=(monostate, monostate) noexcept { - return true; - } - - inline constexpr bool operator==(monostate, monostate) noexcept { - return true; - } - - inline constexpr bool operator!=(monostate, monostate) noexcept { - return false; - } - -#ifdef MPARK_CPP14_CONSTEXPR - namespace detail { - - inline constexpr bool all(std::initializer_list bs) { - for (bool b : bs) { - if (!b) { - return false; - } - } - return true; - } - - } // namespace detail - - template - inline constexpr decltype(auto) visit(Visitor &&visitor, Vs &&... vs) { - return (detail::all({!vs.valueless_by_exception()...}) - ? (void)0 - : throw_bad_variant_access()), - detail::visitation::variant::visit_value( - lib::forward(visitor), lib::forward(vs)...); - } -#else - namespace detail { - - template - inline constexpr bool all_impl(const lib::array &bs, - std::size_t idx) { - return idx >= N || (bs[idx] && all_impl(bs, idx + 1)); - } - - template - inline constexpr bool all(const lib::array &bs) { - return all_impl(bs, 0); - } - - } // namespace detail - - template - inline constexpr DECLTYPE_AUTO visit(Visitor &&visitor, Vs &&... vs) - DECLTYPE_AUTO_RETURN( - (detail::all( - lib::array{{!vs.valueless_by_exception()...}}) - ? (void)0 - : throw_bad_variant_access()), - detail::visitation::variant::visit_value(lib::forward(visitor), - lib::forward(vs)...)) -#endif - - template - inline auto swap(variant &lhs, - variant &rhs) noexcept(noexcept(lhs.swap(rhs))) - -> decltype(lhs.swap(rhs)) { - lhs.swap(rhs); - } - - namespace detail { - - template - using enabled_type = T; - - namespace hash { - - template - constexpr bool meets_requirements() noexcept { - return std::is_copy_constructible::value && - std::is_move_constructible::value && - lib::is_invocable_r::value; - } - - template - constexpr bool is_enabled() noexcept { - using H = std::hash; - return meets_requirements() && - std::is_default_constructible::value && - std::is_copy_assignable::value && - std::is_move_assignable::value; - } - - } // namespace hash - - } // namespace detail - -#undef AUTO -#undef AUTO_RETURN - -#undef AUTO_REFREF -#undef AUTO_REFREF_RETURN - -#undef DECLTYPE_AUTO -#undef DECLTYPE_AUTO_RETURN - -} // namespace mpark - -namespace std { - - template - struct hash, - mpark::lib::enable_if_t>()...>::value>>> { - using argument_type = mpark::variant; - using result_type = std::size_t; - - inline result_type operator()(const argument_type &v) const { - using mpark::detail::visitation::variant; - std::size_t result = - v.valueless_by_exception() - ? 299792458 // Random value chosen by the universe upon creation - : variant::visit_alt( -#ifdef MPARK_GENERIC_LAMBDAS - [](const auto &alt) { - using alt_type = mpark::lib::decay_t; - using value_type = mpark::lib::remove_const_t< - typename alt_type::value_type>; - return hash{}(alt.value); - } -#else - hasher{} -#endif - , - v); - return hash_combine(result, hash{}(v.index())); - } - - private: -#ifndef MPARK_GENERIC_LAMBDAS - struct hasher { - template - inline std::size_t operator()(const Alt &alt) const { - using alt_type = mpark::lib::decay_t; - using value_type = - mpark::lib::remove_const_t; - return hash{}(alt.value); - } - }; -#endif - - static std::size_t hash_combine(std::size_t lhs, std::size_t rhs) { - return lhs ^= rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2); - } - }; - - template <> - struct hash { - using argument_type = mpark::monostate; - using result_type = std::size_t; - - inline result_type operator()(const argument_type &) const noexcept { - return 66740831; // return a fundamentally attractive random value. - } - }; - -} // namespace std - -#endif // MPARK_VARIANT_HPP diff --git a/libspider/graphs/pisdf/Param.h b/libspider/graphs/pisdf/Param.h index 751d0c5c..89bbdea6 100644 --- a/libspider/graphs/pisdf/Param.h +++ b/libspider/graphs/pisdf/Param.h @@ -40,7 +40,7 @@ #include #include #include -#include +#include namespace spider { namespace pisdf { @@ -102,46 +102,46 @@ namespace spider { inline size_t ix() const { return ix_; } inline int64_t value() const { - if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_)->value(); - } else if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_).value(); + if (std::holds_alternative(internal_)) { + return std::get(internal_)->value(); + } else if (std::holds_alternative(internal_)) { + return std::get(internal_).value(); } - return mpark::get(internal_); + return std::get(internal_); } inline int64_t value(const vector > ¶ms) const { - if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_)->value(); - } else if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_).evaluate(params); + if (std::holds_alternative(internal_)) { + return std::get(internal_)->value(); + } else if (std::holds_alternative(internal_)) { + return std::get(internal_).evaluate(params); } - return mpark::get(internal_); + return std::get(internal_); } inline ParamType type() const { return type_; } inline bool dynamic() const { - if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_)->dynamic(); + if (std::holds_alternative(internal_)) { + return std::get(internal_)->dynamic(); } return (type_ == ParamType::DYNAMIC) || (type_ == ParamType::DYNAMIC_DEPENDANT); } inline Param *parent() const { - if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_).get(); + if (std::holds_alternative(internal_)) { + return std::get(internal_).get(); } return nullptr; } inline Expression expression() const { - if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_)->expression(); - } else if (mpark::holds_alternative(internal_)) { - return mpark::get(internal_); + if (std::holds_alternative(internal_)) { + return std::get(internal_)->expression(); + } else if (std::holds_alternative(internal_)) { + return std::get(internal_); } - return Expression(mpark::get(internal_)); + return Expression(std::get(internal_)); } /* === Setter(s) === */ @@ -158,7 +158,7 @@ namespace spider { private: using param_t = std::shared_ptr; - using type_t = mpark::variant; + using type_t = std::variant; std::string name_; /* = Name of the Param. It is transformed to lower case on construction = */ type_t internal_; /* = Internal storage of the parameter = */ size_t ix_{ SIZE_MAX }; /* = Index of the Param in the Graph = */ diff --git a/libspider/memory/memory.cpp b/libspider/memory/memory.cpp index 7d7fd7cc..09949260 100755 --- a/libspider/memory/memory.cpp +++ b/libspider/memory/memory.cpp @@ -85,6 +85,14 @@ void operator delete[](void *ptr) noexcept { std::free(ptr); } +void operator delete(void *ptr, std::size_t) noexcept { + std::free(ptr); +} + +void operator delete[](void *ptr, std::size_t) noexcept { + std::free(ptr); +} + void *operator new(std::size_t size, const std::nothrow_t &) noexcept { return std::malloc(size); } From cf2f70a9e87dbd190ea3c03a2b1345acccc67845 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:36:46 +0200 Subject: [PATCH 03/15] (sonar) fixed vulnaribility warning with sprintf. --- libspider/common/Exception.h | 8 ++++---- libspider/common/Printer.cpp | 4 ++-- libspider/common/Printer.h | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libspider/common/Exception.h b/libspider/common/Exception.h index 66dee2af..b6037d3e 100644 --- a/libspider/common/Exception.h +++ b/libspider/common/Exception.h @@ -79,12 +79,12 @@ namespace spider { const char *msg, Args &&...args) : exceptionMessage_{ } { /* == Writes exception header == */ - int n = printer::sprintf(exceptionMessage_, EXCEPTION_BUFFER_SIZE, "%s::%s(%d): ", fileName, - fctName, lineNumber); + int n = printer::snprintf(exceptionMessage_, EXCEPTION_BUFFER_SIZE, "%s::%s(%d): ", fileName, + fctName, lineNumber); /* == Write the actual exception message == */ - n = printer::sprintf(exceptionMessage_ + n, static_cast(EXCEPTION_BUFFER_SIZE - n), msg, - std::forward(args)...); + n = printer::snprintf(exceptionMessage_ + n, static_cast(EXCEPTION_BUFFER_SIZE - n), msg, + std::forward(args)...); if (n > EXCEPTION_BUFFER_SIZE) { printer::fprintf(stderr, "Exception: ERROR: exception message too big.\n"); printer::fprintf(stderr, "Partially recovered exception: %s\n", exceptionMessage_); diff --git a/libspider/common/Printer.cpp b/libspider/common/Printer.cpp index 59762241..a949710a 100755 --- a/libspider/common/Printer.cpp +++ b/libspider/common/Printer.cpp @@ -59,7 +59,7 @@ namespace spider { return res; } - int sprintf(char *str, size_t size, const char *format, ...) { + int snprintf(char *str, size_t size, const char *format, ...) { va_list list; va_start(list, format); auto res = std::vsnprintf(str, size, format, list); @@ -79,7 +79,7 @@ namespace spider { return std::vfprintf(stream, format, list); } - int sprintf(char *str, size_t size, const char *format, va_list list) { + int snprintf(char *str, size_t size, const char *format, va_list list) { return std::vsnprintf(str, size, format, list); } diff --git a/libspider/common/Printer.h b/libspider/common/Printer.h index 6cfa0770..d982adfd 100755 --- a/libspider/common/Printer.h +++ b/libspider/common/Printer.h @@ -48,13 +48,13 @@ namespace spider { int fprintf(FILE *stream, const char *format, ...); - int sprintf(char *str, size_t size, const char *format, ...); + int snprintf(char *str, size_t size, const char *format, ...); int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, va_list list); - int sprintf(char *str, size_t size, const char *format, va_list list); + int snprintf(char *str, size_t size, const char *format, va_list list); int printf(const char *format, va_list list); } From c78589805855a3ce73c400309be66f326b46baed Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:36:57 +0200 Subject: [PATCH 04/15] (sonar) fixed overflow bug in Vertex::setName --- libspider/graphs/pisdf/Vertex.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libspider/graphs/pisdf/Vertex.cpp b/libspider/graphs/pisdf/Vertex.cpp index 751479b3..f34294f0 100644 --- a/libspider/graphs/pisdf/Vertex.cpp +++ b/libspider/graphs/pisdf/Vertex.cpp @@ -46,7 +46,7 @@ spider::pisdf::Vertex::Vertex(VertexType type, std::string name, size_t edgeINCo nINEdges_{ static_cast(edgeINCount) }, nOUTEdges_{ static_cast(edgeOUTCount) }, subtype_{ type } { - inputEdgeArray_ .reset(spider::make_n(edgeINCount, nullptr)); + inputEdgeArray_.reset(spider::make_n(edgeINCount, nullptr)); outputEdgeArray_.reset(spider::make_n(edgeOUTCount, nullptr)); rtInformation_ = spider::make_unique(StackID::RUNTIME); setName(std::move(name)); @@ -215,7 +215,7 @@ void spider::pisdf::Vertex::setName(std::string name) { const auto size = name.size(); name_.reset(spider::make_n(size + 1)); std::move(std::begin(name), std::end(name), name_.get()); - name_[size] = '\0'; + name_[size - 1] = '\0'; } From 33f3814cbd1f27f83efe6bb013ea9d5e72f08b2a Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:37:18 +0200 Subject: [PATCH 05/15] (sonar) fixed potential division by zeron in RoundRobinMapper --- libspider/scheduling/mapper/RoundRobinMapper.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libspider/scheduling/mapper/RoundRobinMapper.cpp b/libspider/scheduling/mapper/RoundRobinMapper.cpp index 70efd1a8..26170efe 100644 --- a/libspider/scheduling/mapper/RoundRobinMapper.cpp +++ b/libspider/scheduling/mapper/RoundRobinMapper.cpp @@ -53,17 +53,21 @@ const spider::PE *spider::sched::RoundRobinMapper::findPE(const Cluster *cluster const Stats &, const Task *task, ufast64) const { + const auto clusterPeCount = cluster->PECount(); + if (!clusterPeCount) { + return nullptr; + } const auto clusterIx = cluster->ix(); const auto *pe = cluster->peArray()[currentPeIx_[clusterIx]]; - size_t count = 0; - while ((!pe->enabled() || !task->isMappableOnPE(pe)) && count < cluster->PECount()) { - currentPeIx_[clusterIx] = (currentPeIx_[clusterIx] + 1u) % cluster->PECount(); + auto count = size_t{ 0 }; + while ((!pe->enabled() || !task->isMappableOnPE(pe)) && count < clusterPeCount) { + currentPeIx_[clusterIx] = (currentPeIx_[clusterIx] + 1u) % clusterPeCount; pe = cluster->peArray()[currentPeIx_[clusterIx]]; count++; } if (!pe->enabled() || !task->isMappableOnPE(pe)) { return nullptr; } - currentPeIx_[cluster->ix()] = (currentPeIx_[cluster->ix()] + 1u) % cluster->PECount(); + currentPeIx_[cluster->ix()] = (currentPeIx_[clusterIx] + 1u) % clusterPeCount; return pe; } From 617a84d38a3376c6036a683a901b8f25dc305cd7 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:37:37 +0200 Subject: [PATCH 06/15] (misc) set functions as static instead of const in CompilerExpression. --- .../expression-parser/helper/CompiledExpression.cpp | 10 +++++----- .../expression-parser/helper/CompiledExpression.h | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libspider/graphs-tools/expression-parser/helper/CompiledExpression.cpp b/libspider/graphs-tools/expression-parser/helper/CompiledExpression.cpp index 29a6e39a..1fe056f2 100644 --- a/libspider/graphs-tools/expression-parser/helper/CompiledExpression.cpp +++ b/libspider/graphs-tools/expression-parser/helper/CompiledExpression.cpp @@ -51,7 +51,7 @@ extern char **environ; spider::expr::CompiledExpression::CompiledExpression(const spider::vector &postfixStack, const param_table_t ¶ms) { /* == Tries to create the folder if it does not already exists == */ - if (mkdir("./.cache", 0777) < 0 && errno != EEXIST) { + if (mkdir("./.cache", 0755) < 0 && errno != EEXIST) { throwSpiderException("failed to create directory for jit compiled expressions."); } @@ -85,7 +85,7 @@ double spider::expr::CompiledExpression::evaluate(const param_table_t ¶ms) { /* === Private method(s) === */ spider::vector -spider::expr::CompiledExpression::convertToCpp(const spider::vector &postfixStack) const { +spider::expr::CompiledExpression::convertToCpp(const spider::vector &postfixStack) { auto res = factory::vector(postfixStack, StackID::EXPRESSION); for (auto &e : res) { if (e.token_ == "^") { @@ -170,7 +170,7 @@ void spider::expr::CompiledExpression::compile(const vector &postfix void spider::expr::CompiledExpression::writeFunctionFile(const std::string &func, const std::string &expression, - const spider::vector> &args) const { + const spider::vector> &args) { /* == Check if file already exists == */ FILE *outputFile; if (FILE *file = fopen("./.cache/libjitexpr.cpp", "r+")) { @@ -203,7 +203,7 @@ void spider::expr::CompiledExpression::writeFunctionFile(const std::string &func fclose(outputFile); } -void spider::expr::CompiledExpression::writeHelperFile() const { +void spider::expr::CompiledExpression::writeHelperFile() { const auto fileName = "./.cache/jitexpr-helper.h"; if (FILE *file = fopen(fileName, "r")) { fclose(file); @@ -260,7 +260,7 @@ void spider::expr::CompiledExpression::writeHelperFile() const { } } -void spider::expr::CompiledExpression::compileExpression() const { +void spider::expr::CompiledExpression::compileExpression() { #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wwrite-strings" diff --git a/libspider/graphs-tools/expression-parser/helper/CompiledExpression.h b/libspider/graphs-tools/expression-parser/helper/CompiledExpression.h index 0274222b..0977f6d1 100644 --- a/libspider/graphs-tools/expression-parser/helper/CompiledExpression.h +++ b/libspider/graphs-tools/expression-parser/helper/CompiledExpression.h @@ -102,7 +102,7 @@ namespace spider { void updateSymbolTable(const param_table_t ¶ms); - spider::vector convertToCpp(const spider::vector &postfixStack) const; + static spider::vector convertToCpp(const spider::vector &postfixStack) ; /** * @brief Compile the expression if needed. @@ -117,20 +117,20 @@ namespace spider { * @param expression Infix string of the expression to compile. * @param args Arguments of the expression. */ - void writeFunctionFile(const std::string &func, + static void writeFunctionFile(const std::string &func, const std::string &expression, - const spider::vector> &args) const; + const spider::vector> &args) ; /** * @brief Write the .h file with custom functions (if it does not exists). */ - void writeHelperFile() const; + static void writeHelperFile() ; /** * @brief Perform just in time compilation of the expression. * @throw @refitem spider::Exception if failed to compile. */ - void compileExpression() const; + static void compileExpression() ; /** * @brief Import the function from the compiled library. From 532d4462049ec96e7e38ff845b3b5268fa33bb20 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 18:51:25 +0200 Subject: [PATCH 07/15] (misc) renamed array_handle -> array_view. using std::unique_ptr for global stacks. fixed some sonar smells. --- libspider/api/debug-api.cpp | 4 +++ libspider/api/global-api.h | 6 ++-- libspider/api/spider.cpp | 12 +++++-- libspider/archi/Cluster.cpp | 8 ++--- libspider/archi/Cluster.h | 10 +++--- libspider/containers/array.h | 26 +++++++-------- .../{array_handle.h => array_view.h} | 32 +++++++++---------- .../transformation/optims/optimizations.cpp | 2 +- .../transformation/pisdf/GraphFiring.cpp | 8 ++--- .../transformation/pisdf/GraphFiring.h | 4 +-- .../transformation/pisdf/GraphHandler.h | 6 ++-- libspider/graphs/pisdf/Vertex.h | 20 ++++++------ libspider/graphs/srdag/SRDAGVertex.h | 8 ++--- libspider/memory/allocator.h | 4 +-- libspider/memory/memory.cpp | 4 +-- libspider/memory/memory.h | 2 +- libspider/runtime/common/Fifo.cpp | 26 +++++++-------- libspider/runtime/common/Fifo.h | 4 +-- libspider/runtime/runner/RTRunner.cpp | 4 +-- libspider/scheduling/memory/JobFifos.cpp | 8 ++--- libspider/scheduling/memory/JobFifos.h | 6 ++-- sandbox/main.cpp | 2 +- 22 files changed, 107 insertions(+), 99 deletions(-) rename libspider/containers/{array_handle.h => array_view.h} (91%) diff --git a/libspider/api/debug-api.cpp b/libspider/api/debug-api.cpp index 24aa9d03..825b03d3 100644 --- a/libspider/api/debug-api.cpp +++ b/libspider/api/debug-api.cpp @@ -87,6 +87,8 @@ void spider::api::enableLogger(log::Type type) { case log::Type::EXPR: log::enable(); break; + default: + printer::fprintf(stderr, "Unsupported logger enum type.\n"); } } @@ -116,6 +118,8 @@ void spider::api::disableLogger(log::Type type) { case log::Type::EXPR: log::disable(); break; + default: + printer::fprintf(stderr, "Unsupported logger enum type.\n"); } } diff --git a/libspider/api/global-api.h b/libspider/api/global-api.h index ea27b941..2f963833 100644 --- a/libspider/api/global-api.h +++ b/libspider/api/global-api.h @@ -41,6 +41,7 @@ #include #include #include +#include /* === non-namespace Enumeration(s) === */ @@ -288,10 +289,7 @@ namespace spider { return nameArray; } - inline std::array &stackArray() { - static std::array stackArray = {{ nullptr }}; - return stackArray; - } + std::array, STACK_COUNT> &stackArray(); /* === Type definition(s) === */ diff --git a/libspider/api/spider.cpp b/libspider/api/spider.cpp index 80cfe99a..7e76ea12 100755 --- a/libspider/api/spider.cpp +++ b/libspider/api/spider.cpp @@ -116,6 +116,11 @@ static void printConfig(const spider::StartUpConfig &cfg) { /* === Function(s) definition === */ +std::array, STACK_COUNT> &stackArray() { + static std::array, STACK_COUNT> stackArray = {{ nullptr }}; + return stackArray; +} + spider::StartUpConfig spider::parseInputArguments(int32_t argc, char **argv) { spider::log::info("parsing of input arguments is not yet supported.\n"); for (auto i = 0; i < argc; ++i) { @@ -129,7 +134,7 @@ void spider::api::setStackAllocatorPolicy(StackID stackId, size_t alignment, size_t size, void *externBuffer) { - auto *stack = stackArray()[static_cast(stackId)]; + auto &stack = stackArray()[static_cast(stackId)]; switch (policy) { case AllocatorPolicy::FREELIST_FIND_FIRST: stack->setPolicy(new FreeListAllocatorPolicy(size, externBuffer, FreeListPolicy::FIND_FIRST, alignment)); @@ -143,6 +148,8 @@ void spider::api::setStackAllocatorPolicy(StackID stackId, case AllocatorPolicy::LINEAR_STATIC: stack->setPolicy(new LinearStaticAllocator(size, externBuffer, alignment)); break; + default: + throwSpiderException("unsupported AllocatorPolicy value."); } } @@ -161,7 +168,7 @@ void spider::start(const StartUpConfig &cfg) { /* == Initialize stacks == */ auto it = EnumIterator{ }.begin(); for (auto &stack : stackArray()) { - stack = new Stack(*(it++)); + stack = std::unique_ptr(new Stack(*(it++))); } if (cfg.generalStackAllocatorPolicy_ != AllocatorPolicy::GENERIC) { api::setStackAllocatorPolicy(StackID::GENERAL, @@ -296,7 +303,6 @@ void spider::quit() { totalAverage += stack->average(); totalPeak += stack->peak(); } - delete stack; } Stack::print("Total", totalPeak, totalAverage, 1, totalUsage); diff --git a/libspider/archi/Cluster.cpp b/libspider/archi/Cluster.cpp index 6a67c3a4..50d8535f 100644 --- a/libspider/archi/Cluster.cpp +++ b/libspider/archi/Cluster.cpp @@ -37,7 +37,7 @@ #include #include #include -#include +#include /* === Static variable(s) === */ @@ -48,14 +48,14 @@ spider::Cluster::Cluster(size_t PECount, MemoryInterface *memoryInterface) : memoryInterface_{ memoryInterface } { PEArray_ = allocate(PECount); - make_handle(PEArray_, PECount).assign(nullptr); + make_view(PEArray_, PECount).assign(nullptr); if (!memoryInterface) { throwSpiderException("nullptr MemoryInterface"); } } spider::Cluster::~Cluster() { - for (auto &pe : make_handle(PEArray_, PECount_)) { + for (auto &pe : make_view(PEArray_, PECount_)) { destroy(pe); } destroy(PEArray_); @@ -66,7 +66,7 @@ void spider::Cluster::addPE(PE *pe) { if (!pe) { return; } - make_handle(PEArray_, PECount_ + 1).at(PECount_) = pe; + make_view(PEArray_, PECount_ + 1).at(PECount_) = pe; PECount_++; /* = In case at throws, PECount is not change = */ LRTCount_ += pe->isLRT(); if (platform()) { diff --git a/libspider/archi/Cluster.h b/libspider/archi/Cluster.h index baaa0bb6..ee964222 100644 --- a/libspider/archi/Cluster.h +++ b/libspider/archi/Cluster.h @@ -42,7 +42,7 @@ #include #include #include -#include +#include namespace spider { @@ -79,7 +79,7 @@ namespace spider { * @throws std::out_of_range if PE ix is out of bound. */ inline void setPEStatus(size_t ix, bool status) { - auto handle = make_handle(PEArray_, PECount_); + auto handle = make_view(PEArray_, PECount_); status ? handle.at(ix)->enable() : handle.at(ix)->disable(); } @@ -89,8 +89,8 @@ namespace spider { * @brief Get the array of processing element of the cluster. * @return const reference to the @refitem spider::array of @refitem PE of the cluster. */ - inline array_handle peArray() const { - return make_handle(PEArray_, PECount_); + inline array_view peArray() const { + return make_view(PEArray_, PECount_); } /** @@ -108,7 +108,7 @@ namespace spider { * @throws @refitem std::out_of_range if ix is out of bound */ inline PE *at(size_t ix) const { - return make_handle(PEArray_, PECount_).at(ix); + return make_view(PEArray_, PECount_).at(ix); } /** diff --git a/libspider/containers/array.h b/libspider/containers/array.h index dd1e2770..cffd7487 100644 --- a/libspider/containers/array.h +++ b/libspider/containers/array.h @@ -40,7 +40,7 @@ #include #include #include -#include +#include /* === Class definition === */ @@ -51,7 +51,7 @@ namespace spider { * @tparam T Type of the container content. */ template - class array : public array_handle { + class array : public array_view { public: using value_type = T; using size_type = size_t; @@ -72,7 +72,7 @@ namespace spider { * @param stack Stack on which the array should be allocated. * @param size Size of the array. */ - explicit array(size_type size, StackID stack = StackID::GENERAL) : array_handle(allocate(stack, size), + explicit array(size_type size, StackID stack = StackID::GENERAL) : array_view(allocate(stack, size), size) { } /** @@ -82,36 +82,36 @@ namespace spider { * @param value Value to set to all the elements of the array. */ array(size_t size, const_reference value, StackID stack = StackID::GENERAL) : array(size, stack) { - array_handle::assign(value); + array_view::assign(value); } array(std::initializer_list il, StackID stack = StackID::GENERAL) : array(il.size(), stack) { - std::copy(il.begin(), il.end(), array_handle::begin()); + std::copy(il.begin(), il.end(), array_view::begin()); } array() noexcept = default; array(const array &other) : array(other.size_) { - std::copy(other.begin(), other.end(), array_handle::begin()); + std::copy(other.begin(), other.end(), array_view::begin()); }; array(array &&other) noexcept: array() { swap(*this, other); } - array(T *data, size_type size) : array_handle(data, size) { } + array(T *data, size_type size) : array_view(data, size) { } ~array() { - deallocate(array_handle::data_); + deallocate(array_view::data_); } /* === Member functions === */ array &operator=(const array &other) { - deallocate(array_handle::data_); - array_handle::data_ = allocate(other.size_); - array_handle::size_ = other.size_; - std::copy(other.begin(), other.end(), array_handle::begin()); + deallocate(array_view::data_); + array_view::data_ = allocate(other.size_); + array_view::size_ = other.size_; + std::copy(other.begin(), other.end(), array_view::begin()); return *this; } @@ -132,7 +132,7 @@ namespace spider { inline friend void swap(array &first, array &second) noexcept { /* == Do the swapping of the values == */ using std::swap; - swap(static_cast &>(first), static_cast &>(second)); + swap(static_cast &>(first), static_cast &>(second)); } /* === Non member functions === */ diff --git a/libspider/containers/array_handle.h b/libspider/containers/array_view.h similarity index 91% rename from libspider/containers/array_handle.h rename to libspider/containers/array_view.h index 5163ed33..8d335c2e 100644 --- a/libspider/containers/array_handle.h +++ b/libspider/containers/array_view.h @@ -32,8 +32,8 @@ * The fact that you are presently reading this means that you have had * knowledge of the CeCILL license and that you accept its terms. */ -#ifndef SPIDER2_ARRAY_HANDLE_H -#define SPIDER2_ARRAY_HANDLE_H +#ifndef SPIDER2_ARRAY_VIEW_H +#define SPIDER2_ARRAY_VIEW_H /* === Include(s) === */ @@ -51,7 +51,7 @@ namespace spider { * @tparam T Type of the container content. */ template - class array_handle { + class array_view { public: using value_type = T; using size_type = size_t; @@ -62,9 +62,9 @@ namespace spider { using iterator = value_type *; using const_iterator = const value_type *; - array_handle() = default; + array_view() = default; - array_handle(T *data, size_type size) : data_{ data }, size_{ size } { + array_view(T *data, size_type size) : data_{ data }, size_{ size } { #ifndef NDEBUG if (!data && size) { throwSpiderException("unsafe array handle created with nullptr data and size != 0."); @@ -72,17 +72,17 @@ namespace spider { #endif } - array_handle(const array_handle &) = default; + array_view(const array_view &) = default; - array_handle(array_handle &&) noexcept = default; + array_view(array_view &&) noexcept = default; - ~array_handle() = default; + ~array_view() = default; /* === Member functions === */ - array_handle &operator=(const array_handle &) = default; + array_view &operator=(const array_view &) = default; - array_handle &operator=(array_handle &&) noexcept = default; + array_view &operator=(array_view &&) noexcept = default; /** * @brief Replaces the contents of the container. @@ -112,7 +112,7 @@ namespace spider { * @param first First container. * @param second Other container to exchange the contents with. */ - inline friend void swap(array_handle &first, array_handle &second) noexcept { + inline friend void swap(array_view &first, array_view &second) noexcept { /* == Do the swapping of the values == */ using std::swap; swap(first.data_, second.data_); @@ -259,7 +259,7 @@ namespace spider { /* === Non member functions === */ - inline friend bool operator==(const array_handle &lhs, const array_handle &rhs) { + inline friend bool operator==(const array_view &lhs, const array_view &rhs) { if (lhs.size() != rhs.size()) { return false; } @@ -273,7 +273,7 @@ namespace spider { return true; } - inline friend bool operator!=(const array_handle &lhs, const array_handle &rhs) { + inline friend bool operator!=(const array_view &lhs, const array_view &rhs) { return !(lhs == rhs); } @@ -283,8 +283,8 @@ namespace spider { }; template - array_handle make_handle(T *data, size_t size) { - return array_handle(data, size); + array_view make_view(T *data, size_t size) { + return array_view(data, size); } } -#endif //SPIDER2_ARRAY_HANDLE_H +#endif //SPIDER2_ARRAY_VIEW_H diff --git a/libspider/graphs-tools/transformation/optims/optimizations.cpp b/libspider/graphs-tools/transformation/optims/optimizations.cpp index c2bd36d9..232c003d 100644 --- a/libspider/graphs-tools/transformation/optims/optimizations.cpp +++ b/libspider/graphs-tools/transformation/optims/optimizations.cpp @@ -49,7 +49,7 @@ /* === Static function(s) === */ -static u32 countNonNullEdges(const spider::array_handle &edges) { +static u32 countNonNullEdges(const spider::array_view &edges) { u32 count = 0; for (const auto *edge : edges) { if (edge) { diff --git a/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp b/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp index 45892a7b..a6ea42f4 100644 --- a/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp +++ b/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp @@ -131,12 +131,12 @@ void spider::pisdf::GraphFiring::clear() { resolved_ = parent_->isStatic(); } -spider::array_handle spider::pisdf::GraphFiring::subgraphFirings() const { - return make_handle(subgraphHandlers_.get(), parent_->graph()->subgraphCount()); +spider::array_view spider::pisdf::GraphFiring::subgraphFirings() const { + return make_view(subgraphHandlers_.get(), parent_->graph()->subgraphCount()); } -spider::array_handle spider::pisdf::GraphFiring::subgraphHandlers() { - return make_handle(subgraphHandlers_.get(), parent_->graph()->subgraphCount()); +spider::array_view spider::pisdf::GraphFiring::subgraphHandlers() { + return make_view(subgraphHandlers_.get(), parent_->graph()->subgraphCount()); } int64_t spider::pisdf::GraphFiring::getSrcRate(const Edge *edge) const { diff --git a/libspider/graphs-tools/transformation/pisdf/GraphFiring.h b/libspider/graphs-tools/transformation/pisdf/GraphFiring.h index 41f5fd8b..87eff3fe 100644 --- a/libspider/graphs-tools/transformation/pisdf/GraphFiring.h +++ b/libspider/graphs-tools/transformation/pisdf/GraphFiring.h @@ -105,13 +105,13 @@ namespace spider { * @brief Get the subgraphs @refitem GraphFiring. * @return const reference to the array of subgraphs GraphFiring. */ - spider::array_handle subgraphFirings() const; + spider::array_view subgraphFirings() const; /** * @brief non const overload of the @refitem GraphFiring::subgraphFirings method. * @return non const reference to the array of subgraphs GraphFiring. */ - spider::array_handle subgraphHandlers(); + spider::array_view subgraphHandlers(); /** * @brief Get the firing value of this GraphFiring. diff --git a/libspider/graphs-tools/transformation/pisdf/GraphHandler.h b/libspider/graphs-tools/transformation/pisdf/GraphHandler.h index 920e5ca2..61bad58e 100644 --- a/libspider/graphs-tools/transformation/pisdf/GraphHandler.h +++ b/libspider/graphs-tools/transformation/pisdf/GraphHandler.h @@ -39,7 +39,7 @@ #include #include -#include +#include #include namespace spider { @@ -79,11 +79,11 @@ namespace spider { /* === Getter(s) === */ - inline array_handle firings() const { return make_handle(firings_.get(), repetitionCount_); } + inline array_view firings() const { return make_view(firings_.get(), repetitionCount_); } inline GraphFiring *firing(size_t ix) const { return firings_[ix]; } - inline array_handle firings() { return make_handle(firings_.get(), repetitionCount_); } + inline array_view firings() { return make_view(firings_.get(), repetitionCount_); } inline const GraphFiring *base() const { return handler_; } diff --git a/libspider/graphs/pisdf/Vertex.h b/libspider/graphs/pisdf/Vertex.h index 584ca32f..caa3d2c9 100755 --- a/libspider/graphs/pisdf/Vertex.h +++ b/libspider/graphs/pisdf/Vertex.h @@ -187,8 +187,8 @@ namespace spider { * @brief A const reference on the array of input edges. Useful for iterating on the edges. * @return const reference to input edge array */ - inline spider::array_handle inputEdges() const { - return spider::make_handle(inputEdgeArray_.get(), nINEdges_); + inline spider::array_view inputEdges() const { + return spider::make_view(inputEdgeArray_.get(), nINEdges_); }; /** @@ -216,8 +216,8 @@ namespace spider { * @brief A const reference on the array of output edges. Useful for iterating on the edges. * @return const reference to output edge array. */ - inline spider::array_handle outputEdges() const { - return spider::make_handle(outputEdgeArray_.get(), nOUTEdges_); + inline spider::array_view outputEdges() const { + return spider::make_view(outputEdgeArray_.get(), nOUTEdges_); }; /** @@ -263,16 +263,16 @@ namespace spider { * @brief A const reference on the vector of refinement input params. * @return const reference to input params vector. */ - inline spider::array_handle refinementParamIxVector() const { - return spider::make_handle(refinementParamArray_.get(), nRefinementParams_); + inline spider::array_view refinementParamIxVector() const { + return spider::make_view(refinementParamArray_.get(), nRefinementParams_); }; /** * @brief A const reference on the vector of input params. * @return const reference to input params vector. */ - inline spider::array_handle inputParamIxVector() const { - return spider::make_handle(inputParamArray_.get(), nINParams_); + inline spider::array_view inputParamIxVector() const { + return spider::make_view(inputParamArray_.get(), nINParams_); }; /** @@ -285,8 +285,8 @@ namespace spider { * @brief A const reference on the vector of output params. * @return const reference to output params vector. */ - inline spider::array_handle outputParamIxVector() const { - return spider::make_handle(outputParamArray_.get(), nOUTParams_); + inline spider::array_view outputParamIxVector() const { + return spider::make_view(outputParamArray_.get(), nOUTParams_); }; /** diff --git a/libspider/graphs/srdag/SRDAGVertex.h b/libspider/graphs/srdag/SRDAGVertex.h index 500dee23..566341a0 100644 --- a/libspider/graphs/srdag/SRDAGVertex.h +++ b/libspider/graphs/srdag/SRDAGVertex.h @@ -168,8 +168,8 @@ namespace spider { * @brief A const reference on the array of input edges. Useful for iterating on the edges. * @return const reference to input edge array */ - inline spider::array_handle inputEdges() const { - return spider::make_handle(inputEdgeArray_, nINEdges_); + inline spider::array_view inputEdges() const { + return spider::make_view(inputEdgeArray_, nINEdges_); }; /** @@ -197,8 +197,8 @@ namespace spider { * @brief A const reference on the array of output edges. Useful for iterating on the edges. * @return const reference to output edge array. */ - inline spider::array_handle outputEdges() const { - return spider::make_handle(outputEdgeArray_, nOUTEdges_); + inline spider::array_view outputEdges() const { + return spider::make_view(outputEdgeArray_, nOUTEdges_); }; /** diff --git a/libspider/memory/allocator.h b/libspider/memory/allocator.h index ff88ca5f..e1175964 100755 --- a/libspider/memory/allocator.h +++ b/libspider/memory/allocator.h @@ -74,12 +74,12 @@ namespace spider { /* === Constructors / Destructors === */ - allocator() noexcept: stack_{ stackArray()[static_cast(StackID::GENERAL)] } { }; + allocator() noexcept: stack_{ stackArray()[static_cast(StackID::GENERAL)].get() } { }; template allocator(const allocator &other) noexcept : stack_{ other.stack() } { } - explicit allocator(StackID stackId) : stack_{ stackArray()[static_cast(stackId)] } { + explicit allocator(StackID stackId) : stack_{ stackArray()[static_cast(stackId)].get() } { if (!stack_) { throwSpiderException("trying to use non-initialized stack_."); } diff --git a/libspider/memory/memory.cpp b/libspider/memory/memory.cpp index 7d7fd7cc..b36012cf 100755 --- a/libspider/memory/memory.cpp +++ b/libspider/memory/memory.cpp @@ -39,7 +39,7 @@ /* === Function(s) definition === */ -void *spider::allocate(Stack *stack, size_t size, size_t n) { +void *spider::allocate(std::unique_ptr &stack, size_t size, size_t n) { if (!n) { return nullptr; } @@ -63,7 +63,7 @@ void spider::deallocate(void *ptr) { auto stackId = static_cast(reinterpret_cast(originalPtr)[0]); /* == Deallocate the pointer == */ - auto *stack = stackArray()[static_cast(stackId)]; + auto &stack = stackArray()[static_cast(stackId)]; stack->deallocate(originalPtr); } diff --git a/libspider/memory/memory.h b/libspider/memory/memory.h index 73a6be3d..0eda6210 100755 --- a/libspider/memory/memory.h +++ b/libspider/memory/memory.h @@ -63,7 +63,7 @@ namespace spider { * @param n Number of element of size "size" to allocate. * @return allocated buffer on success, nullptr else. */ - void *allocate(Stack *stack, size_t size, size_t n); + void *allocate(std::unique_ptr &stack, size_t size, size_t n); /** * @brief Allocate raw memory buffer on given stack. diff --git a/libspider/runtime/common/Fifo.cpp b/libspider/runtime/common/Fifo.cpp index 564c3a62..5fe23c28 100644 --- a/libspider/runtime/common/Fifo.cpp +++ b/libspider/runtime/common/Fifo.cpp @@ -46,12 +46,12 @@ namespace spider { /* === Static read functions declaration === */ - static void *readDummy(array_handle::iterator &it, MemoryInterface *) { + static void *readDummy(array_view::iterator &it, MemoryInterface *) { it++; return nullptr; } - static void *readExternBuffer(array_handle::iterator &it, MemoryInterface *) { + static void *readExternBuffer(array_view::iterator &it, MemoryInterface *) { const auto fifo = *(it++); if (!fifo.size_) { return nullptr; @@ -59,7 +59,7 @@ namespace spider { return cast_buffer_woffset(archi::platform()->getExternalBuffer(fifo.address_), fifo.offset_); } - static void *readBuffer(array_handle::iterator &it, MemoryInterface *memoryInterface) { + static void *readBuffer(array_view::iterator &it, MemoryInterface *memoryInterface) { const auto fifo = *(it++); if (!fifo.size_) { return nullptr; @@ -67,13 +67,13 @@ namespace spider { return cast_buffer_woffset(memoryInterface->read(fifo.address_, fifo.count_), fifo.offset_); } - static void *readRepeatBuffer(array_handle::iterator &, MemoryInterface *); + static void *readRepeatBuffer(array_view::iterator &, MemoryInterface *); - static void *readMergedBuffer(array_handle::iterator &, MemoryInterface *); + static void *readMergedBuffer(array_view::iterator &, MemoryInterface *); /* === Static array of read functions === */ - using fifo_fun_t = void *(*)(array_handle::iterator &it, MemoryInterface *); + using fifo_fun_t = void *(*)(array_view::iterator &it, MemoryInterface *); static std::array readFunctions = {{ readBuffer, /*!< RW_ONLY */ readBuffer, /*!< RW_OWN */ @@ -86,7 +86,7 @@ namespace spider { /* === Static read functions definition === */ - static void *readMergedBuffer(array_handle::iterator &it, MemoryInterface *memoryInterface) { + static void *readMergedBuffer(array_view::iterator &it, MemoryInterface *memoryInterface) { const auto mergedFifo = *(it++); auto *mergedBuffer = memoryInterface->allocate(mergedFifo.address_, mergedFifo.size_, mergedFifo.count_); #ifndef NDEBUG @@ -105,7 +105,7 @@ namespace spider { return mergedBuffer; } - static void *readRepeatBuffer(array_handle::iterator &it, MemoryInterface *memoryInterface) { + static void *readRepeatBuffer(array_view::iterator &it, MemoryInterface *memoryInterface) { const auto repeatFifo = *(it++); auto *repeatBuffer = memoryInterface->allocate(repeatFifo.address_, repeatFifo.size_, repeatFifo.count_); #ifndef NDEBUG @@ -134,7 +134,7 @@ namespace spider { /* === Static allocate functions === */ - static void *allocBuffer(array_handle::iterator &it, MemoryInterface *memoryInterface) { + static void *allocBuffer(array_view::iterator &it, MemoryInterface *memoryInterface) { const auto fifo = *(it++); return memoryInterface->allocate(fifo.address_, fifo.size_, fifo.count_); } @@ -154,7 +154,7 @@ namespace spider { /* === Function(s) definition === */ spider::array -spider::getInputBuffers(const array_handle &fifos, MemoryInterface *memoryInterface) { +spider::getInputBuffers(const array_view &fifos, MemoryInterface *memoryInterface) { size_t count = 0u; auto it = std::begin(fifos); while (it != std::end(fifos)) { @@ -163,7 +163,7 @@ spider::getInputBuffers(const array_handle &fifos, MemoryInterface *memory } auto result = spider::array{ count, nullptr, StackID::RUNTIME }; /* = yeah it is ugly, but avoids changing everything else and keeps const at high level = */ - auto fifoIt = const_cast::iterator>(std::begin(fifos)); + auto fifoIt = const_cast::iterator>(std::begin(fifos)); auto resIt = std::begin(result); while (resIt != std::end(result)) { if (fifoIt->attribute_ == FifoAttribute::DUMMY) { @@ -177,10 +177,10 @@ spider::getInputBuffers(const array_handle &fifos, MemoryInterface *memory } spider::array -spider::getOutputBuffers(const array_handle &fifos, MemoryInterface *memoryInterface) { +spider::getOutputBuffers(const array_view &fifos, MemoryInterface *memoryInterface) { auto result = spider::array{ fifos.size(), nullptr, StackID::RUNTIME }; /* = yeah it is ugly, but avoids changing everything else and keeps const at high level = */ - auto fifoIt = const_cast::iterator>(std::begin(fifos)); + auto fifoIt = const_cast::iterator>(std::begin(fifos)); for (auto it = std::begin(result); it != std::end(result); ++it) { (*it) = allocFunctions[static_cast((*fifoIt).attribute_)](fifoIt, memoryInterface); } diff --git a/libspider/runtime/common/Fifo.h b/libspider/runtime/common/Fifo.h index cb0da86e..a42a7c12 100644 --- a/libspider/runtime/common/Fifo.h +++ b/libspider/runtime/common/Fifo.h @@ -88,9 +88,9 @@ namespace spider { class MemoryInterface; - spider::array getInputBuffers(const array_handle &fifos, MemoryInterface *memoryInterface); + spider::array getInputBuffers(const array_view &fifos, MemoryInterface *memoryInterface); - spider::array getOutputBuffers(const array_handle &fifos, MemoryInterface *memoryInterface); + spider::array getOutputBuffers(const array_view &fifos, MemoryInterface *memoryInterface); } #endif //SPIDER2_FIFO_H diff --git a/libspider/runtime/runner/RTRunner.cpp b/libspider/runtime/runner/RTRunner.cpp index aac00477..6da8ff71 100644 --- a/libspider/runtime/runner/RTRunner.cpp +++ b/libspider/runtime/runner/RTRunner.cpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include /* === Define(s) === */ @@ -109,7 +109,7 @@ void spider::RTRunner::sendJobStampNotification(bool *notificationFlags, size_t return; } size_t lrtIx = 0; - for (const auto &shouldNotify : array_handle{ notificationFlags, archi::platform()->LRTCount() }) { + for (const auto &shouldNotify : array_view{ notificationFlags, archi::platform()->LRTCount() }) { if (shouldNotify && lrtIx != ix()) { rt::platform()->communicator()->push(Notification{ NotificationType::JOB_UPDATE_JOBSTAMP, ix(), jobIx }, lrtIx); diff --git a/libspider/scheduling/memory/JobFifos.cpp b/libspider/scheduling/memory/JobFifos.cpp index c3de0c96..78e74762 100644 --- a/libspider/scheduling/memory/JobFifos.cpp +++ b/libspider/scheduling/memory/JobFifos.cpp @@ -47,12 +47,12 @@ spider::JobFifos::JobFifos(u32 inputFifoCount, u32 outputFifoCount) : } -spider::array_handle spider::JobFifos::inputFifos() const { - return make_handle(inputFifos_.get(), inputFifoCount_); +spider::array_view spider::JobFifos::inputFifos() const { + return make_view(inputFifos_.get(), inputFifoCount_); } -spider::array_handle spider::JobFifos::outputFifos() const { - return make_handle(outputFifos_.get(), outputFifoCount_); +spider::array_view spider::JobFifos::outputFifos() const { + return make_view(outputFifos_.get(), outputFifoCount_); } size_t spider::JobFifos::inputFifoCount() const { diff --git a/libspider/scheduling/memory/JobFifos.h b/libspider/scheduling/memory/JobFifos.h index 5c0c728b..3b55a495 100644 --- a/libspider/scheduling/memory/JobFifos.h +++ b/libspider/scheduling/memory/JobFifos.h @@ -38,7 +38,7 @@ /* === Include(s) === */ #include -#include +#include #include namespace spider { @@ -67,13 +67,13 @@ namespace spider { * @brief Return an array handle on the input @refitem RTFifo. * @return @refitem spider::array_handle of @refitem RTFifo. */ - array_handle inputFifos() const; + array_view inputFifos() const; /** * @brief Return an array handle on the output @refitem RTFifo. * @return @refitem spider::array_handle of @refitem RTFifo. */ - array_handle outputFifos() const; + array_view outputFifos() const; /** * @brief Get the input fifo at index ix. diff --git a/sandbox/main.cpp b/sandbox/main.cpp index 0683753d..1d5d3953 100644 --- a/sandbox/main.cpp +++ b/sandbox/main.cpp @@ -56,7 +56,7 @@ #include #include #include -#include +#include #include #include #include From 660046fdf94bf68a648f2fed872d137fa1d1501e Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 20:24:00 +0200 Subject: [PATCH 08/15] (sonar) misc cleans. --- libspider/archi/Cluster.h | 2 +- libspider/archi/MemoryBus.cpp | 4 ++-- libspider/common/Exception.h | 2 +- libspider/common/Logger.h | 16 ++++++++-------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libspider/archi/Cluster.h b/libspider/archi/Cluster.h index ee964222..96df3b01 100644 --- a/libspider/archi/Cluster.h +++ b/libspider/archi/Cluster.h @@ -148,7 +148,7 @@ namespace spider { * @brief Get the platform of the cluster. * @return @refitem Platform of the cluster. */ - inline Platform *platform() const { + static inline Platform *platform() { return archi::platform(); } diff --git a/libspider/archi/MemoryBus.cpp b/libspider/archi/MemoryBus.cpp index 647de8fc..071f3f74 100755 --- a/libspider/archi/MemoryBus.cpp +++ b/libspider/archi/MemoryBus.cpp @@ -52,8 +52,8 @@ spider::MemoryBus::MemoryBus() { sendCostRoutine_ = [](u64) -> u64 { return 0; }; receiveCostRoutine_ = [](u64) -> u64 { return 0; }; - sendRoutine_ = [](i64, const void *, const void *) { }; - receiveRoutine_ = [](i64, const void *, const void *) { }; + sendRoutine_ = [](i64, const void *, const void *) { /* = empty method = */ }; + receiveRoutine_ = [](i64, const void *, const void *) { /* = empty method = */ }; } uint64_t spider::MemoryBus::sendCost(uint64_t size) const { diff --git a/libspider/common/Exception.h b/libspider/common/Exception.h index b6037d3e..fe48d717 100644 --- a/libspider/common/Exception.h +++ b/libspider/common/Exception.h @@ -44,7 +44,7 @@ /* === Defines === */ /* == Size of 50 minimum is required for the error message associated == */ -#define EXCEPTION_BUFFER_SIZE 400 +constexpr auto EXCEPTION_BUFFER_SIZE = 400; /* === Macros === */ diff --git a/libspider/common/Logger.h b/libspider/common/Logger.h index 9887ff4a..5f534b6c 100644 --- a/libspider/common/Logger.h +++ b/libspider/common/Logger.h @@ -49,14 +49,14 @@ namespace spider { namespace log { - constexpr static const char green[] = "\x1B[32m"; - constexpr static const char red[] = "\x1B[31m"; - constexpr static const char yellow[] = "\x1B[33m"; - constexpr static const char blue[] = "\x1B[34m"; - constexpr static const char magenta[] = "\x1B[35m"; - constexpr static const char cyan[] = "\x1B[36m"; - constexpr static const char white[] = "\x1B[37m"; - constexpr static const char normal[] = "\x1B[0m"; + constexpr static auto green = "\x1B[32m"; + constexpr static auto red = "\x1B[31m"; + constexpr static auto yellow = "\x1B[33m"; + constexpr static auto blue = "\x1B[34m"; + constexpr static auto magenta = "\x1B[35m"; + constexpr static auto cyan = "\x1B[36m"; + constexpr static auto white = "\x1B[37m"; + constexpr static auto normal = "\x1B[0m"; inline std::mutex &mutex() { static std::mutex lock; From 05e402995dcc76b2f436d7340cddf797a8472995 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Fri, 28 May 2021 22:31:43 +0200 Subject: [PATCH 09/15] (fix) fixed doxygen url version. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7aea7b3f..0f6a5169 100644 --- a/.travis.yml +++ b/.travis.yml @@ -65,7 +65,7 @@ jobs: - choco install visualstudio2019-workload-vctools install: - mkdir doxygen && cd doxygen - - curl https://www.doxygen.nl/files/doxygen-1.8.20.windows.x64.bin.zip -o doxygen.zip + - curl https://www.doxygen.nl/files/doxygen-1.9.1.windows.x64.bin.zip -o doxygen.zip - unzip doxygen.zip - export PATH=$PATH:$PWD - cd .. From cdcd7e1f0f45cc6f9cad8a05cc441779cbed62b8 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 18:39:43 +0100 Subject: [PATCH 10/15] [cpp] fixed signed / unsigned warnings --- .../srdag/singleRateTransformation.cpp | 2 +- libspider/graphs/pisdf/Vertex.cpp | 1 - libspider/graphs/srdag/SRDAGGraph.cpp | 28 +++++++++---------- libspider/graphs/srdag/SRDAGGraph.h | 4 +-- libspider/graphs/srdag/SRDAGVertex.h | 4 +-- libspider/monitor/Monitor.h | 1 + .../scheduling/launcher/TaskLauncher.cpp | 2 +- libspider/scheduling/memory/FifoAllocator.h | 8 +++--- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp b/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp index 457e7a69..380b0c96 100644 --- a/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp +++ b/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp @@ -149,7 +149,7 @@ void spider::srdag::detail::cloneVertex(const pisdf::Vertex *vertex, u32 firing, * A -> | | -> B * But in reality the vertex does not make it after the SR-Transformation. */ - auto *clone = make(vertex, firing, 2, 2); + auto *clone = make(vertex, firing, 2u, 2u); clone->setExecutable(false); /* == Add clone to the srdag == */ srdag->addVertex(clone); diff --git a/libspider/graphs/pisdf/Vertex.cpp b/libspider/graphs/pisdf/Vertex.cpp index f34294f0..55883ae6 100644 --- a/libspider/graphs/pisdf/Vertex.cpp +++ b/libspider/graphs/pisdf/Vertex.cpp @@ -38,7 +38,6 @@ #include #include #include -#include /* === Function(s) definition === */ diff --git a/libspider/graphs/srdag/SRDAGGraph.cpp b/libspider/graphs/srdag/SRDAGGraph.cpp index df26eb6e..e0b2f553 100644 --- a/libspider/graphs/srdag/SRDAGGraph.cpp +++ b/libspider/graphs/srdag/SRDAGGraph.cpp @@ -143,7 +143,7 @@ spider::srdag::Vertex *spider::srdag::Graph::createDuplicateVertex(std::string n auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::DUPLICATE_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, 1, edgeOUTCount); + auto *srVertex = make(vertex, 0u, 1u, edgeOUTCount); addVertex(srVertex); return srVertex; } @@ -153,17 +153,17 @@ spider::srdag::Vertex *spider::srdag::Graph::createForkVertex(std::string name, auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::FORK_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, 1, edgeOUTCount); + auto *srVertex = make(vertex, 0u, 1u, edgeOUTCount); addVertex(srVertex); return srVertex; } spider::srdag::Vertex *spider::srdag::Graph::createJoinVertex(std::string name, size_t edgeINCount) { - auto *vertex = make(pisdf::VertexType::JOIN, std::move(name), 0, 1u); + auto *vertex = make(pisdf::VertexType::JOIN, std::move(name), 0u, 1u); auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::JOIN_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, edgeINCount, 1); + auto *srVertex = make(vertex, 0u, edgeINCount, 1u); addVertex(srVertex); return srVertex; } @@ -172,7 +172,7 @@ spider::srdag::Vertex * spider::srdag::Graph::createVertex(std::string name, size_t edgeINCount, size_t edgeOUTCount) { auto *vertex = make(pisdf::VertexType::NORMAL, std::move(name)); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, edgeINCount, edgeOUTCount); + auto *srVertex = make(vertex, 0u, edgeINCount, edgeOUTCount); addVertex(srVertex); return srVertex; } @@ -181,7 +181,7 @@ spider::srdag::Vertex * spider::srdag::Graph::createVoidVertex(std::string name, size_t edgeINCount, size_t edgeOUTCount) { auto *vertex = make(pisdf::VertexType::NORMAL, std::move(name)); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, edgeINCount, edgeOUTCount); + auto *srVertex = make(vertex, 0u, edgeINCount, edgeOUTCount); srVertex->setExecutable(false); addVertex(srVertex); return srVertex; @@ -192,37 +192,37 @@ spider::srdag::Vertex *spider::srdag::Graph::createRepeatVertex(std::string name auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::REPEAT_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, 1, 1); + auto *srVertex = make(vertex, 0u, 1u, 1u); addVertex(srVertex); return srVertex; } spider::srdag::Vertex *spider::srdag::Graph::createTailVertex(std::string name, size_t edgeINCount) { - auto *vertex = make(pisdf::VertexType::TAIL, std::move(name), 0, 1u); + auto *vertex = make(pisdf::VertexType::TAIL, std::move(name), 0u, 1u); auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::TAIL_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, edgeINCount, 1); + auto *srVertex = make(vertex, 0u, edgeINCount, 1u); addVertex(srVertex); return srVertex; } spider::srdag::Vertex *spider::srdag::Graph::createHeadVertex(std::string name, size_t edgeINCount) { - auto *vertex = make(pisdf::VertexType::HEAD, std::move(name), 0, 1u); + auto *vertex = make(pisdf::VertexType::HEAD, std::move(name), 0u, 1u); auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::HEAD_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, edgeINCount, 1); + auto *srVertex = make(vertex, 0u, edgeINCount, 1u); addVertex(srVertex); return srVertex; } spider::srdag::Vertex *spider::srdag::Graph::createInitVertex(std::string name) { - auto *vertex = make(pisdf::VertexType::INIT, std::move(name), 0, 1u); + auto *vertex = make(pisdf::VertexType::INIT, std::move(name), 0u, 1u); auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::INIT_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, 0, 1); + auto *srVertex = make(vertex, 0u, 0u, 1u); addVertex(srVertex); return srVertex; } @@ -232,7 +232,7 @@ spider::srdag::Vertex *spider::srdag::Graph::createEndVertex(std::string name) { auto *runtimeInfo = vertex->runtimeInformation(); runtimeInfo->setKernelIx(rt::END_KERNEL_IX); specialVertexVector_.emplace_back(vertex); - auto *srVertex = make(vertex, 0, 1); + auto *srVertex = make(vertex, 0u, 1u); addVertex(srVertex); return srVertex; } diff --git a/libspider/graphs/srdag/SRDAGGraph.h b/libspider/graphs/srdag/SRDAGGraph.h index 00684ff6..56189d7b 100644 --- a/libspider/graphs/srdag/SRDAGGraph.h +++ b/libspider/graphs/srdag/SRDAGGraph.h @@ -63,9 +63,9 @@ namespace spider { Graph &operator=(Graph &&) = default; - Graph(const Graph &) = default; + Graph(const Graph &) = delete; - Graph &operator=(const Graph &) = default; + Graph &operator=(const Graph &) = delete; ~Graph() = default; diff --git a/libspider/graphs/srdag/SRDAGVertex.h b/libspider/graphs/srdag/SRDAGVertex.h index 566341a0..7a3de986 100644 --- a/libspider/graphs/srdag/SRDAGVertex.h +++ b/libspider/graphs/srdag/SRDAGVertex.h @@ -70,9 +70,9 @@ namespace spider { Vertex &operator=(Vertex &&) = default; - Vertex(const Vertex &) = default; + Vertex(const Vertex &) = delete; - Vertex &operator=(const Vertex &) = default; + Vertex &operator=(const Vertex &) = delete; ~Vertex(); diff --git a/libspider/monitor/Monitor.h b/libspider/monitor/Monitor.h index 614faffa..656c35c9 100644 --- a/libspider/monitor/Monitor.h +++ b/libspider/monitor/Monitor.h @@ -38,6 +38,7 @@ /* === Include(s) === */ #include +#include #include namespace spider { diff --git a/libspider/scheduling/launcher/TaskLauncher.cpp b/libspider/scheduling/launcher/TaskLauncher.cpp index 956f1494..0e7b063e 100644 --- a/libspider/scheduling/launcher/TaskLauncher.cpp +++ b/libspider/scheduling/launcher/TaskLauncher.cpp @@ -257,7 +257,7 @@ void spider::sched::TaskLauncher::sendSyncTask(SyncTask *task, const JobMessage /* == Set the execution task constraints == */ syncMessage.execConstraints_ = buildExecConstraints(task); /* == Set Fifos == */ - syncMessage.fifos_ = spider::make_unique(1, 1); + syncMessage.fifos_ = spider::make_unique(1u, 1u); auto fifo = message.fifos_->inputFifo(task->getDepIx()); fifo.count_ = 0; fifo.attribute_ = FifoAttribute::RW_ONLY; diff --git a/libspider/scheduling/memory/FifoAllocator.h b/libspider/scheduling/memory/FifoAllocator.h index da2d3f3d..03afe776 100644 --- a/libspider/scheduling/memory/FifoAllocator.h +++ b/libspider/scheduling/memory/FifoAllocator.h @@ -111,7 +111,7 @@ namespace spider { #ifndef _NO_BUILD_LEGACY_RT inline virtual spider::unique_ptr buildJobFifos(SRDAGTask *) { - return spider::make_unique(0, 0); + return spider::make_unique(0u, 0u); } #endif @@ -119,7 +119,7 @@ namespace spider { inline virtual void updateDynamicBuffersCount() { } inline virtual spider::unique_ptr buildJobFifos(PiSDFTask *, JobFifos *) { - return spider::make_unique(0, 0); + return spider::make_unique(0u, 0u); } /* === Getter(s) === */ @@ -140,8 +140,8 @@ namespace spider { private: const Schedule *schedule_ = nullptr; - size_t reservedMemory_ = 0; - size_t virtualMemoryAddress_ = 0; + size_t reservedMemory_ = 0u; + size_t virtualMemoryAddress_ = 0u; protected: explicit FifoAllocator(FifoAllocatorTraits traits) noexcept: traits_{ traits } { From 9faf2cf8022caf375ced4de0d2637618cac74498 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 20:02:06 +0100 Subject: [PATCH 11/15] [cpp] fixed namespace with stackArray. fixed allocator-test. --- libspider/api/spider.cpp | 2 +- libspider/memory/allocator.h | 22 +++++++++++----------- test/allocator-test/allocatorTest.cpp | 6 +++--- test/pisdf-test/CMakeLists.txt | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/libspider/api/spider.cpp b/libspider/api/spider.cpp index 7e76ea12..32bd4b41 100755 --- a/libspider/api/spider.cpp +++ b/libspider/api/spider.cpp @@ -116,7 +116,7 @@ static void printConfig(const spider::StartUpConfig &cfg) { /* === Function(s) definition === */ -std::array, STACK_COUNT> &stackArray() { +std::array, STACK_COUNT> &spider::stackArray() { static std::array, STACK_COUNT> stackArray = {{ nullptr }}; return stackArray; } diff --git a/libspider/memory/allocator.h b/libspider/memory/allocator.h index e1175964..f452946d 100755 --- a/libspider/memory/allocator.h +++ b/libspider/memory/allocator.h @@ -58,19 +58,19 @@ namespace spider { using value_type = T; -// using pointer = value_type *; -// using const_pointer = typename std::pointer_traits::template rebind; -// using void_pointer = typename std::pointer_traits::template rebind; -// using const_void_pointer = typename std::pointer_traits::template rebind; -// -// using difference_type = typename std::pointer_traits::difference_type; -// using size_t = typename std::make_unsigned::type; + using pointer = value_type *; + using const_pointer = typename std::pointer_traits::template rebind; + using void_pointer = typename std::pointer_traits::template rebind; + using const_void_pointer = typename std::pointer_traits::template rebind; + + using difference_type = typename std::pointer_traits::difference_type; + using size_t = typename std::make_unsigned::type; /* == Rebind SpiderAllocator to type U == */ -// template -// struct rebind { -// typedef allocator other; -// }; + template + struct rebind { + typedef allocator other; + }; /* === Constructors / Destructors === */ diff --git a/test/allocator-test/allocatorTest.cpp b/test/allocator-test/allocatorTest.cpp index 8693b949..d3b752b9 100644 --- a/test/allocator-test/allocatorTest.cpp +++ b/test/allocator-test/allocatorTest.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include class allocatorTest : public ::testing::Test { @@ -95,7 +96,7 @@ TEST_F(allocatorTest, linearAllocCtorTest) { } TEST_F(allocatorTest, linearAllocTest) { - auto *stack = spider::stackArray()[static_cast(StackID::OPTIMS)]; + auto &stack = spider::stackArray()[static_cast(StackID::OPTIMS)]; ASSERT_EQ(stack->setPolicy(nullptr), false) << "Stack::setPolicy should return false if nullptr as new policy."; ASSERT_EQ(stack->setPolicy(new LinearStaticAllocator(512)), true) << "Stack::setPolicy should return true."; auto &allocator = *(stack->policy()); @@ -313,8 +314,7 @@ TEST_F(allocatorTest, errorUsageTest) { void *tmp = nullptr; ASSERT_NO_THROW((tmp = spider::allocate())); ASSERT_NO_THROW(spider::deallocate(tmp)); - delete spider::stackArray()[static_cast(StackID::GENERAL)]; - spider::stackArray()[static_cast(StackID::GENERAL)] = nullptr; + spider::stackArray()[static_cast(StackID::GENERAL)].reset(); ASSERT_THROW((spider::allocator(StackID::GENERAL)), spider::Exception) << "spider::allocator should throw for nullptr stack."; } diff --git a/test/pisdf-test/CMakeLists.txt b/test/pisdf-test/CMakeLists.txt index de8d8812..c12aa763 100644 --- a/test/pisdf-test/CMakeLists.txt +++ b/test/pisdf-test/CMakeLists.txt @@ -1,4 +1,4 @@ -set(GRAPH_TARGET_NAME graph-${PROJECT_NAME}-test) +set(GRAPH_TARGET_NAME pisdf-${PROJECT_NAME}-test) # Add the test files set(${GRAPH_TARGET_NAME}_SRC From 5f71c01791ef59f5b9cb8a0caea649b019cf6c6f Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 20:02:30 +0100 Subject: [PATCH 12/15] [cpp17] fixed Vertex::setName --- libspider/graphs/pisdf/Vertex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libspider/graphs/pisdf/Vertex.cpp b/libspider/graphs/pisdf/Vertex.cpp index 55883ae6..9336bd30 100644 --- a/libspider/graphs/pisdf/Vertex.cpp +++ b/libspider/graphs/pisdf/Vertex.cpp @@ -214,7 +214,7 @@ void spider::pisdf::Vertex::setName(std::string name) { const auto size = name.size(); name_.reset(spider::make_n(size + 1)); std::move(std::begin(name), std::end(name), name_.get()); - name_[size - 1] = '\0'; + name_[size] = '\0'; } From 6f63ea4fbb69b92773092f482c9ed75502b75253 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 20:03:01 +0100 Subject: [PATCH 13/15] [cpp17] fixed seg fault in execDependenciesImpl.h. Fixed warnings in runtime-test --- .../numerical/detail/execDependenciesImpl.h | 4 +- .../appTest/reinforcement/include/clock.h | 2 +- .../appTest/reinforcement/include/common.h | 12 +++++- .../reinforcement/include/environment.h | 4 +- .../spider2-application-kernels.cpp | 1 - .../appTest/reinforcement/src/actor.cpp | 6 +-- .../appTest/reinforcement/src/clock.cpp | 14 +++---- .../appTest/reinforcement/src/common.cpp | 16 +++++--- .../appTest/reinforcement/src/environment.cpp | 39 +++++++++++-------- .../appTest/reinforcement/src/mlp.cpp | 12 +++--- 10 files changed, 63 insertions(+), 47 deletions(-) diff --git a/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h b/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h index dd8d9718..4e4a4892 100644 --- a/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h +++ b/libspider/graphs-tools/numerical/detail/execDependenciesImpl.h @@ -95,9 +95,7 @@ namespace spider { template inline void apply(const DependencyInfo &dep, const Function &func, Args &&...args) { - if constexpr(std::is_invocable_r_v) { - func(dep, std::forward(args)...); - } + func(dep, std::forward(args)...); } template diff --git a/test/runtime-test/appTest/reinforcement/include/clock.h b/test/runtime-test/appTest/reinforcement/include/clock.h index 47c14771..04e89a13 100644 --- a/test/runtime-test/appTest/reinforcement/include/clock.h +++ b/test/runtime-test/appTest/reinforcement/include/clock.h @@ -19,6 +19,6 @@ void startTiming(int stamp); // Stoping to record time for a given stamp. Returns the time in us -unsigned int stopTiming(int stamp); +unsigned long stopTiming(int stamp); #endif diff --git a/test/runtime-test/appTest/reinforcement/include/common.h b/test/runtime-test/appTest/reinforcement/include/common.h index f96fd4c5..f5b528af 100644 --- a/test/runtime-test/appTest/reinforcement/include/common.h +++ b/test/runtime-test/appTest/reinforcement/include/common.h @@ -21,12 +21,20 @@ #define M_PI 3.1415926535897932385f #endif -#define MODF(a, b) ((a) - (b) * (int)((a) / (b))) -#define POW2(x) ((x) * (x)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define ABS(x) ((x) < 0 ? -(x) : (x)) +template +constexpr T modf(const T a, const T b) { + return static_cast(a - b * static_cast(static_cast(a / b))); +} + +template +constexpr T pow2(const T a) { + return static_cast(a * a); +} + /** * @brief Samples a random variable following a normal distribution. * Each distribution is parameterized by mu and diff --git a/test/runtime-test/appTest/reinforcement/include/environment.h b/test/runtime-test/appTest/reinforcement/include/environment.h index 4dd4f395..22815d4c 100644 --- a/test/runtime-test/appTest/reinforcement/include/environment.h +++ b/test/runtime-test/appTest/reinforcement/include/environment.h @@ -33,12 +33,12 @@ void envInit(float *state_angular, float *state_observation); * @param state_observation List of output observation of the next state of the environment * @param reward Step reward from the actions applied */ -void step(int state_space_size, int action_space_size, int state_angular_size, +void step(int state_space_size, int action_space_size, IN float *state_angular_in, OUT float *state_angular_out, IN float *input_actions, OUT float *state_observation, OUT float *reward); -void step_noreward(int state_space_size, int action_space_size, int state_angular_size, +void step_noreward(int state_space_size, int action_space_size, IN float *state_angular_in, OUT float *state_angular_out, IN float *input_actions, OUT float *state_observation); diff --git a/test/runtime-test/appTest/reinforcement/spider2-application-kernels.cpp b/test/runtime-test/appTest/reinforcement/spider2-application-kernels.cpp index 23dfc98a..77a21dea 100644 --- a/test/runtime-test/appTest/reinforcement/spider2-application-kernels.cpp +++ b/test/runtime-test/appTest/reinforcement/spider2-application-kernels.cpp @@ -118,7 +118,6 @@ void spider::rl::stepRTKernel(const int64_t inputParams[], int64_t [], void *inp step( /* = state_space_size = */ static_cast(inputParams[0]), /* = action_space_size = */ static_cast(inputParams[1]), - /* = state_angular_size = */ static_cast(inputParams[2]), /* = state_angular_in = */ reinterpret_cast(inputs[0]), /* = state_angular_out = */ reinterpret_cast(outputs[0]), /* = input_actions = */ reinterpret_cast(inputs[1]), diff --git a/test/runtime-test/appTest/reinforcement/src/actor.cpp b/test/runtime-test/appTest/reinforcement/src/actor.cpp index c60b6711..56884585 100644 --- a/test/runtime-test/appTest/reinforcement/src/actor.cpp +++ b/test/runtime-test/appTest/reinforcement/src/actor.cpp @@ -153,7 +153,7 @@ void sigmaGen(OUT float *sigma) { sigma[0] = sigma_static; // Sigma decay - sigma_static = (float) (timestep) * (-SIGMA_GAUSSIAN / 20000.f) + SIGMA_GAUSSIAN; + sigma_static = static_cast(timestep) * (-SIGMA_GAUSSIAN / 20000.f) + SIGMA_GAUSSIAN; timestep = (timestep + 1) % 20000; if (timestep == 0) { sigma_static = SIGMA_GAUSSIAN; @@ -180,10 +180,10 @@ void saveNetWork(int n_layer, int w_size = size_layer_weights[i]; int b_size = size_layer_bias[i]; for (int w = 0; w < w_size; ++w) { - fprintf(file, "\t\t\tweights_out[%d] = %ff;\n", w, weights[i][w]); + fprintf(file, "\t\t\tweights_out[%d] = %lf;\n", w, weights[i][w]); } for (int b = 0; b < b_size; ++b) { - fprintf(file, "\t\t\tbias_out[%d] = %ff;\n", b, bias[i][b]); + fprintf(file, "\t\t\tbias_out[%d] = %lf;\n", b, bias[i][b]); } fprintf(file, "\t\t\tbreak;\n"); offset_bias += b_size; diff --git a/test/runtime-test/appTest/reinforcement/src/clock.cpp b/test/runtime-test/appTest/reinforcement/src/clock.cpp index 83c9bc64..380a8874 100644 --- a/test/runtime-test/appTest/reinforcement/src/clock.cpp +++ b/test/runtime-test/appTest/reinforcement/src/clock.cpp @@ -29,7 +29,7 @@ LARGE_INTEGER startTimes[MAX_STAMPS]; struct timeval startTimes[MAX_STAMPS]; #endif -double elapsedTimes[MAX_STAMPS]; +unsigned long elapsedTimes[MAX_STAMPS]; // Starting to record time for a given stamp void startTiming(int stamp) { @@ -41,8 +41,8 @@ void startTiming(int stamp) { } // Stoping to record time for a given stamp. Returns the time in us -unsigned int stopTiming(int stamp) { - unsigned int elapsedus; +unsigned long stopTiming(int stamp) { + unsigned long elapsedus; #ifdef _WIN32 LARGE_INTEGER frequency; LARGE_INTEGER t2; @@ -52,17 +52,17 @@ unsigned int stopTiming(int stamp) { QueryPerformanceFrequency(&frequency); // compute and print the elapsed time in millisec - elapsedTimes[stamp] = (t2.QuadPart - startTimes[stamp].QuadPart) * 1000.0 / frequency.QuadPart; + elapsedTimes[stamp] = (t2.QuadPart - startTimes[stamp].QuadPart) * 1000 / frequency.QuadPart; #else struct timeval t2; gettimeofday(&t2, NULL); // compute and print the elapsed time in millisec - elapsedTimes[stamp] = (t2.tv_sec - startTimes[stamp].tv_sec) * 1000.0; // sec to ms - elapsedTimes[stamp] += (t2.tv_usec - startTimes[stamp].tv_usec) / 1000.0; // us to ms + elapsedTimes[stamp] = (t2.tv_sec - startTimes[stamp].tv_sec) * 1000; // sec to ms + elapsedTimes[stamp] += (t2.tv_usec - startTimes[stamp].tv_usec) / 1000; // us to ms #endif - elapsedus = (int) (elapsedTimes[stamp] * 1000); + elapsedus = static_cast(elapsedTimes[stamp] * 1000); return elapsedus; } diff --git a/test/runtime-test/appTest/reinforcement/src/common.cpp b/test/runtime-test/appTest/reinforcement/src/common.cpp index 4a5743f7..861c9009 100644 --- a/test/runtime-test/appTest/reinforcement/src/common.cpp +++ b/test/runtime-test/appTest/reinforcement/src/common.cpp @@ -10,6 +10,10 @@ // file header #include "../include/common.h" +static float rand_norm_float() { + return static_cast(rand()) / static_cast(RAND_MAX); +} + float normalSampler(float mu, float sigma) { // Gaussian function: // 1 (x - mu)² @@ -30,17 +34,17 @@ float normalSampler(float mu, float sigma) { for (int i = 0; i < 2; ++i) { // Pre-compute constant value for (int n = 0; n < N_SAMPLING_SIZE; ++n) { - array_samples[n] = (float) (rand()) / (float) (RAND_MAX); + array_samples[n] = rand_norm_float(); } // Choose uniformly a random sample among the sampled ones - int sample = (int) ((float) (N_SAMPLING_SIZE) * (float) (rand()) / (float) (RAND_MAX)); + int sample = static_cast(static_cast(N_SAMPLING_SIZE) * rand_norm_float()); sample = MIN(N_SAMPLING_SIZE - 1, sample); array_uniform[i] = array_samples[sample]; } double U1 = array_uniform[0]; double U2 = array_uniform[1]; - float U1_part = (float) (sqrt(-2. * log(U1))); - float U2_part = (float) (cos(-2. * M_PI * U2)); + float U1_part = static_cast(sqrt(-2. * log(U1))); + float U2_part = static_cast(cos(-2. * M_PI * U2)); float standard_normal = U1_part * U2_part; return standard_normal * sigma + mu; } @@ -49,10 +53,10 @@ int randomSign(void) { // Samples 10 values float array_samples[10]; for (int n = 0; n < 10; ++n) { - float value = (float) (rand()) / (float) (RAND_MAX); + float value = rand_norm_float(); array_samples[n] = value; } - int sample = (int) (10.f * (float) (rand()) / (float) (RAND_MAX)); + int sample = static_cast(10.f * rand_norm_float()); sample = MIN(9, sample); float value = array_samples[sample]; if (value > 0.5f) { diff --git a/test/runtime-test/appTest/reinforcement/src/environment.cpp b/test/runtime-test/appTest/reinforcement/src/environment.cpp index 8a0cd071..2636056d 100644 --- a/test/runtime-test/appTest/reinforcement/src/environment.cpp +++ b/test/runtime-test/appTest/reinforcement/src/environment.cpp @@ -15,25 +15,30 @@ extern int preesmStopThreads; // Global Variable float last_action = 0.f; + +static float rand_norm_float() { + return static_cast(rand()) / static_cast(RAND_MAX); +} + void envInit(float *state_angular, float *state_observation) { // Use a random value to determine the sign of initial values - float sign = (float) (randomSign()); - state_angular[0] = sign * M_PI * (float) (rand()) / (float) (RAND_MAX); - sign = (float) (randomSign()); - state_angular[1] = sign * (float) (rand()) / (float) (RAND_MAX); + float sign = static_cast(randomSign()); + state_angular[0] = sign * static_cast(M_PI) * rand_norm_float(); + sign = static_cast(randomSign()); + state_angular[1] = sign * rand_norm_float(); //state_angular[0] = 2.631469f; // state_angular[1] = 0.818002f; fprintf(stderr, "Initial angular state:\n"); fprintf(stderr, " Angular Position: %f rad\n", state_angular[0]); fprintf(stderr, " Angular Velocity: %f rad/s\n", state_angular[1]); - state_observation[0] = (float) (cos((double) (state_angular[0]))); - state_observation[1] = (float) (sin((double) (state_angular[0]))); + state_observation[0] = static_cast(cos(static_cast(state_angular[0]))); + state_observation[1] = static_cast(sin(static_cast(state_angular[0]))); state_observation[2] = state_angular[1]; fprintf(stderr, "Initial state: %f - %f - %f\n", state_observation[0], state_observation[1], state_observation[2]); } -void step(int state_space_size, int action_space_size, int state_angular_size, +void step(int state_space_size, int action_space_size, IN float *state_angular_in, OUT float *state_angular_out, IN float *input_actions, OUT float *state_observation, OUT float *reward) { static long long int timestep = 0; @@ -47,7 +52,7 @@ void step(int state_space_size, int action_space_size, int state_angular_size, for (int i = 0; i < REWARD_SIZE; ++i) { mean_reward += reward_array[i]; } - mean_reward /= (float) (REWARD_SIZE); + mean_reward /= static_cast(REWARD_SIZE); if (ABS(mean_reward) < 0.1f) { fprintf(stderr, "System converged in: %lld time steps.\n", timestep); is_over = 1; @@ -80,8 +85,10 @@ void step(int state_space_size, int action_space_size, int state_angular_size, // Compute reward if (reward) { - reward[0] = POW2(MODF((theta + M_PI), (2.f * M_PI)) - M_PI) + 0.1f * POW2(angular_speed) + - 0.001f * (POW2(action_clip)); + const auto scaled_angular_speed = 0.1 * pow2(static_cast(angular_speed)); + const auto scaled_action_clip = 0.001 * (pow2(static_cast(action_clip))); + const auto scaled_theta = pow2(modf((static_cast(theta) + M_PI), (2. * M_PI)) - M_PI); + reward[0] = static_cast(scaled_theta + scaled_angular_speed + scaled_action_clip); reward[0] = -(reward[0]); reward_array[index_reward] = (*reward); index_reward = (index_reward + 1) % REWARD_SIZE; @@ -89,25 +96,25 @@ void step(int state_space_size, int action_space_size, int state_angular_size, } // Update angular state - angular_speed = angular_speed + ((-3.f) * G_CONSTANT / (2.f * LENGTH_CONSTANT) * (float) (sin(theta + M_PI)) + - (3.f / (MASS_CONSTANT * POW2(LENGTH_CONSTANT))) * action_clip) * TIME_DELTA; + angular_speed = angular_speed + ((-3.f) * G_CONSTANT / (2.f * LENGTH_CONSTANT) * + static_cast(sin(theta + static_cast(M_PI))) + + (3.f / (MASS_CONSTANT * pow2(LENGTH_CONSTANT))) * action_clip) * TIME_DELTA; theta = theta + angular_speed * TIME_DELTA; angular_speed = MIN(MAX(angular_speed, -MAX_SPEED), MAX_SPEED); state_angular_out[0] = theta; state_angular_out[1] = angular_speed; // Output the observations - state_observation[0] = (float) cos((double) theta); - state_observation[1] = (float) sin((double) theta); + state_observation[0] = static_cast(cos(static_cast(theta))); + state_observation[1] = static_cast(sin(static_cast(theta))); state_observation[2] = angular_speed; } -void step_noreward(int state_space_size, int action_space_size, int state_angular_size, +void step_noreward(int state_space_size, int action_space_size, IN float *state_angular_in, OUT float *state_angular_out, IN float *input_actions, OUT float *state_observation) { step(state_space_size, action_space_size, - state_angular_size, state_angular_in, state_angular_out, input_actions, diff --git a/test/runtime-test/appTest/reinforcement/src/mlp.cpp b/test/runtime-test/appTest/reinforcement/src/mlp.cpp index a7a513a0..4f22e636 100644 --- a/test/runtime-test/appTest/reinforcement/src/mlp.cpp +++ b/test/runtime-test/appTest/reinforcement/src/mlp.cpp @@ -70,12 +70,12 @@ void neuron(int input_size, /************************************/ void activateTanHyperbolic(IN float *input, OUT float *output) { - output[0] = (float) (tanh((double) (input[0]))); + output[0] = static_cast(tanh(static_cast(input[0]))); } void derivativeTanHyperbolic(IN float *input, OUT float *output) { - float f_x = (float) (tanh((double) (input[0]))); + float f_x = static_cast(tanh(static_cast(input[0]))); output[0] = 1 - (f_x * f_x); } @@ -111,12 +111,12 @@ void derivativeSoftSign(IN float *input, void activateLogistic(IN float *input, OUT float *output) { - output[0] = 1 / (1.f + (float) (exp((double) (-input[0])))); + output[0] = 1 / (1.f + static_cast(exp(static_cast(-input[0])))); } void derivativeLogistic(IN float *input, OUT float *output) { - float f_x = 1 / (1.f + (float) (exp((double) (-input[0])))); + float f_x = 1 / (1.f + static_cast(exp(static_cast(-input[0])))); output[0] = f_x * (1 - f_x); } @@ -180,7 +180,7 @@ void lossMSE(int size, // Compute element wise (label - prediction) * (label - prediction) double mse = 0; for (int i = 0; i < size; ++i) { - mse += (double) (predictions[i] - targets[i]) * (double) (predictions[i] - targets[i]); + mse += static_cast(predictions[i] - targets[i]) * static_cast(predictions[i] - targets[i]); } mse_output[0] = mse / 2.; } @@ -236,7 +236,7 @@ void applyAdamOptimizer(int size, fo_moment_out[i] = fo_moment_in[i] * beta1 + (1 - beta1) * g; // Biased first order moment estimate so_moment_out[i] = so_moment_in[i] * beta2 + (1 - beta2) * g * g; // Biased second raw order moment estimate - param_out[i] = param_in[i] - (float) (lr * fo_moment_out[i] / (epsilon_t + sqrt(so_moment_out[i]))); + param_out[i] = param_in[i] - static_cast(lr * fo_moment_out[i] / (epsilon_t + sqrt(so_moment_out[i]))); } } From 813af34b4fb1c7f7ec632c8a12093da374c03d74 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 20:12:10 +0100 Subject: [PATCH 14/15] [misc] fixing potential nullptr deferencing --- .../graphs-tools/transformation/pisdf/GraphFiring.cpp | 3 +++ .../transformation/srdag/singleRateTransformation.cpp | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp b/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp index a6ea42f4..b272cf68 100644 --- a/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp +++ b/libspider/graphs-tools/transformation/pisdf/GraphFiring.cpp @@ -305,6 +305,9 @@ spider::pisdf::GraphFiring::copyParameter(const std::shared_ptr &p paramParentIx = (*parent)->parent() ? (*parent)->parent()->ix() : throwNullptrException(); parent = &parentHandler->getParams()[paramParentIx]; } + if (!parent) { + throwNullptrException(); + } auto newParam = spider::make_shared(param->name(), *parent); newParam->setIx(param->ix()); return newParam; diff --git a/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp b/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp index 380b0c96..fb2e409e 100644 --- a/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp +++ b/libspider/graphs-tools/transformation/srdag/singleRateTransformation.cpp @@ -127,7 +127,11 @@ void spider::srdag::detail::updateParams(TransfoJob &job) { if (!graph->configVertexCount()) { for (auto ¶m : job.params_) { if (param->type() == pisdf::ParamType::INHERITED) { - const auto value = param->parent()->value(job.params_); + const auto *parent = param->parent(); + if (!parent) { + throwNullptrException(); + } + const auto value = parent->value(job.params_); const auto ix = param->ix(); param = spider::make_shared(param->name(), value); param->setIx(ix); @@ -207,10 +211,11 @@ std::shared_ptr spider::srdag::detail::copyParameter(const if (param->dynamic()) { std::shared_ptr p; if (param->type() == pisdf::ParamType::INHERITED) { - if (!param->parent()) { + const auto *parent = param->parent(); + if (!parent) { throwNullptrException(); } - const auto &parentParam = jobParams[param->parent()->ix()]; + const auto &parentParam = jobParams[parent->ix()]; p = spider::make_shared(param->name(), parentParam); } else { p = spider::make_shared(*param); From 02c4e5d8d6b1b1b7f23d7713b160be8164e119f2 Mon Sep 17 00:00:00 2001 From: Florian Arrestier Date: Sun, 16 Feb 2025 20:15:37 +0100 Subject: [PATCH 15/15] [misc] fixed GCC warning and usage of multithread during link time optim --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2914cc4a..cf0090e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ if (MSVC) install(FILES $ CONFIGURATIONS "Debug" "RelWithDebInfo" DESTINATION "${CMAKE_INSTALL_BINDIR}") else () set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg -O0 --coverage -ftest-coverage -fprofile-arcs") - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -DNDEBUG -fno-rtti -flto") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -DNDEBUG -fno-rtti -flto=auto") message(STATUS "Using Link Time Optimization (-flto) on GNU platforms.") if (${CMAKE_GENERATOR} MATCHES "MinGW Makefiles") # To prevent a redefinition conflict