diff --git a/include/etl/generators/type_traits_generator.h b/include/etl/generators/type_traits_generator.h index d8c1ee605..35db7ea62 100644 --- a/include/etl/generators/type_traits_generator.h +++ b/include/etl/generators/type_traits_generator.h @@ -1406,6 +1406,52 @@ typedef integral_constant true_type; inline constexpr bool is_one_of_v = etl::is_one_of::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 + struct is_base_of_all + { + static const bool value = etl::is_base_of::value && + etl::is_base_of_all::value; + }; + + template + struct is_base_of_all + { + static const bool value = etl::is_base_of::value; + }; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool is_base_of_all_v = etl::is_base_of_all::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 + struct is_base_of_any + { + static const bool value = etl::is_base_of::value || + etl::is_base_of_any::value; + }; + + template + struct is_base_of_any + { + static const bool value = etl::is_base_of::value; + }; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool is_base_of_any_v = etl::is_base_of_any::value; +#endif + //*************************************************************************** /// A set of templates to allow related types to be derived. ///\ingroup types diff --git a/include/etl/generators/variant_pool_generator.h b/include/etl/generators/variant_pool_generator.h index 1ff767588..a212a1e51 100644 --- a/include/etl/generators/variant_pool_generator.h +++ b/include/etl/generators/variant_pool_generator.h @@ -73,6 +73,120 @@ cog.outl("//******************************************************************** namespace etl { +#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION) + //*************************************************************************** + template + class variant_pool + : public etl::generic_pool::size, + etl::largest::alignment, + MAX_SIZE_> + { + public: + + typedef etl::generic_pool::size, + etl::largest::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 + T* create(Args&&... args) + { + ETL_STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + return base_t::template create(etl::forward(args)...); + } + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + void destroy(const T* const p) + { + ETL_STATIC_ASSERT((etl::is_one_of::value || etl::is_base_of_any::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 + class variant_pool_ext + : public etl::generic_pool_ext::size, + etl::largest::alignment> + { + public: + + typedef etl::generic_pool_ext::size, + etl::largest::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 + T* create(Args&&... args) + { + ETL_STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + return base_t::template create(etl::forward(args)...); + } + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + void destroy(const T* const p) + { + ETL_STATIC_ASSERT((etl::is_one_of::value || etl::is_base_of_any::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 @@ -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 diff --git a/include/etl/type_traits.h b/include/etl/type_traits.h index 43f01de3c..14831ed80 100644 --- a/include/etl/type_traits.h +++ b/include/etl/type_traits.h @@ -1399,6 +1399,52 @@ typedef integral_constant true_type; inline constexpr bool is_one_of_v = etl::is_one_of::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 + struct is_base_of_all + { + static const bool value = etl::is_base_of::value && + etl::is_base_of_all::value; + }; + + template + struct is_base_of_all + { + static const bool value = etl::is_base_of::value; + }; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool is_base_of_all_v = etl::is_base_of_all::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 + struct is_base_of_any + { + static const bool value = etl::is_base_of::value || + etl::is_base_of_any::value; + }; + + template + struct is_base_of_any + { + static const bool value = etl::is_base_of::value; + }; +#endif + +#if ETL_USING_CPP17 + template + inline constexpr bool is_base_of_any_v = etl::is_base_of_any::value; +#endif + //*************************************************************************** /// A set of templates to allow related types to be derived. ///\ingroup types diff --git a/include/etl/variant_pool.h b/include/etl/variant_pool.h index cb5ae7fd8..d97b735a9 100644 --- a/include/etl/variant_pool.h +++ b/include/etl/variant_pool.h @@ -61,6 +61,120 @@ SOFTWARE. namespace etl { +#if ETL_USING_CPP11 && !defined(ETL_VARIANT_POOL_FORCE_CPP03_IMPLEMENTATION) + //*************************************************************************** + template + class variant_pool + : public etl::generic_pool::size, + etl::largest::alignment, + MAX_SIZE_> + { + public: + + typedef etl::generic_pool::size, + etl::largest::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 + T* create(Args&&... args) + { + ETL_STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + return base_t::template create(etl::forward(args)...); + } + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + void destroy(const T* const p) + { + ETL_STATIC_ASSERT((etl::is_one_of::value || etl::is_base_of_any::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 + class variant_pool_ext + : public etl::generic_pool_ext::size, + etl::largest::alignment> + { + public: + + typedef etl::generic_pool_ext::size, + etl::largest::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 + T* create(Args&&... args) + { + ETL_STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); + + return base_t::template create(etl::forward(args)...); + } + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + void destroy(const T* const p) + { + ETL_STATIC_ASSERT((etl::is_one_of::value || etl::is_base_of_any::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 ::value) == (std::is_trivially_copyable::value)); #endif #endif +#endif + } + + //************************************************************************* + TEST(test_is_base_of_any) + { + struct Base {}; + struct D1 : Base {}; + struct D2 : Base {}; + struct D3 : Base {}; + struct D4 {}; + +#if ETL_USING_CPP17 + CHECK_TRUE(bool(etl::is_base_of_any_v)); + CHECK_FALSE(bool(etl::is_base_of_any_v)); +#else + CHECK_TRUE(bool(etl::is_base_of_any::value)); + CHECK_FALSE(bool(etl::is_base_of_any::value)); +#endif + } + + //************************************************************************* + TEST(test_is_base_of_all) + { + struct Base {}; + struct D1 : Base {}; + struct D2 : Base {}; + struct D3 : Base {}; + struct D4 {}; + +#if ETL_USING_CPP17 + CHECK_TRUE(bool(etl::is_base_of_all_v)); + CHECK_FALSE(bool(etl::is_base_of_all_v)); +#else + CHECK_TRUE(bool(etl::is_base_of_all::value)); + CHECK_FALSE(bool(etl::is_base_of_all::value)); #endif } }