mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Added etl::function traits
Removed private delegate function_traits Added etl::type_list + etl::nth_type overloads
This commit is contained in:
parent
dd4d0088c5
commit
1f897e1095
136
include/etl/function_traits.h
Normal file
136
include/etl/function_traits.h
Normal file
@ -0,0 +1,136 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2025 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_FUNCTION_TRAITS_INCLUDED
|
||||
#define ETL_FUNCTION_TRAITS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_list.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// A template to extract the function type traits.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct function_traits;
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename... TArgs>
|
||||
struct function_traits<TReturn(*)(TArgs...)>
|
||||
{
|
||||
using function_type = TReturn(TArgs...); ///< The signature of the function.
|
||||
using return_type = TReturn; ///< The return type.
|
||||
using object_type = void; ///< The object type, if a member function.
|
||||
using argument_types = etl::type_list<TArgs...>; ///< An etl::type_list containing the function argument types.
|
||||
|
||||
static constexpr bool is_function = true; ///< <b>true</b> if the type is a free, static or global function, otherwise <b>false</b>.
|
||||
static constexpr bool is_member_function = false; ///< <b>true</b> if the type is a member function, otherwise <b>false</b>.
|
||||
static constexpr bool is_const = false; ///< <b>true</b> if the type is a const member function, otherwise <b>false</b>.
|
||||
static constexpr size_t argument_count = sizeof...(TArgs); ///< The number of arguments that the function takes.
|
||||
};
|
||||
|
||||
template <typename TReturn, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(*)(TArgs...)>::is_function;
|
||||
|
||||
template <typename TReturn, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(*)(TArgs...)>::is_member_function;
|
||||
|
||||
template <typename TReturn, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(*)(TArgs...)>::is_const;
|
||||
|
||||
template <typename TReturn, typename... TArgs>
|
||||
constexpr size_t function_traits<TReturn(*)(TArgs...)>::argument_count;
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for member function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
struct function_traits<TReturn(TObject::*)(TArgs...)>
|
||||
{
|
||||
using function_type = TReturn(TArgs...); ///< The signature of the function.
|
||||
using return_type = TReturn; ///< The return type.
|
||||
using object_type = TObject; ///< The object type, if a member function.
|
||||
using argument_types = etl::type_list<TArgs...>; ///< An etl::type_list containing the function argument types.
|
||||
|
||||
static constexpr bool is_function = false; ///< <b>true</b> if the type is a free, static or global function, otherwise <b>false</b>.
|
||||
static constexpr bool is_member_function = true; ///< <b>true</b> if the type is a member function, otherwise <b>false</b>.
|
||||
static constexpr bool is_const = false; ///< <b>true</b> if the type is a const member function, otherwise <b>false</b>.
|
||||
static constexpr size_t argument_count = sizeof...(TArgs); ///< The number of arguments that the function takes.
|
||||
};
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...)>::is_function;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...)>::is_member_function;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...)>::is_const;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr size_t function_traits<TReturn(TObject::*)(TArgs...)>::argument_count;
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for const member function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
struct function_traits<TReturn(TObject::*)(TArgs...) const>
|
||||
{
|
||||
using function_type = TReturn(TArgs...); ///< The signature of the function.
|
||||
using return_type = TReturn; ///< The return type.
|
||||
using object_type = TObject; ///< The object type, if a member function.
|
||||
using argument_types = etl::type_list<TArgs...>; ///< An etl::type_list containing the function argument types.
|
||||
|
||||
static constexpr bool is_function = false; ///< <b>true</b> if the type is a free, static or global function, otherwise <b>false</b>.
|
||||
static constexpr bool is_member_function = true; ///< <b>true</b> if the type is a member function, otherwise <b>false</b>.
|
||||
static constexpr bool is_const = true; ///< <b>true</b> if the type is a const member function, otherwise <b>false</b>.
|
||||
static constexpr size_t argument_count = sizeof...(TArgs); ///< The number of arguments that the function takes.
|
||||
};
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...) const>::is_function;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...) const>::is_member_function;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr bool function_traits<TReturn(TObject::*)(TArgs...) const>::is_const;
|
||||
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
constexpr size_t function_traits<TReturn(TObject::*)(TArgs...) const>::argument_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -63,7 +63,7 @@ namespace etl
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Finds the 0thth type in a variadic type parameter.
|
||||
/// Finds the 0th type in a variadic type parameter.
|
||||
//***************************************************************************
|
||||
template <size_t N>
|
||||
struct nth_type<N>
|
||||
|
||||
@ -52,6 +52,7 @@ Original publication: https://www.codeproject.com/Articles/1170503/The-Impossibl
|
||||
#include "../error_handler.h"
|
||||
#include "../exception.h"
|
||||
#include "../type_traits.h"
|
||||
#include "../function_traits.h"
|
||||
#include "../utility.h"
|
||||
#include "../optional.h"
|
||||
|
||||
@ -659,57 +660,6 @@ namespace etl
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP17
|
||||
namespace private_delegate
|
||||
{
|
||||
//***************************************************************************
|
||||
/// A template to extract the function type traits.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct function_traits;
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename... TArgs>
|
||||
struct function_traits<TReturn(*)(TArgs...)>
|
||||
{
|
||||
using function_type = TReturn(TArgs...);
|
||||
|
||||
enum : bool
|
||||
{
|
||||
is_const = false
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for member function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
struct function_traits<TReturn(TObject::*)(TArgs...)>
|
||||
{
|
||||
using function_type = TReturn(TArgs...);
|
||||
|
||||
enum : bool
|
||||
{
|
||||
is_const = false
|
||||
};
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation for const member function pointers
|
||||
//***************************************************************************
|
||||
template <typename TReturn, typename TObject, typename... TArgs>
|
||||
struct function_traits<TReturn(TObject::*)(TArgs...) const>
|
||||
{
|
||||
using function_type = TReturn(TArgs...);
|
||||
|
||||
enum : bool
|
||||
{
|
||||
is_const = true
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Make a delegate from a free function.
|
||||
//*************************************************************************
|
||||
@ -717,7 +667,7 @@ namespace etl
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate() ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(Function)>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(Function)>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<Function>();
|
||||
}
|
||||
@ -729,7 +679,7 @@ namespace etl
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate(TLambda& instance) ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(&TLambda::operator())>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(&TLambda::operator())>::function_type;
|
||||
|
||||
return etl::delegate<function_type>(instance);
|
||||
}
|
||||
@ -741,7 +691,7 @@ namespace etl
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate() ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(&T::operator())>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(&T::operator())>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<T, Instance>();
|
||||
}
|
||||
@ -749,11 +699,11 @@ namespace etl
|
||||
//*************************************************************************
|
||||
/// Make a delegate from a member function at compile time.
|
||||
//*************************************************************************
|
||||
template <typename T, auto Method, T& Instance, typename = etl::enable_if_t<!private_delegate::function_traits<decltype(Method)>::is_const>>
|
||||
template <typename T, auto Method, T& Instance, typename = etl::enable_if_t<!etl::function_traits<decltype(Method)>::is_const>>
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate() ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(Method)>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(Method)>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<T, Method, Instance>();
|
||||
}
|
||||
@ -761,11 +711,11 @@ namespace etl
|
||||
//*************************************************************************
|
||||
/// Make a delegate from a const member function at compile time.
|
||||
//*************************************************************************
|
||||
template <typename T, auto Method, const T& Instance, typename = etl::enable_if_t<private_delegate::function_traits<decltype(Method)>::is_const>>
|
||||
template <typename T, auto Method, const T& Instance, typename = etl::enable_if_t<etl::function_traits<decltype(Method)>::is_const>>
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate() ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(Method)>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(Method)>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<T, Method, Instance>();
|
||||
}
|
||||
@ -777,7 +727,7 @@ namespace etl
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate(T& instance) ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(Method)>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(Method)>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<T, Method>(instance);
|
||||
}
|
||||
@ -789,7 +739,7 @@ namespace etl
|
||||
ETL_NODISCARD
|
||||
constexpr auto make_delegate(const T& instance) ETL_NOEXCEPT
|
||||
{
|
||||
using function_type = typename etl::private_delegate::function_traits<decltype(Method)>::function_type;
|
||||
using function_type = typename etl::function_traits<decltype(Method)>::function_type;
|
||||
|
||||
return etl::delegate<function_type>::template create<T, Method>(instance);
|
||||
}
|
||||
|
||||
186
include/etl/type_list.h
Normal file
186
include/etl/type_list.h
Normal file
@ -0,0 +1,186 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2025 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_TYPE_LIST_INCLUDED
|
||||
#define ETL_TYPE_LIST_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "nth_type.h"
|
||||
#include "static_assert.h"
|
||||
#include "type_traits.h"
|
||||
#include "utility.h"
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Type list forward declaration.
|
||||
//***************************************************************************
|
||||
template <typename... TTypes>
|
||||
struct type_list;
|
||||
|
||||
//***************************************************************************
|
||||
/// The empty type list.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
struct type_list<>
|
||||
{
|
||||
static constexpr size_t size = 0U;
|
||||
|
||||
using index_sequence_type = etl::make_index_sequence<0>; ///< The index_sequence type for this type_list.
|
||||
|
||||
private:
|
||||
|
||||
type_list() ETL_DELETE;
|
||||
type_list(const type_list&) ETL_DELETE;
|
||||
type_list& operator =(const type_list&) ETL_DELETE;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Recursive type list implementation for multiple types.
|
||||
//***************************************************************************
|
||||
template <typename THead, typename... TTail>
|
||||
struct type_list<THead, TTail...> : type_list<TTail...>
|
||||
{
|
||||
using type = THead;
|
||||
|
||||
static constexpr size_t size = sizeof...(TTail) + 1U;
|
||||
|
||||
using index_sequence_type = etl::make_index_sequence<sizeof...(TTail) + 1U>; ///< The index_sequence type for this type_list.
|
||||
|
||||
private:
|
||||
|
||||
type_list() ETL_DELETE;
|
||||
type_list(const type_list&) ETL_DELETE;
|
||||
type_list& operator =(const type_list&) ETL_DELETE;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Type list implementation for one type.
|
||||
//***************************************************************************
|
||||
template <typename THead>
|
||||
struct type_list<THead> : type_list<>
|
||||
{
|
||||
using type = THead;
|
||||
|
||||
static constexpr size_t size = 1U;
|
||||
|
||||
using index_sequence_type = etl::make_index_sequence<1>; ///< The index_sequence type for this type_list.
|
||||
|
||||
private:
|
||||
|
||||
type_list() ETL_DELETE;
|
||||
type_list(const type_list&) ETL_DELETE;
|
||||
type_list& operator =(const type_list&) ETL_DELETE;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation of etl::nth_type for etl::type_list
|
||||
//***************************************************************************
|
||||
template <size_t N, typename THead, typename... TTail>
|
||||
struct nth_type<N, type_list<THead, TTail...>>
|
||||
{
|
||||
ETL_STATIC_ASSERT(N <= sizeof...(TTail), "etl::nth_type out of range for etl::type_list");
|
||||
|
||||
using type = typename nth_type<N - 1, type_list<TTail...>>::type;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation of etl::nth_type for etl::type_list with index of 0
|
||||
//***************************************************************************
|
||||
template <typename THead, typename... TTail>
|
||||
struct nth_type<0, type_list<THead, TTail...>>
|
||||
{
|
||||
using type = THead;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Specialisation of etl::nth_type for empty etl::type_list
|
||||
//***************************************************************************
|
||||
template <size_t N>
|
||||
struct nth_type<N, type_list<>>
|
||||
{
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Declares a new type_list by selecting types from a given type_list, according to an index sequence.
|
||||
//***************************************************************************
|
||||
template <typename TTypeList, size_t... Indices>
|
||||
struct type_list_select
|
||||
{
|
||||
using type = type_list<nth_type_t<Indices, TTypeList>...>;
|
||||
};
|
||||
|
||||
template <typename TTypeList, size_t... Indices>
|
||||
using type_list_select_t = typename type_list_select<TTypeList, Indices...>::type;
|
||||
|
||||
//***************************************************************************
|
||||
/// Type list size.
|
||||
//***************************************************************************
|
||||
template <typename... TTypes>
|
||||
struct type_list_size;
|
||||
|
||||
template <typename... TTypes>
|
||||
struct type_list_size<etl::type_list<TTypes...>> : public etl::integral_constant<size_t, sizeof...(TTypes)>
|
||||
{
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP17
|
||||
template <typename... TTypes>
|
||||
inline constexpr size_t type_list_size_v = type_list_size<etl::type_list<TTypes...>>::value;
|
||||
#endif
|
||||
|
||||
//***************************************************************************
|
||||
/// Concatenates two or more type_lists.
|
||||
//***************************************************************************
|
||||
template <typename... TypeLists>
|
||||
struct type_list_cat;
|
||||
|
||||
//***************************************************************************
|
||||
/// Concatenates two or more type_lists.
|
||||
/// Specialisation for a single type_list (base case)
|
||||
//***************************************************************************
|
||||
template <typename TypeList>
|
||||
struct type_list_cat<TypeList>
|
||||
{
|
||||
using type = TypeList;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Concatenates two or more type_lists.
|
||||
/// Specialisation for two or more type_lists
|
||||
//***************************************************************************
|
||||
template <typename... TTypes1, typename... TTypes2, typename... TTail>
|
||||
struct type_list_cat<etl::type_list<TTypes1...>, etl::type_list<TTypes2...>, TTail...>
|
||||
{
|
||||
using type = typename type_list_cat<etl::type_list<TTypes1..., TTypes2...>, TTail...>::type;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -143,6 +143,7 @@ add_executable(etl_tests
|
||||
test_fsm.cpp
|
||||
test_function.cpp
|
||||
test_functional.cpp
|
||||
test_function_traits.cpp
|
||||
test_gamma.cpp
|
||||
test_hash.cpp
|
||||
test_hfsm.cpp
|
||||
|
||||
@ -178,6 +178,7 @@ target_sources(tests PRIVATE
|
||||
fsm.h.t.cpp
|
||||
function.h.t.cpp
|
||||
functional.h.t.cpp
|
||||
function_traits.h.t.cpp
|
||||
gamma.h.t.cpp
|
||||
gcd.h.t.cpp
|
||||
generic_pool.h.t.cpp
|
||||
|
||||
29
test/syntax_check/function_traits.h.t.cpp
Normal file
29
test/syntax_check/function_traits.h.t.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2025 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include <etl/function_traits.h>
|
||||
211
test/test_function_traits.cpp
Normal file
211
test/test_function_traits.cpp
Normal file
@ -0,0 +1,211 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2024 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.
|
||||
******************************************************************************/
|
||||
|
||||
#include "unit_test_framework.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "etl/function_traits.h"
|
||||
#include "etl/type_list.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
//*****************************************************************************
|
||||
// The free function taking no parameters.
|
||||
//*****************************************************************************
|
||||
void free_void()
|
||||
{
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// The free function taking an int parameter.
|
||||
//*****************************************************************************
|
||||
int free_int(int i, int j)
|
||||
{
|
||||
return i + j;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// The test class with member functions.
|
||||
//*****************************************************************************
|
||||
class Object
|
||||
{
|
||||
public:
|
||||
|
||||
//*******************************************
|
||||
// void
|
||||
void member_void()
|
||||
{
|
||||
}
|
||||
|
||||
void member_void_const() const
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// int
|
||||
int member_int(int i, int j)
|
||||
{
|
||||
return i + j;
|
||||
}
|
||||
|
||||
int member_int_const(int i, int j) const
|
||||
{
|
||||
return i + j;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// static
|
||||
static void member_static(int j)
|
||||
{
|
||||
(void)j;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
SUITE(test_function_traits)
|
||||
{
|
||||
//*************************************************************************
|
||||
TEST(test_free_function_free_void)
|
||||
{
|
||||
free_void(); // Keep clang happy
|
||||
|
||||
using traits = etl::function_traits<decltype(&free_void)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<void(void), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<>, traits::argument_types>::value));
|
||||
|
||||
CHECK_TRUE(traits::is_function);
|
||||
CHECK_FALSE(traits::is_member_function);
|
||||
CHECK_FALSE(traits::is_const);
|
||||
CHECK_EQUAL(0, traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_free_function_free_int)
|
||||
{
|
||||
free_int(1, 2); // Keep clang happy
|
||||
|
||||
using traits = etl::function_traits<decltype(&free_int)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<int(int, int), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<int, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<int, int>, traits::argument_types>::value));
|
||||
|
||||
CHECK_TRUE(traits::is_function);
|
||||
CHECK_FALSE(traits::is_member_function);
|
||||
CHECK_FALSE(traits::is_const);
|
||||
CHECK_EQUAL(2 , traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_function_void)
|
||||
{
|
||||
using traits = etl::function_traits<decltype(&Object::member_void)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<void(void), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<Object, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<>, traits::argument_types>::value));
|
||||
|
||||
CHECK_FALSE(traits::is_function);
|
||||
CHECK_TRUE(traits::is_member_function);
|
||||
CHECK_FALSE(traits::is_const);
|
||||
CHECK_EQUAL(0, traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_function_void_const)
|
||||
{
|
||||
using traits = etl::function_traits<decltype(&Object::member_void_const)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<void(void), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<Object, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<>, traits::argument_types>::value));
|
||||
|
||||
CHECK_FALSE(traits::is_function);
|
||||
CHECK_TRUE(traits::is_member_function);
|
||||
CHECK_TRUE(traits::is_const);
|
||||
CHECK_EQUAL(0, traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_function_int)
|
||||
{
|
||||
using traits = etl::function_traits<decltype(&Object::member_int)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<int(int, int), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<int, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<Object, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<int, int>, traits::argument_types>::value));
|
||||
|
||||
CHECK_FALSE(traits::is_function);
|
||||
CHECK_TRUE(traits::is_member_function);
|
||||
CHECK_FALSE(traits::is_const);
|
||||
CHECK_EQUAL(2, traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_function_int_const)
|
||||
{
|
||||
using traits = etl::function_traits<decltype(&Object::member_int_const)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<int(int, int), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<int, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<Object, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<int, int>, traits::argument_types>::value));
|
||||
|
||||
CHECK_FALSE(traits::is_function);
|
||||
CHECK_TRUE(traits::is_member_function);
|
||||
CHECK_TRUE(traits::is_const);
|
||||
CHECK_EQUAL(2, traits::argument_count);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_member_function_static)
|
||||
{
|
||||
using traits = etl::function_traits<decltype(&Object::member_static)>;
|
||||
|
||||
CHECK_TRUE((std::is_same<void(int), traits::function_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::return_type>::value));
|
||||
CHECK_TRUE((std::is_same<void, traits::object_type>::value));
|
||||
CHECK_TRUE((std::is_same<etl::type_list<int>, traits::argument_types>::value));
|
||||
|
||||
CHECK_TRUE(traits::is_function);
|
||||
CHECK_FALSE(traits::is_member_function);
|
||||
CHECK_FALSE(traits::is_const);
|
||||
CHECK_EQUAL(1, traits::argument_count);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -3084,6 +3084,7 @@
|
||||
<ClInclude Include="..\..\include\etl\crc8_rohc.h" />
|
||||
<ClInclude Include="..\..\include\etl\crc8_wcdma.h" />
|
||||
<ClInclude Include="..\..\include\etl\expected.h" />
|
||||
<ClInclude Include="..\..\include\etl\function_traits.h" />
|
||||
<ClInclude Include="..\..\include\etl\gcd.h" />
|
||||
<ClInclude Include="..\..\include\etl\lcm.h" />
|
||||
<ClInclude Include="..\..\include\etl\math.h" />
|
||||
@ -3248,6 +3249,7 @@
|
||||
<ClInclude Include="..\..\include\etl\to_u32string.h" />
|
||||
<ClInclude Include="..\..\include\etl\to_u8string.h" />
|
||||
<ClInclude Include="..\..\include\etl\to_wstring.h" />
|
||||
<ClInclude Include="..\..\include\etl\type_list.h" />
|
||||
<ClInclude Include="..\..\include\etl\type_lookup.h" />
|
||||
<ClInclude Include="..\..\include\etl\type_select.h" />
|
||||
<ClInclude Include="..\..\include\etl\u16format_spec.h" />
|
||||
@ -5582,6 +5584,21 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++14 - No STL|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - Force C++03|Win32'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\syntax_check\function_traits.h.t.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - No virtual imessage|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++ 20 - No Tests|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - No STL|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++14|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release MSVC C++20 - No STL - Optimised -O2|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release MSVC C++20 - No STL - Optimised -O2 - Sanitiser|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++17|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++17 - No STL|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release MSVC C++20 - Optimised O2|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - No virtual messages|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++14 - No STL|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - Force C++03|Win32'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\syntax_check\gamma.h.t.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug MSVC C++20 - No virtual imessage|Win32'">true</ExcludedFromBuild>
|
||||
@ -8079,6 +8096,7 @@
|
||||
<ClCompile Include="..\test_crc8_rohc.cpp" />
|
||||
<ClCompile Include="..\test_crc8_wcdma.cpp" />
|
||||
<ClCompile Include="..\test_expected.cpp" />
|
||||
<ClCompile Include="..\test_function_traits.cpp" />
|
||||
<ClCompile Include="..\test_hfsm_recurse_to_inner_state_on_start.cpp" />
|
||||
<ClCompile Include="..\test_intrusive_links.cpp" />
|
||||
<ClCompile Include="..\test_macros.cpp" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user