///\file /****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2016 John Wellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef ETL_UTILITY_INCLUDED #define ETL_UTILITY_INCLUDED #include "platform.h" #include "type_traits.h" #include "private/tuple_element.h" #include "private/tuple_size.h" #if defined(ETL_IN_UNIT_TEST) || ETL_USING_STL #if ETL_USING_CPP11 #include #else #include #endif #endif ///\defgroup utility utility ///\ingroup utilities namespace etl { #if ETL_USING_CPP11 //****************************************************************************** template constexpr typename etl::remove_reference::type&& move(T&& t) ETL_NOEXCEPT { return static_cast::type&&>(t); } //****************************************************************************** template constexpr T&& forward(typename etl::remove_reference::type& t) ETL_NOEXCEPT { return static_cast(t); } template constexpr T&& forward(typename etl::remove_reference::type&& t) ETL_NOEXCEPT { ETL_STATIC_ASSERT(!etl::is_lvalue_reference::value, "Invalid rvalue to lvalue conversion"); return static_cast(t); } //****************************************************************************** /// See std::forward_like /// https://en.cppreference.com/w/cpp/utility/forward_like Returns a reference /// to x which has similar properties to T&&. ///\return /// If etl::remove_reference_t is const then returns a const reference if U /// is an lvalue, otherwise a const rvalue reference. If /// etl::remove_reference_t is not const then returns a reference if U is /// an lvalue, otherwise an rvalue reference. //****************************************************************************** //*********************************** /// T is const & lvalue. //*********************************** template ETL_NODISCARD ETL_CONSTEXPR etl::enable_if_t>::value && etl::is_lvalue_reference::value, const etl::remove_reference_t&> forward_like(U&& u) ETL_NOEXCEPT { return static_cast&>(u); } //*********************************** /// T is const & rvalue. //*********************************** template ETL_NODISCARD ETL_CONSTEXPR etl::enable_if_t>::value && !etl::is_lvalue_reference::value, const etl::remove_reference_t&&> forward_like(U&& u) ETL_NOEXCEPT { return static_cast&&>(u); } //*********************************** /// T is not const & lvalue. //*********************************** template ETL_NODISCARD ETL_CONSTEXPR etl::enable_if_t>::value && etl::is_lvalue_reference::value, etl::remove_reference_t&> forward_like(U&& u) ETL_NOEXCEPT { return static_cast&>(u); } //*********************************** /// T is not const & rvalue. //*********************************** template ETL_NODISCARD ETL_CONSTEXPR etl::enable_if_t>::value && !etl::is_lvalue_reference::value, etl::remove_reference_t&&> forward_like(U&& u) ETL_NOEXCEPT { return static_cast&&>(u); } //*********************************** // Defines the type that forward_like would cast to. //*********************************** template using forward_like_t = decltype(etl::forward_like(etl::declval())); #endif //*********************************** // Gets the underlying type of an enum. //*********************************** template ETL_CONSTEXPR typename underlying_type::type to_underlying(T value) ETL_NOEXCEPT { return static_cast::type>(value); } // We can't have std::swap and etl::swap templates coexisting in the unit // tests as the compiler will be unable to decide which one to use, due to // ADL. #if ETL_NOT_USING_STL && !defined(ETL_IN_UNIT_TEST) //*************************************************************************** // swap template ETL_CONSTEXPR14 void swap(T& a, T& b) ETL_NOEXCEPT { T temp(ETL_MOVE(a)); a = ETL_MOVE(b); b = ETL_MOVE(temp); } template < class T, size_t Size > ETL_CONSTEXPR14 void swap(T (&a)[Size], T (&b)[Size]) ETL_NOEXCEPT { for (size_t i = 0UL; i < Size; ++i) { swap(a[i], b[i]); } } #endif //*************************************************************************** ///\brief pair holds two objects of arbitrary type /// ///\tparam T1, T2 The types of the elements that the pair stores //*************************************************************************** template struct pair { typedef T1 first_type; ///< @c first_type is the first bound type typedef T2 second_type; ///< @c second_type is the second bound type T1 first; ///< @c first is a copy of the first object T2 second; ///< @c second is a copy of the second object //*************************************************************************** ///\brief Default constructor /// /// The default constructor creates @c first and @c second using their /// respective default constructors. //*************************************************************************** ETL_CONSTEXPR pair() : first(T1()) , second(T2()) { } //*************************************************************************** ///\brief Constructor from parameters /// /// Two objects may be passed to a @c pair constructor to be copied. //*************************************************************************** ETL_CONSTEXPR14 pair(const T1& a, const T2& b) : first(a) , second(b) { } #if ETL_USING_CPP11 //*************************************************************************** ///\brief Move constructor from parameters. //*************************************************************************** template ETL_CONSTEXPR14 pair(U1&& a, U2&& b) : first(etl::forward(a)) , second(etl::forward(b)) { } #endif //*************************************************************************** ///\brief Copy constructor /// /// There is also a templated copy constructor for the @c pair class itself. //*************************************************************************** template ETL_CONSTEXPR14 pair(const pair& other) : first(other.first) , second(other.second) { } /// Copy constructor pair(const pair& other) : first(other.first) , second(other.second) { } #if ETL_USING_CPP11 /// Move constructor template ETL_CONSTEXPR14 pair(pair&& other) : first(etl::forward(other.first)) , second(etl::forward(other.second)) { } #endif #if defined(ETL_IN_UNIT_TEST) || ETL_USING_STL /// Converting to std::pair template operator std::pair() { return std::make_pair(first, second); } /// Constructing from std::pair template pair(const std::pair& other) : first(other.first) , second(other.second) { } #if ETL_USING_CPP11 /// Constructing to etl::pair template pair(std::pair&& other) : first(etl::forward(other.first)) , second(etl::forward(other.second)) { } #endif #endif void swap(pair& other) { using ETL_OR_STD::swap; swap(first, other.first); swap(second, other.second); } pair& operator=(const pair& other) { first = other.first; second = other.second; return *this; } template pair& operator=(const pair& other) { first = other.first; second = other.second; return *this; } #if ETL_USING_CPP11 pair& operator=(pair&& other) { first = etl::forward(other.first); second = etl::forward(other.second); return *this; } template pair& operator=(pair&& other) { first = etl::forward(other.first); second = etl::forward(other.second); return *this; } #endif }; //*************************************************************************** ///\brief A convenience wrapper for creating a @ref pair from two objects. /// ///\param a The first object. ///\param b The second object. /// ///\return A newly-constructed @ref pair object of the appropriate type. //*************************************************************************** #if ETL_USING_CPP11 template inline pair make_pair(T1&& a, T2&& b) { return pair(etl::forward(a), etl::forward(b)); } #else template inline pair make_pair(T1 a, T2 b) { return pair(a, b); } #endif #if ETL_USING_CPP11 //****************************************************************************** template struct tuple_element > { ETL_STATIC_ASSERT(Index < 2U, "pair has only 2 elements"); }; template struct tuple_element<0U, ETL_OR_STD::pair > { typedef T1 type; }; template struct tuple_element<1U, ETL_OR_STD::pair > { typedef T2 type; }; //****************************************************************************** template struct tuple_size> : public etl::integral_constant { }; #endif //****************************************************************************** template inline void swap(pair& a, pair& b) { a.swap(b); } /// Two pairs of the same type are equal if their members are equal. template inline bool operator==(const pair& a, const pair& b) { #include "private/diagnostic_float_equal_push.h" return (a.first == b.first) && !(a.second < b.second) && !(a.second > b.second); #include "private/diagnostic_pop.h" } /// Uses @c operator== to find the result. template inline bool operator!=(const pair& a, const pair& b) { return !(a == b); } template inline bool operator<(const pair& a, const pair& b) { return (a.first < b.first) || (!(b.first < a.first) && (a.second < b.second)); } /// Uses @c operator< to find the result. template inline bool operator>(const pair& a, const pair& b) { return (b < a); } /// Uses @c operator< to find the result. template inline bool operator<=(const pair& a, const pair& b) { return !(b < a); } /// Uses @c operator< to find the result. template inline bool operator>=(const pair& a, const pair& b) { return !(a < b); } //*************************************************************************** ///\brief Functor to select @ref pair::first /// ///\ref select1st is a functor object that takes a single argument, a @ref /// pair, and returns the @ref pair::first /// element. /// ///\b Example ///\snippet test_utility.cpp test_select1st_example /// ///\tparam TPair The function object's argument type. /// ///\see select2nd //*************************************************************************** template struct select1st { typedef typename TPair::first_type type; ///< type of member @ref pair::first. //*************************************************************************** ///\brief Function call that return @c p.first. ///\return a reference to member @ref pair::first of the @c pair `p` //*************************************************************************** type& operator()(TPair& p) const { return p.first; } //*************************************************************************** ///\copydoc operator()(TPair&)const // const type& operator()(const TPair& p) const { return p.first; } }; //*************************************************************************** ///\brief Functor to select @ref pair::second /// ///\ref select2nd is a functor object that takes a single argument, a @ref /// pair, and returns the @ref pair::second /// element. /// ///\b Example ///\snippet test_utility.cpp test_select2nd_example /// ///\tparam TPair The function object's argument type. /// ///\see select1st //*************************************************************************** template struct select2nd { typedef typename TPair::second_type type; ///< type of member @ref pair::second. //*************************************************************************** ///\brief Function call. The return value is `p.second`. ///\return a reference to member `second` of the pair `p`. //*************************************************************************** type& operator()(TPair& p) const { return p.second; } //*************************************************************************** ///\copydoc operator()(TPair&)const //*************************************************************************** const type& operator()(const TPair& p) const { return p.second; } }; #if ETL_NOT_USING_STL || ETL_CPP14_NOT_SUPPORTED //*************************************************************************** /// exchange (const) //*************************************************************************** template T exchange(T& object, const T& new_value) { T old_value = object; object = new_value; return old_value; } template T exchange(T& object, const U& new_value) { T old_value = object; object = new_value; return old_value; } #else //*************************************************************************** /// exchange (const) //*************************************************************************** template T exchange(T& object, const U& new_value) { return std::exchange(object, new_value); } #endif //*************************************************************************** /// as_const //*************************************************************************** template typename etl::add_const::type& as_const(T& t) { return t; } //*************************************************************************** /// integer_sequence //*************************************************************************** #if ETL_USING_CPP11 template class integer_sequence { public: ETL_STATIC_ASSERT(etl::is_integral::value, "Integral types only"); typedef T value_type; static ETL_CONSTEXPR size_t size() ETL_NOEXCEPT { return sizeof...(Integers); } }; namespace private_integer_sequence { template struct make_index_sequence; template struct make_index_sequence> { using type = typename make_index_sequence< Count - 1, etl::integer_sequence>::type; }; template struct make_index_sequence<0, etl::integer_sequence> { using type = etl::integer_sequence; }; template struct offset_index_sequence; template struct offset_index_sequence> { using type = etl::integer_sequence; }; } // namespace private_integer_sequence //*********************************** /// Make an integer sequence. //*********************************** template using make_index_sequence = typename private_integer_sequence::make_index_sequence< Count, etl::integer_sequence>::type; //*********************************** /// Make an integer sequence with an offset. //*********************************** template using make_index_sequence_with_offset = typename private_integer_sequence::offset_index_sequence< Offset, etl::make_index_sequence>::type; //*********************************** // Helper to support both parameter packs and etl::type_list // Forward declare etl::type_list to allow use without including type_list.h template struct type_list; namespace private_make_index_sequence_for { // Generic pack form template struct impl { using type = typename private_integer_sequence::make_index_sequence< sizeof...(TTypes), etl::integer_sequence>::type; }; // etl::type_list form template struct impl> : impl { }; } // namespace private_make_index_sequence_for //*********************************** /// Make an index sequence for a parameter pack of types or an /// etl::type_list. Accepts either a parameter pack of types or a single /// etl::type_list //*********************************** template using make_index_sequence_for = typename private_make_index_sequence_for::impl::type; //*********************************** /// An index sequence. //*********************************** template using index_sequence = etl::integer_sequence; //************************************ /// Alias for make_index_sequence_for. //************************************ template using index_sequence_for = typename etl::make_index_sequence_for; //************************************ /// Concatenates two index_sequences. //************************************ template struct index_sequence_cat; template struct index_sequence_cat, etl::index_sequence> { using type = etl::index_sequence; }; template using index_sequence_cat_t = typename index_sequence_cat::type; //************************************ /// Pushes an index to the front of an index_sequence. //************************************ template struct index_sequence_push_front; template struct index_sequence_push_front, Index> { // Adds the new index to the front of the sequence. using type = etl::index_sequence; }; template using index_sequence_push_front_t = typename index_sequence_push_front::type; //************************************ /// Pop an index from the front of an index_sequence. //************************************ template struct index_sequence_pop_front; template <> struct index_sequence_pop_front> { using type = etl::index_sequence<>; }; template struct index_sequence_pop_front> { // Removes the front index by declaring the type to be the tail of the // sequence. using type = etl::index_sequence; }; template using index_sequence_pop_front_t = typename index_sequence_pop_front::type; //************************************ /// Pushes an index to the back of an index_sequence. //************************************ template struct index_sequence_push_back; template struct index_sequence_push_back, Index> { // Adds the new index to the back of the sequence by concatenating the new // index with the sequence. using type = etl::index_sequence; }; template using index_sequence_push_back_t = typename index_sequence_push_back::type; //************************************ /// Pop an index from the back of an index_sequence. //************************************ template struct index_sequence_pop_back; // Pop back of and empty sequence is an empty sequence. template <> struct index_sequence_pop_back> { using type = etl::index_sequence<>; }; // Pop back of a single element sequence is an empty sequence. // The single element is never added to the result, so is effectively removed. // This is the terminating specialisation for the general case. template struct index_sequence_pop_back> { using type = etl::index_sequence<>; }; // Multi element sequence. Pop back is the front element concatenated with the // pop back of the tail. template struct index_sequence_pop_back> { // Removes the last index by concatenating the front index with the pop back // of the tail. The last index is never added to the result, so is // effectively removed. using type = etl::index_sequence_cat_t< etl::index_sequence, typename index_sequence_pop_back>::type>; }; template using index_sequence_pop_back_t = typename index_sequence_pop_back::type; //************************************ /// Gets the index at the Nth position in an index_sequence. //************************************ template struct index_sequence_at; template struct index_sequence_at, Nth> { template struct dependent_false : etl::false_type { }; static_assert(dependent_false::value, "Nth out of range for index_sequence_at"); }; // When Nth is 0, the index at the Nth position is the front index of the // sequence. template struct index_sequence_at, 0> { static constexpr size_t value = Index; }; // When Nth is greater than 0, recurse with the tail of the sequence and Nth // - 1. template struct index_sequence_at, Nth> { static_assert(Nth < sizeof...(Indices) + 1U, "Nth out of range for index_sequence_at"); static constexpr size_t value = index_sequence_at, Nth - 1U>::value; }; #if ETL_USING_CPP17 template inline constexpr size_t index_sequence_at_v = index_sequence_at::value; #endif //************************************ /// Checks if an index_sequence contains a value. //************************************ template struct index_sequence_contains; // An empty sequence does not contain any value. template struct index_sequence_contains, Value> : etl::false_type { }; // When the front index of the sequence is the value, the sequence contains // the value. When the front index of the sequence is not the value, recurse // with the tail of the sequence. template struct index_sequence_contains, Value> : etl::integral_constant< bool, (Index == Value) || index_sequence_contains, Value>::value> { }; #if ETL_USING_CPP17 template inline constexpr bool index_sequence_contains_v = index_sequence_contains::value; #endif //*************************************************************************** /// Defines a new index_sequence by removing duplicate indexes from a given /// index_sequence, preserving the first occurrence. //*************************************************************************** namespace private_index_sequence { template struct type_index_sequence_impl; // When the front index of the sequence is not in the unique sequence, add // it to the back of the unique sequence and recurse with the tail of the // sequence. template struct type_index_sequence_impl, etl::index_sequence> { // If the index is already in the unique sequence, do not add it again. // Otherwise, add it to the back of the unique sequence. using type = typename etl::conditional_t< etl::index_sequence_contains, Index>::value, type_index_sequence_impl, etl::index_sequence>, type_index_sequence_impl< etl::index_sequence, etl::index_sequence_push_back_t, Index>>>::type; }; // When the sequence is empty, the unique sequence is the result. template struct type_index_sequence_impl, etl::index_sequence> { using type = etl::index_sequence; }; } // namespace private_index_sequence template struct index_sequence_unique; template struct index_sequence_unique> { using type = typename private_index_sequence::type_index_sequence_impl< etl::index_sequence, etl::index_sequence<>>::type; }; #if ETL_USING_CPP11 template using index_sequence_unique_t = typename etl::index_sequence_unique::type; #endif //*************************************************************************** /// Checks that all of the indices in an index_sequence are unique. //*************************************************************************** template struct index_sequence_is_unique; template struct index_sequence_is_unique> : etl::bool_constant< etl::is_same< etl::index_sequence, etl::index_sequence_unique_t>>::value> { }; #if ETL_USING_CPP17 template inline constexpr bool index_sequence_is_unique_v = index_sequence_is_unique::type::value; #endif //*************************************************************************** /// Checks if the index_sequence is empty. //*************************************************************************** template struct index_sequence_is_empty; template <> struct index_sequence_is_empty> : etl::true_type { }; template struct index_sequence_is_empty> : etl::false_type { }; #if ETL_USING_CPP17 template inline constexpr bool index_sequence_is_empty_v = index_sequence_is_empty::value; #endif #endif //*************************************************************************** /// 2D coordinate type. //*************************************************************************** template struct coordinate_2d { coordinate_2d() : x(T(0)) , y(T(0)) { } coordinate_2d(T x_, T y_) : x(x_) , y(y_) { } friend bool operator==(const coordinate_2d& lhs, const coordinate_2d& rhs) { return (lhs.x == rhs.x) && (lhs.y == rhs.y); } friend bool operator!=(const coordinate_2d& lhs, const coordinate_2d& rhs) { return !(lhs == rhs); } T x; T y; }; //*************************************************************************** /// in_place disambiguation tags. //*************************************************************************** //************************* struct in_place_t { explicit ETL_CONSTEXPR in_place_t() {} }; #if ETL_USING_CPP17 inline constexpr in_place_t in_place{}; #endif //************************* template struct in_place_type_t { explicit ETL_CONSTEXPR in_place_type_t() {} }; #if ETL_USING_CPP17 template inline constexpr in_place_type_t in_place_type{}; #endif //************************* template struct in_place_index_t { explicit ETL_CONSTEXPR in_place_index_t() {} }; #if ETL_USING_CPP17 template inline constexpr in_place_index_t in_place_index{}; #endif #if ETL_USING_CPP11 //************************************************************************* // A function wrapper for free/global functions. // Deprecated. // See etl::function_ptr_as_functor for a runtime time wrapper option. // See etl::function_as_functor for a compile time wrapper option. //************************************************************************* template class ETL_DEPRECATED functor { public: //********************************* /// Constructor. //********************************* constexpr functor(TReturn (*ptr_)(TParams...)) : ptr(ptr_) { } //********************************* /// Const function operator. //********************************* constexpr TReturn operator()(TParams... args) const { return ptr(etl::forward(args)...); } private: /// The pointer to the function. TReturn (*ptr)(TParams...); }; //***************************************************************************** // Wrap a member function with a static free function. // Creates a static member function that calls the specified member function. // Deprecated // See etl::member_function_as_static //***************************************************************************** template class member_function_wrapper; template class ETL_DEPRECATED member_function_wrapper { public: template static constexpr TReturn function(TParams... params) { return (Instance.*Method)(etl::forward(params)...); } }; //***************************************************************************** // Wrap a functor with a static free function. // Creates a static member function that calls the specified functor. // Deprecated // See etl::functor_as_static //***************************************************************************** template class functor_wrapper; template class functor_wrapper { public: template static constexpr TReturn function(TParams... params) { return Instance(etl::forward(params)...); } }; #endif #if ETL_USING_CPP17 //***************************************************************************** // Wraps a functor with a static free function at compile time. // Creates a static member 'call' that calls the specified functor. //***************************************************************************** template struct functor_as_static { template static constexpr auto call(TArgs&&... args) { return (Instance.operator())(etl::forward(args)...); } }; //***************************************************************************** // Wraps a member function with a static free function at compile time. // Creates a static member 'call' that calls the specified member function. //***************************************************************************** template struct member_function_as_static { template static constexpr auto call(TArgs&&... args) { return (Instance.*Method)(etl::forward(args)...); } }; //***************************************************************************** // Wraps a member function with a functor at compile time. // Creates a functor that calls the specified member function. //***************************************************************************** template class member_function_as_functor { public: template constexpr auto operator()(TArgs&&... args) const -> decltype((Instance.*Method)(etl::forward(args)...)) { return (Instance.*Method)(etl::forward(args)...); } }; //***************************************************************************** // Wraps a function with a functor at compile time. // Creates a functor that calls the specified free function. //***************************************************************************** template class function_as_functor { public: template constexpr auto operator()(TArgs&&... args) const -> decltype(Function(etl::forward(args)...)) { return Function(etl::forward(args)...); } }; #endif #if ETL_USING_CPP11 //***************************************************************************** // Wraps a function pointer with a functor at run time. // Creates a functor that calls the specified free function. //***************************************************************************** template class function_ptr_as_functor; template class function_ptr_as_functor { public: //********************************* /// Constructor. //********************************* constexpr function_ptr_as_functor(TReturn (*ptr_)(TArgs...)) : ptr(ptr_) { } //********************************* /// Const function operator. //********************************* constexpr TReturn operator()(TArgs... args) const { return ptr(etl::forward(args)...); } private: /// The pointer to the function. TReturn (*ptr)(TArgs...); }; #endif #if ETL_USING_CPP17 && !defined(ETL_FORCE_CPP11_NONTYPE) //***************************************************************************** // Wraps a non-type template parameter as a type. //***************************************************************************** template struct nontype_t { static constexpr decltype(Value) value = Value; }; #elif ETL_USING_CPP11 //***************************************************************************** // Wraps a non-type template parameter as a type. //***************************************************************************** template struct nontype_t { static constexpr T value = Value; }; #endif } // namespace etl #endif