Variadic versions of etl::type_id_lookup and etl::type_type_lookup for C++11 and above.

This commit is contained in:
John Wellbelove 2020-05-19 20:12:12 +01:00
parent 02ca9e8cb7
commit 0bfc97a914
17 changed files with 1003 additions and 48 deletions

1
.gitignore vendored
View File

@ -254,3 +254,4 @@ unittest-cpp
*.opendb
build-test-Desktop_x86_windows_msvc2017_pe_32bit-Debug
test/Logs
build-test-Desktop_x86_windows_msvc2019_pe_32bit-Debug

View File

@ -34,6 +34,7 @@ SOFTWARE.
#include "platform.h"
#include "type_traits.h"
#include "static_assert.h"
#include "integral_limits.h"
#include "null_type.h"
#undef ETL_FILE
@ -85,6 +86,150 @@ namespace etl
typedef T2 type2;
};
#if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
//***************************************************************************
// type_id_lookup
//***************************************************************************
template <typename... TTypes>
struct type_id_lookup
{
private:
// The type for no match.
struct nulltype {};
// For N type pairs.
template <size_t ID, typename T1, typename... TRest>
struct type_from_id_helper
{
using type = typename etl::conditional<ID == T1::ID,
typename T1::type,
typename type_from_id_helper<ID, TRest...>::type>::type;
};
// Specialisation for 1 type pair.
template <size_t ID, typename T1>
struct type_from_id_helper<ID, T1>
{
using type = typename etl::conditional<ID == T1::ID,
typename T1::type,
nulltype>::type;
};
public:
//************************************
// type_from_id
//************************************
template <int ID>
struct type_from_id
{
using type = typename type_from_id_helper<ID, TTypes...>::type;
static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
};
template <int ID>
using type_from_id_t = typename type_from_id<ID>::type;
private:
static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
// For N type pairs.
template <typename T, typename T1, typename... TRest>
struct id_from_type_helper
{
static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : id_from_type_helper<T, TRest...>::value;
};
// Specialisation for 1 type pair.
template <typename T, typename T1>
struct id_from_type_helper<T, T1>
{
static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : UNKNOWN;
};
public:
//************************************
// id_from_type
//************************************
template <typename T>
struct id_from_type
{
static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
static_assert(value != UNKNOWN, "Invalid type");
};
#if ETL_CPP17_SUPPORTED
template <typename T>
static constexpr size_t id_from_type_v = id_from_type<T, TTypes...>::value;
#endif
//************************************
template <typename T>
static unsigned int get_id_from_type(const T&)
{
return get_id_from_type<T>();
}
//************************************
template <typename T>
static unsigned int get_id_from_type()
{
return id_from_type<T>::value;
}
};
//***************************************************************************
// type_type_lookup
//***************************************************************************
template <typename... TTypes>
class type_type_lookup
{
private:
// The type for no match.
struct nulltype {};
template <typename T, typename T1, typename... TRest>
struct type_from_type_helper
{
using type = typename etl::conditional<etl::is_same<T, typename T1::type1>::value,
typename T1::type2,
typename type_from_type_helper<T, TRest...>::type>::type;
};
template <typename T, typename T1>
struct type_from_type_helper<T, T1>
{
using type = typename etl::conditional<etl::is_same<T, typename T1::type1>::value,
typename T1::type2,
nulltype>::type;
};
public:
template <typename T>
class type_from_type
{
public:
// The matched type or nulltype
using type = typename type_from_type_helper<T, TTypes...>::type;
static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
};
// Template alias.
template <typename T>
using TypeFromType_t = typename type_from_type<T>::type;
};
#else
/*[[[cog
import cog
cog.outl("//***************************************************************************")
@ -188,6 +333,8 @@ namespace etl
cog.outl("};")
]]]*/
/*[[[end]]]*/
#endif
}
#undef ETL_FILE

View File

@ -59,6 +59,47 @@ cog.outl("//********************************************************************
namespace etl
{
#if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
//***************************************************************************
// Variadic version.
//***************************************************************************
template <typename... TTypes>
struct type_select
{
private:
//***********************************
template <size_t ID, size_t N, typename T1, typename... TRest>
struct type_select_helper
{
using type = typename etl::conditional<ID == N,
T1,
typename type_select_helper<ID, N + 1, TRest...>::type>::type;
};
//***********************************
template <size_t ID, size_t N, typename T1>
struct type_select_helper<ID, N, T1>
{
using type = T1;
};
public:
template <size_t ID>
struct select
{
static_assert(ID < sizeof...(TTypes), "Illegal type_select::select index");
using type = typename type_select_helper<ID, 0, TTypes...>::type;
};
template <size_t ID>
using select_t = typename select<ID>::type;
};
#else
/*[[[cog
import cog
cog.outl("//***************************************************************************")
@ -127,6 +168,7 @@ namespace etl
cog.outl("};")
]]]*/
/*[[[end]]]*/
#endif
}
#undef ETL_FILE

View File

@ -116,6 +116,30 @@ namespace etl
using bool_constant = integral_constant<bool, B>;
#endif
//***************************************************************************
/// bool_constant
template <bool B>
struct bool_constant :etl::integral_constant<bool, B>
{
};
#if ETL_CPP17_SUPPORTED
template <bool B>
inline constexpr bool bool_constant_v = bool_constant<B>::value;
#endif
//***************************************************************************
/// negation
template <typename T>
struct negation : etl::bool_constant<!bool(T::value)>
{
};
#if ETL_CPP17_SUPPORTED
template <typename T>
inline constexpr bool negation_v = negation<T>::value;
#endif
//***************************************************************************
/// remove_reference
template <typename T> struct remove_reference { typedef T type; };
@ -1533,6 +1557,43 @@ namespace etl
template <typename T>
inline constexpr size_t size_of_v = etl::size_of<T>::value;
#endif
//***************************************************************************
/// index_of
namespace private_type_traits
{
template <typename T, typename... TTypes>
struct index_of_helper;
template <typename T, typename T1, typename... TRest>
struct index_of_helper<T, T1, TRest...>
{
enum
{
value = etl::is_same<T, T1>::value ? 1 : 1 + index_of_helper<T, TRest...>::value
};
};
template <typename T, typename T1>
struct index_of_helper<T, T1>
{
enum
{
value = 1
};
};
}
template <typename T, typename... TTypes>
struct index_of
{
static ETL_CONST_OR_CONSTEXPR size_t npos = -1;
enum
{
value = etl::is_one_of<T, TTypes...>::value ? private_type_traits::index_of_helper<T, TTypes...>::value - 1 : npos
};
};
}
#endif // ETL_TYPE_TRAITS_INCLUDED

View File

@ -35,7 +35,7 @@ SOFTWARE.
#include "type_traits.h"
#include "utility.h"
#if ETL_USING_STL
#if ETL_USING_STL || defined(ETL_IN_UNIT_TEST)
#include <iterator>
#endif

View File

@ -75,9 +75,7 @@ SOFTWARE.
// NAN not defined or Rowley CrossWorks
#if !defined(NAN) || defined(__CROSSWORKS_ARM) || defined(ETL_COMPILER_ARM5)
#if !defined(ETL_NO_CPP_NAN_SUPPORT)
#define ETL_NO_CPP_NAN_SUPPORT
#endif
#define ETL_NO_CPP_NAN_SUPPORT
#endif
#endif

View File

@ -34,6 +34,7 @@ SOFTWARE.
#include "platform.h"
#include "type_traits.h"
#include "static_assert.h"
#include "integral_limits.h"
#include "null_type.h"
#undef ETL_FILE
@ -73,6 +74,150 @@ namespace etl
typedef T2 type2;
};
#if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
//***************************************************************************
// type_id_lookup
//***************************************************************************
template <typename... TTypes>
struct type_id_lookup
{
private:
// The type for no match.
struct nulltype {};
// For N type pairs.
template <size_t ID, typename T1, typename... TRest>
struct type_from_id_helper
{
using type = typename etl::conditional<ID == T1::ID,
typename T1::type,
typename type_from_id_helper<ID, TRest...>::type>::type;
};
// Specialisation for 1 type pair.
template <size_t ID, typename T1>
struct type_from_id_helper<ID, T1>
{
using type = typename etl::conditional<ID == T1::ID,
typename T1::type,
nulltype>::type;
};
public:
//************************************
// type_from_id
//************************************
template <int ID>
struct type_from_id
{
using type = typename type_from_id_helper<ID, TTypes...>::type;
static_assert(!(etl::is_same<nulltype, type>::value), "Invalid id");
};
template <int ID>
using type_from_id_t = typename type_from_id<ID>::type;
private:
static constexpr size_t UNKNOWN = etl::integral_limits<size_t>::max;
// For N type pairs.
template <typename T, typename T1, typename... TRest>
struct id_from_type_helper
{
static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : id_from_type_helper<T, TRest...>::value;
};
// Specialisation for 1 type pair.
template <typename T, typename T1>
struct id_from_type_helper<T, T1>
{
static constexpr size_t value = etl::is_same<T, typename T1::type>::value ? T1::ID : UNKNOWN;
};
public:
//************************************
// id_from_type
//************************************
template <typename T>
struct id_from_type
{
static constexpr size_t value = id_from_type_helper<T, TTypes...>::value;
static_assert(value != UNKNOWN, "Invalid type");
};
#if ETL_CPP17_SUPPORTED
template <typename T>
static constexpr size_t id_from_type_v = id_from_type<T>::value;
#endif
//************************************
template <typename T>
static unsigned int get_id_from_type(const T&)
{
return get_id_from_type<T>();
}
//************************************
template <typename T>
static unsigned int get_id_from_type()
{
return id_from_type<T>::value;
}
};
//***************************************************************************
// type_type_lookup
//***************************************************************************
template <typename... TTypes>
class type_type_lookup
{
private:
// The type for no match.
struct nulltype {};
template <typename T, typename T1, typename... TRest>
struct type_from_type_helper
{
using type = typename etl::conditional<etl::is_same<T, typename T1::type1>::value,
typename T1::type2,
typename type_from_type_helper<T, TRest...>::type>::type;
};
template <typename T, typename T1>
struct type_from_type_helper<T, T1>
{
using type = typename etl::conditional<etl::is_same<T, typename T1::type1>::value,
typename T1::type2,
nulltype>::type;
};
public:
template <typename T>
class type_from_type
{
public:
// The matched type or nulltype
using type = typename type_from_type_helper<T, TTypes...>::type;
static_assert(!etl::is_same<type, nulltype>::value, "Type match not found");
};
// Template alias.
template <typename T>
using TypeFromType_t = typename type_from_type<T>::type;
};
#else
//***************************************************************************
// For 16 types.
//***************************************************************************
@ -224,6 +369,8 @@ namespace etl
ETL_STATIC_ASSERT(!(etl::is_same<etl::null_type<0>, type>::value), "Invalid type");
};
};
#endif
}
#undef ETL_FILE

View File

@ -47,6 +47,47 @@ SOFTWARE.
namespace etl
{
#if ETL_CPP11_SUPPORTED && !defined(ETL_TYPE_SELECT_FORCE_CPP03)
//***************************************************************************
// Variadic version.
//***************************************************************************
template <typename... TTypes>
struct type_select
{
private:
//***********************************
template <size_t ID, size_t N, typename T1, typename... TRest>
struct type_select_helper
{
using type = typename etl::conditional<ID == N,
T1,
typename type_select_helper<ID, N + 1, TRest...>::type>::type;
};
//***********************************
template <size_t ID, size_t N, typename T1>
struct type_select_helper<ID, N, T1>
{
using type = T1;
};
public:
template <size_t ID>
struct select
{
static_assert(ID < sizeof...(TTypes), "Illegal type_select::select index");
using type = typename type_select_helper<ID, 0, TTypes...>::type;
};
template <size_t ID>
using select_t = typename select<ID>::type;
};
#else
//***************************************************************************
// For 16 types.
//***************************************************************************
@ -583,6 +624,7 @@ namespace etl
ETL_STATIC_ASSERT(ID < 1, "Invalid ID");
};
};
#endif
}
#undef ETL_FILE

View File

@ -45,6 +45,7 @@ SOFTWARE.
#include "static_assert.h"
#include "alignment.h"
#include "error_handler.h"
#include "null_type.h"
#if defined(ETL_COMPILER_KEIL)
#pragma diag_suppress 940
@ -106,13 +107,13 @@ namespace etl
///\ingroup variant
//***************************************************************************
template <typename T1,
typename T2 = private_variant::no_type<2>,
typename T3 = private_variant::no_type<3>,
typename T4 = private_variant::no_type<4>,
typename T5 = private_variant::no_type<5>,
typename T6 = private_variant::no_type<6>,
typename T7 = private_variant::no_type<7>,
typename T8 = private_variant::no_type<8> >
typename T2 = etl::null_type<2>,
typename T3 = etl::null_type<3>,
typename T4 = etl::null_type<4>,
typename T5 = etl::null_type<5>,
typename T6 = etl::null_type<6>,
typename T7 = etl::null_type<7>,
typename T8 = etl::null_type<8> >
class variant
{
public:
@ -151,13 +152,13 @@ namespace etl
//***************************************************************************
/// Short form of no_type placeholders.
//***************************************************************************
typedef private_variant::no_type<2> no_type2;
typedef private_variant::no_type<3> no_type3;
typedef private_variant::no_type<4> no_type4;
typedef private_variant::no_type<5> no_type5;
typedef private_variant::no_type<6> no_type6;
typedef private_variant::no_type<7> no_type7;
typedef private_variant::no_type<8> no_type8;
typedef etl::null_type<2> no_type2;
typedef etl::null_type<3> no_type3;
typedef etl::null_type<4> no_type4;
typedef etl::null_type<5> no_type5;
typedef etl::null_type<6> no_type6;
typedef etl::null_type<7> no_type7;
typedef etl::null_type<8> no_type8;
//***************************************************************************
/// Lookup the id of type.

509
include/etl/variant_new.h Normal file
View File

@ -0,0 +1,509 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 jwellbelove
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_VARIANT_NEW_INCLUDED
#define ETL_VARIANT_NEW_INCLUDED
#include <stdint.h>
#include <new>
#include "platform.h"
#include "utility.h"
#include "array.h"
#include "largest.h"
#include "exception.h"
#include "type_traits.h"
#include "integral_limits.h"
#include "static_assert.h"
#include "alignment.h"
#include "error_handler.h"
#include "null_type.h"
#if defined(ETL_COMPILER_KEIL)
#pragma diag_suppress 940
#pragma diag_suppress 111
#endif
#undef ETL_FILE
#define ETL_FILE "24"
#if ETL_CPP11_SUPPORTED
//*****************************************************************************
///\defgroup variant variant
/// A class that can contain one a several specified types in a type safe manner.
///\ingroup containers
//*****************************************************************************
namespace etl
{
//***************************************************************************
/// Base exception for the variant class.
///\ingroup variant
//***************************************************************************
class variant_exception : public exception
{
public:
variant_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
/// 'Unsupported type' exception for the variant class.
///\ingroup variant
//***************************************************************************
class variant_incorrect_type_exception : public variant_exception
{
public:
variant_incorrect_type_exception(string_type file_name_, numeric_type line_number_)
: variant_exception(ETL_ERROR_TEXT("variant: unsupported type", ETL_FILE"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// A template class that can store any of the types defined in the template parameter list.
/// Supports up to 8 types.
///\ingroup variant
//***************************************************************************
template <typename... TVariants>
class variant
{
public:
//***************************************************************************
/// The id a unsupported types.
//***************************************************************************
static ETL_CONST_OR_CONSTEXPR size_t npos = -1;
private:
// All types of variant are friends.
template <typename... U>
friend class variant;
//***************************************************************************
/// The largest type.
//***************************************************************************
typedef typename largest_type<TVariants...>::type largest_t;
//***************************************************************************
/// The largest size.
//***************************************************************************
static const size_t SIZE = sizeof(largest_t);
//***************************************************************************
/// The largest alignment.
//***************************************************************************
static const size_t ALIGNMENT = etl::largest_alignment<TVariants...>::value;
public:
//***************************************************************************
/// Destructor.
//***************************************************************************
~variant()
{
destruct_current();
}
//***************************************************************************
/// Default constructor.
/// Sets the state of the instance to containing no valid data.
//***************************************************************************
variant()
: type_id(UNSUPPORTED_TYPE_ID)
{
}
//***************************************************************************
/// Constructor that catches any types that are not supported.
/// Forces a ETL_STATIC_ASSERT.
//***************************************************************************
template <typename T>
variant(const T& value)
{
ETL_STATIC_ASSERT(etl::index_of<T, TVariants>::value != etl::index_of<T, TVariants>::npos, "Unsupported type");
::new (static_cast<T*>(data)) T(value);
type_id = etl::index_of<T, TVariants>::value;
}
//***************************************************************************
/// Copy constructor.
///\param other The other variant object to copy.
//***************************************************************************
variant(const variant& other)
{
switch (other.type_id)
{
case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
default: break;
}
type_id = other.type_id;
}
#if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && !defined(ETL_VARIANT_FORCE_CPP03)
//*************************************************************************
/// Emplace with variadic constructor parameters.
//*************************************************************************
template <typename T, typename... Args>
T& emplace(Args&&... args)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(etl::forward<Args>(args)...);
type_id = Type_Id_Lookup<T>::type_id;
return *static_cast<T*>(data);
}
#else
//***************************************************************************
/// Emplace with one constructor parameter.
//***************************************************************************
template <typename T, typename TP1>
T& emplace(const TP1& value1)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(value1);
type_id = Type_Id_Lookup<T>::type_id;
return *static_cast<T*>(data);
}
//***************************************************************************
/// Emplace with two constructor parameters.
//***************************************************************************
template <typename T, typename TP1, typename TP2>
T& emplace(const TP1& value1, const TP2& value2)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(value1, value2);
type_id = Type_Id_Lookup<T>::type_id;
return *static_cast<T*>(data);
}
//***************************************************************************
/// Emplace with three constructor parameters.
//***************************************************************************
template <typename T, typename TP1, typename TP2, typename TP3>
T& emplace(const TP1& value1, const TP2& value2, const TP3& value3)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(value1, value2, value3);
type_id = Type_Id_Lookup<T>::type_id;
return *static_cast<T*>(data);
}
//***************************************************************************
/// Emplace with four constructor parameters.
//***************************************************************************
template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
T& emplace(const TP1& value1, const TP2& value2, const TP3& value3, const TP4& value4)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(value1, value2, value3, value4);
type_id = Type_Id_Lookup<T>::type_id;
return *static_cast<T*>(data);
}
#endif
//***************************************************************************
/// Assignment operator for T1 type.
///\param value The value to assign.
//***************************************************************************
template <typename T>
variant& operator =(const T& value)
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
destruct_current();
::new (static_cast<T*>(data)) T(value);
type_id = Type_Id_Lookup<T>::type_id;
return *this;
}
//***************************************************************************
/// Assignment operator for variant type.
///\param other The variant to assign.
//***************************************************************************
variant& operator =(const variant& other)
{
if (this != &other)
{
destruct_current();
switch (other.type_id)
{
case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
default: break;
}
type_id = other.type_id;
}
return *this;
}
//***************************************************************************
/// Checks if the type is the same as the current stored type.
/// For variants with the same type declarations.
///\return <b>true</b> if the types are the same, otherwise <b>false</b>.
//***************************************************************************
bool is_same_type(const variant& other) const
{
return type_id == other.type_id;
}
//***************************************************************************
/// Checks if the type is the same as the current stored type.
/// For variants with differing declarations.
///\return <b>true</b> if the types are the same, otherwise <b>false</b>.
//***************************************************************************
template <typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7, typename U8>
bool is_same_type(const variant<U1, U2, U3, U4, U5, U6, U7, U8>& other) const
{
bool is_same = false;
switch (other.type_id)
{
case 0: is_same = (type_id == Type_Id_Lookup<U1>::type_id); break;
case 1: is_same = (type_id == Type_Id_Lookup<U2>::type_id); break;
case 2: is_same = (type_id == Type_Id_Lookup<U3>::type_id); break;
case 3: is_same = (type_id == Type_Id_Lookup<U4>::type_id); break;
case 4: is_same = (type_id == Type_Id_Lookup<U5>::type_id); break;
case 5: is_same = (type_id == Type_Id_Lookup<U6>::type_id); break;
case 6: is_same = (type_id == Type_Id_Lookup<U7>::type_id); break;
case 7: is_same = (type_id == Type_Id_Lookup<U8>::type_id); break;
default: break;
}
return is_same;
}
//***************************************************************************
/// Calls the supplied reader instance.
/// The 'read' function appropriate to the current type is called with the stored value.
//***************************************************************************
void call(reader& r)
{
switch (type_id)
{
case 0: r.read(static_cast<T1&>(data)); break;
case 1: r.read(static_cast<T2&>(data)); break;
case 2: r.read(static_cast<T3&>(data)); break;
case 3: r.read(static_cast<T4&>(data)); break;
case 4: r.read(static_cast<T5&>(data)); break;
case 5: r.read(static_cast<T6&>(data)); break;
case 6: r.read(static_cast<T7&>(data)); break;
case 7: r.read(static_cast<T8&>(data)); break;
default: break;
}
}
//***************************************************************************
/// Checks whether a valid value is currently stored.
///\return <b>true</b> if the value is valid, otherwise <b>false</b>.
//***************************************************************************
bool is_valid() const
{
return type_id != UNSUPPORTED_TYPE_ID;
}
//***************************************************************************
/// Checks to see if the type currently stored is the same as that specified in the template parameter.
///\return <b>true</b> if it is the specified type, otherwise <b>false</b>.
//***************************************************************************
template <typename T>
bool is_type() const
{
return type_id == Type_Id_Lookup<T>::type_id;
}
//***************************************************************************
/// Gets the index of the type currently stored or UNSUPPORTED_TYPE_ID
//***************************************************************************
size_t index() const
{
return type_id;
}
//***************************************************************************
/// Clears the value to 'no valid stored value'.
//***************************************************************************
void clear()
{
destruct_current();
}
//***************************************************************************
/// Gets the value stored as the specified template type.
/// Throws a variant_incorrect_type_exception if the actual type is not that specified.
///\return A reference to the value.
//***************************************************************************
template <typename T>
T& get()
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
ETL_ASSERT(is_type<T>(), ETL_ERROR(variant_incorrect_type_exception));
return static_cast<T&>(data);
}
//***************************************************************************
/// Gets the value stored as the specified template type.
/// Throws a variant_incorrect_type_exception if the actual type is not that specified.
///\return A const reference to the value.
//***************************************************************************
template <typename T>
const T& get() const
{
ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
ETL_ASSERT(is_type<T>(), ETL_ERROR(variant_incorrect_type_exception));
return static_cast<const T&>(data);
}
//***************************************************************************
/// Gets the value stored as the specified template type.
///\return A reference to the value.
//***************************************************************************
template <typename TBase>
TBase& upcast()
{
return upcast_functor<TBase, T1, T2, T3, T4, T5, T6, T7, T8>()(data, type_id);
}
//***************************************************************************
/// Gets the value stored as the specified template type.
///\return A const reference to the value.
//***************************************************************************
template <typename TBase>
const TBase& upcast() const
{
return upcast_functor<TBase, T1, T2, T3, T4, T5, T6, T7, T8>()(data, type_id);
}
//***************************************************************************
/// Conversion operators for each type.
//***************************************************************************
operator T1&() { return get<T1>(); }
operator T2&() { return get<T2>(); }
operator T3&() { return get<T3>(); }
operator T4&() { return get<T4>(); }
operator T5&() { return get<T5>(); }
operator T6&() { return get<T6>(); }
operator T7&() { return get<T7>(); }
operator T8&() { return get<T8>(); }
//***************************************************************************
/// Checks if the template type is supported by the implementation of variant..
///\return <b>true</b> if the type is supported, otherwise <b>false</b>.
//***************************************************************************
template <typename T>
static bool is_supported_type()
{
return Type_Is_Supported<T>::value;
}
private:
//***************************************************************************
/// Destruct the current occupant of the variant.
//***************************************************************************
void destruct_current()
{
switch (type_id)
{
case 0: { static_cast<T1*>(data)->~T1(); break; }
case 1: { static_cast<T2*>(data)->~T2(); break; }
case 2: { static_cast<T3*>(data)->~T3(); break; }
case 3: { static_cast<T4*>(data)->~T4(); break; }
case 4: { static_cast<T5*>(data)->~T5(); break; }
case 5: { static_cast<T6*>(data)->~T6(); break; }
case 6: { static_cast<T7*>(data)->~T7(); break; }
case 7: { static_cast<T8*>(data)->~T8(); break; }
default: { break; }
}
type_id = UNSUPPORTED_TYPE_ID;
}
constexpr size_t NUMBER_OF_VARIANTS = sizeof...(TVariants);
//***************************************************************************
/// The internal storage.
/// Aligned on a suitable boundary, which should be good for all types.
//***************************************************************************
typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;
//***************************************************************************
/// The id of the current stored type.
//***************************************************************************
type_id_t type_id;
};
}
#endif
#undef ETL_FILE
#endif

View File

@ -38,8 +38,8 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 18
#define ETL_VERSION_MINOR 1
#define ETL_VERSION_PATCH 3
#define ETL_VERSION_MINOR 2
#define ETL_VERSION_PATCH 0
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_U16 ETL_STRINGIFY(ETL_VERSION_MAJOR) u"." ETL_STRINGIFY(ETL_VERSION_MINOR) u"." ETL_STRINGIFY(ETL_VERSION_PATCH)

View File

@ -1,6 +1,6 @@
{
"name": "Embedded Template Library",
"version": "18.1.3",
"version": "18.2.0",
"authors": {
"name": "John Wellbelove",
"email": "john.wellbelove@etlcpp.com"

View File

@ -1,5 +1,5 @@
name=Embedded Template Library
version=18.1.3
version=18.2.0
author= John Wellbelove <john.wellbelove@etlcpp.com>
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
license=MIT

View File

@ -1,3 +1,7 @@
===============================================================================
18.2.0
Variadic versions of etl::type_id_lookup and etl::type_type_lookup for C++11 and above.
===============================================================================
18.1.3
Added CircleCI yml file for master branch

View File

@ -91,6 +91,7 @@ SOFTWARE.
//#define ETL_QUEUE_LOCKED_FORCE_CPP03
//#define ETL_OPTIONAL_FORCE_CPP03
//#define ETL_LARGEST_TYPE_FORCE_CPP03
//#define ETL_TYPE_SELECT_FORCE_CPP03
#if defined(ETL_NO_STL)
#define ETL_TIMER_SEMAPHORE_TYPE uint32_t

View File

@ -70,7 +70,7 @@ namespace
void free_reference(const Data& data, int j)
{
function_called = true;
parameter_correct = (data.d == VALUE1) && (j = VALUE2);
parameter_correct = (data.d == VALUE1) && (j == VALUE2);
}
//*****************************************************************************
@ -185,7 +185,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_free_void)
{
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<free_void>();
auto d = etl::delegate<void(void)>::create<free_void>();
d();
@ -195,7 +195,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_free_int)
{
etl::delegate<void(int, int)> d = etl::delegate<void(int, int)>::create<free_int>();
auto d = etl::delegate<void(int, int)>::create<free_int>();
d(VALUE1, VALUE2);
@ -256,7 +256,7 @@ namespace
{
Test test;
etl::delegate<void(void)> d = etl::delegate<void(void)>::create(test);
auto d = etl::delegate<void(void)>::create(test);
d();
@ -279,7 +279,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_operator_void_compile_time)
{
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<Test, test_static>();
auto d = etl::delegate<void(void)>::create<Test, test_static>();
d();
@ -289,7 +289,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_operator_void_compile_time_const)
{
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<const Test, const_test_static>();
auto d = etl::delegate<void(void)>::create<const Test, const_test_static>();
d();
@ -316,7 +316,7 @@ namespace
{
Test test;
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<Test, &Test::member_void>(test);
auto d = etl::delegate<void(void)>::create<Test, &Test::member_void>(test);
d();
@ -328,7 +328,7 @@ namespace
{
const Test test;
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<Test, &Test::member_void_const>(test);
auto d = etl::delegate<void(void)>::create<Test, &Test::member_void_const>(test);
d();
@ -340,7 +340,7 @@ namespace
{
Test test;
etl::delegate<void(int, int)> d = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
auto d = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
d(VALUE1, VALUE2);
@ -353,7 +353,7 @@ namespace
{
const Test test;
etl::delegate<void(int, int)> d = etl::delegate<void(int, int)>::create<Test, &Test::member_int_const>(test);
auto d = etl::delegate<void(int, int)>::create<Test, &Test::member_int_const>(test);
d(VALUE1, VALUE2);
@ -365,7 +365,7 @@ namespace
TEST_FIXTURE(SetupFixture, test_member_reference)
{
Test test;
etl::delegate<void(const Data&, int)> d = etl::delegate<void(const Data&, int)>::create<Test, &Test::member_reference>(test);
auto d = etl::delegate<void(const Data&, int)>::create<Test, &Test::member_reference>(test);
Data data;
data.d = VALUE1;
@ -380,7 +380,7 @@ namespace
TEST_FIXTURE(SetupFixture, test_member_reference_const)
{
const Test test;
etl::delegate<void(const Data&, int)> d = etl::delegate<void(const Data&, int)>::create<Test, &Test::member_reference_const>(test);
auto d = etl::delegate<void(const Data&, int)>::create<Test, &Test::member_reference_const>(test);
Data data;
data.d = VALUE1;
@ -394,7 +394,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_static)
{
etl::delegate<void(const Data&, int)> d = etl::delegate<void(const Data&, int)>::create<Test::member_static>();
auto d = etl::delegate<void(const Data&, int)>::create<Test::member_static>();
Data data;
data.d = VALUE1;
@ -409,7 +409,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_void_compile_time)
{
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<Test, test_static, &Test::member_void>();
auto d = etl::delegate<void(void)>::create<Test, test_static, &Test::member_void>();
d();
@ -419,7 +419,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time)
{
etl::delegate<void(void)> d = etl::delegate<void(void)>::create<Test, const_test_static, &Test::member_void_const>();
auto d = etl::delegate<void(void)>::create<Test, const_test_static, &Test::member_void_const>();
d();
@ -429,7 +429,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_int_compile_time)
{
etl::delegate<void(int, int)> d = etl::delegate<void(int, int)>::create<Test, test_static, &Test::member_int>();
auto d = etl::delegate<void(int, int)>::create<Test, test_static, &Test::member_int>();
d(VALUE1, VALUE2);
@ -440,7 +440,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time)
{
etl::delegate<void(int, int)> d = etl::delegate<void(int, int)>::create<Test, const_test_static, &Test::member_int_const>();
auto d = etl::delegate<void(int, int)>::create<Test, const_test_static, &Test::member_int_const>();
d(VALUE1, VALUE2);
@ -451,7 +451,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_reference_compile_time)
{
etl::delegate<void(const Data&, int)> d = etl::delegate<void(const Data&, int)>::create<Test, test_static, &Test::member_reference>();
auto d = etl::delegate<void(const Data&, int)>::create<Test, test_static, &Test::member_reference>();
Data data;
data.d = VALUE1;
@ -465,7 +465,7 @@ namespace
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time)
{
etl::delegate<void(const Data&, int)> d = etl::delegate<void(const Data&, int)>::create<Test, const_test_static, &Test::member_reference_const>();
auto d = etl::delegate<void(const Data&, int)>::create<Test, const_test_static, &Test::member_reference_const>();
Data data;
data.d = VALUE1;
@ -482,8 +482,8 @@ namespace
{
Test test;
etl::delegate<void(int, int)> d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
etl::delegate<void(int, int)> d2(d1);
auto d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
auto d2(d1);
d2(VALUE1, VALUE2);
@ -496,7 +496,7 @@ namespace
{
Test test;
etl::delegate<void(int, int)> d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
auto d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
etl::delegate<void(int, int)> d2;
d2 = d1;
@ -512,8 +512,8 @@ namespace
{
Test test;
etl::delegate<void(int, int)> d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
etl::delegate<void(int, int)> d2 = d1;
auto d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
auto d2 = d1;
CHECK(d1 == d2);
}
@ -523,8 +523,8 @@ namespace
{
Test test;
etl::delegate<void(int, int)> d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
etl::delegate<void(int, int)> d2 = etl::delegate<void(int, int)>::create<Test, &Test::member_int_const>(test);;
auto d1 = etl::delegate<void(int, int)>::create<Test, &Test::member_int>(test);
auto d2 = etl::delegate<void(int, int)>::create<Test, &Test::member_int_const>(test);;
CHECK(d1 != d2);
}

View File

@ -162,6 +162,7 @@ namespace
x = other.x;
moved = false;
copied = true;
return *this;
}
Message3& operator =(Message3&& other)
@ -169,6 +170,7 @@ namespace
x = other.x;
moved = true;
copied = false;
return *this;
}
std::string x;