Skip to content

Commit

Permalink
#806 etl::variant_pool should support C++17 variadic parameters
Browse files Browse the repository at this point in the history
Added variadic version that supports >=C++11
  • Loading branch information
John Wellbelove committed Apr 17, 2024
1 parent 754eba9 commit a2bd57c
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 0 deletions.
46 changes: 46 additions & 0 deletions include/etl/generators/type_traits_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,52 @@ typedef integral_constant<bool, true> true_type;
inline constexpr bool is_one_of_v = etl::is_one_of<T, TRest...>::value;
#endif

#if ETL_USING_CPP11
//***************************************************************************
/// Template to determine if a type is a base of all types in a specified list.
///\ingroup types
template <typename T, typename T1, typename... TRest>
struct is_base_of_all
{
static const bool value = etl::is_base_of<T, T1>::value &&
etl::is_base_of_all<T, TRest...>::value;
};

template <typename T, typename T1>
struct is_base_of_all<T, T1>
{
static const bool value = etl::is_base_of<T, T1>::value;
};
#endif

#if ETL_USING_CPP17
template <typename T, typename... TRest>
inline constexpr bool is_base_of_all_v = etl::is_base_of_all<T, TRest...>::value;
#endif

#if ETL_USING_CPP11
//***************************************************************************
/// Template to determine if a type is a base of any type in a specified list.
///\ingroup types
template <typename T, typename T1, typename... TRest>
struct is_base_of_any
{
static const bool value = etl::is_base_of<T, T1>::value ||
etl::is_base_of_any<T, TRest...>::value;
};

template <typename T, typename T1>
struct is_base_of_any<T, T1>
{
static const bool value = etl::is_base_of<T, T1>::value;
};
#endif

#if ETL_USING_CPP17
template <typename T, typename... TRest>
inline constexpr bool is_base_of_any_v = etl::is_base_of_any<T, TRest...>::value;
#endif

//***************************************************************************
/// A set of templates to allow related types to be derived.
///\ingroup types
Expand Down
115 changes: 115 additions & 0 deletions include/etl/generators/variant_pool_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,120 @@ cog.outl("//********************************************************************

namespace etl
{
#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION)
//***************************************************************************
template <size_t MAX_SIZE_, typename ... Ts>
class variant_pool
: public etl::generic_pool<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment,
MAX_SIZE_>
{
public:

typedef etl::generic_pool<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment,
MAX_SIZE_> base_t;

static const size_t MAX_SIZE = MAX_SIZE_;

//*************************************************************************
/// Default constructor.
//*************************************************************************
variant_pool()
{
}

//*************************************************************************
/// Creates the object from a type. Variadic parameter constructor.
//*************************************************************************
template <typename T, typename... Args>
T* create(Args&&... args)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");

return base_t::template create<T>(etl::forward<Args>(args)...);
}

//*************************************************************************
/// Destroys the object.
//*************************************************************************
template <typename T>
void destroy(const T* const p)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");

base_t::destroy(p);
}

//*************************************************************************
/// Returns the maximum number of items in the variant_pool.
//*************************************************************************
size_t max_size() const
{
return MAX_SIZE;
}

private:

variant_pool(const variant_pool&) ETL_DELETE;
variant_pool& operator =(const variant_pool&) ETL_DELETE;
};

//***************************************************************************
template <typename ... Ts>
class variant_pool_ext
: public etl::generic_pool_ext<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment>
{
public:

typedef etl::generic_pool_ext<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment> base_t;

//*************************************************************************
/// Default constructor.
//*************************************************************************
variant_pool_ext(typename base_t::element* buffer, size_t size)
: base_t(buffer, size)
{
}

//*************************************************************************
/// Creates the object from a type. Variadic parameter constructor.
//*************************************************************************
template <typename T, typename... Args>
T* create(Args&&... args)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");

return base_t::template create<T>(etl::forward<Args>(args)...);
}

//*************************************************************************
/// Destroys the object.
//*************************************************************************
template <typename T>
void destroy(const T* const p)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");

base_t::destroy(p);
}

//*************************************************************************
/// Returns the maximum number of items in the variant_pool.
//*************************************************************************
size_t max_size() const
{
return base_t::max_size();
}

private:

variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
};
#else
//***************************************************************************
/*[[[cog
import cog
Expand Down Expand Up @@ -500,6 +614,7 @@ namespace etl
variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
};
#endif
}

#endif
46 changes: 46 additions & 0 deletions include/etl/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,52 @@ typedef integral_constant<bool, true> true_type;
inline constexpr bool is_one_of_v = etl::is_one_of<T, TRest...>::value;
#endif

#if ETL_USING_CPP11
//***************************************************************************
/// Template to determine if a type is a base of all types in a specified list.
///\ingroup types
template <typename T, typename T1, typename... TRest>
struct is_base_of_all
{
static const bool value = etl::is_base_of<T, T1>::value &&
etl::is_base_of_all<T, TRest...>::value;
};

template <typename T, typename T1>
struct is_base_of_all<T, T1>
{
static const bool value = etl::is_base_of<T, T1>::value;
};
#endif

#if ETL_USING_CPP17
template <typename T, typename... TRest>
inline constexpr bool is_base_of_all_v = etl::is_base_of_all<T, TRest...>::value;
#endif

#if ETL_USING_CPP11
//***************************************************************************
/// Template to determine if a type is a base of any type in a specified list.
///\ingroup types
template <typename T, typename T1, typename... TRest>
struct is_base_of_any
{
static const bool value = etl::is_base_of<T, T1>::value ||
etl::is_base_of_any<T, TRest...>::value;
};

template <typename T, typename T1>
struct is_base_of_any<T, T1>
{
static const bool value = etl::is_base_of<T, T1>::value;
};
#endif

#if ETL_USING_CPP17
template <typename T, typename... TRest>
inline constexpr bool is_base_of_any_v = etl::is_base_of_any<T, TRest...>::value;
#endif

//***************************************************************************
/// A set of templates to allow related types to be derived.
///\ingroup types
Expand Down
115 changes: 115 additions & 0 deletions include/etl/variant_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,120 @@ SOFTWARE.

namespace etl
{
#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION)
//***************************************************************************
template <size_t MAX_SIZE_, typename ... Ts>
class variant_pool
: public etl::generic_pool<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment,
MAX_SIZE_>
{
public:

typedef etl::generic_pool<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment,
MAX_SIZE_> base_t;

static const size_t MAX_SIZE = MAX_SIZE_;

//*************************************************************************
/// Default constructor.
//*************************************************************************
variant_pool()
{
}

//*************************************************************************
/// Creates the object from a type. Variadic parameter constructor.
//*************************************************************************
template <typename T, typename... Args>
T* create(Args&&... args)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");

return base_t::template create<T>(etl::forward<Args>(args)...);
}

//*************************************************************************
/// Destroys the object.
//*************************************************************************
template <typename T>
void destroy(const T* const p)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");

base_t::destroy(p);
}

//*************************************************************************
/// Returns the maximum number of items in the variant_pool.
//*************************************************************************
size_t max_size() const
{
return MAX_SIZE;
}

private:

variant_pool(const variant_pool&) ETL_DELETE;
variant_pool& operator =(const variant_pool&) ETL_DELETE;
};

//***************************************************************************
template <typename ... Ts>
class variant_pool_ext
: public etl::generic_pool_ext<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment>
{
public:

typedef etl::generic_pool_ext<etl::largest<Ts...>::size,
etl::largest<Ts...>::alignment> base_t;

//*************************************************************************
/// Default constructor.
//*************************************************************************
variant_pool_ext(typename base_t::element* buffer, size_t size)
: base_t(buffer, size)
{
}

//*************************************************************************
/// Creates the object from a type. Variadic parameter constructor.
//*************************************************************************
template <typename T, typename... Args>
T* create(Args&&... args)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value), "Unsupported type");

return base_t::template create<T>(etl::forward<Args>(args)...);
}

//*************************************************************************
/// Destroys the object.
//*************************************************************************
template <typename T>
void destroy(const T* const p)
{
ETL_STATIC_ASSERT((etl::is_one_of<T, Ts...>::value || etl::is_base_of_any<T, Ts...>::value), "Invalid type");

base_t::destroy(p);
}

//*************************************************************************
/// Returns the maximum number of items in the variant_pool.
//*************************************************************************
size_t max_size() const
{
return base_t::max_size();
}

private:

variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
};
#else
//***************************************************************************
template <size_t MAX_SIZE_,
typename T1,
Expand Down Expand Up @@ -350,6 +464,7 @@ namespace etl
variant_pool_ext(const variant_pool_ext&) ETL_DELETE;
variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE;
};
#endif
}

#endif
1 change: 1 addition & 0 deletions test/etl_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ SOFTWARE.
#define ETL_FORWARD_LIST_FORCE_CPP03_IMPLEMENTATION
#define ETL_FLAT_SET_FORCE_CPP03_IMPLEMENTATION
#define ETL_FLAT_MULTISET_FORCE_CPP03_IMPLEMENTATION
#define ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION
#endif

#if defined(ETL_FORCE_TEST_CPP11)
Expand Down
Loading

0 comments on commit a2bd57c

Please sign in to comment.