Loading...
Searching...
No Matches
variant_pool.h
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 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_VARIANT_POOL_INCLUDED
#define ETL_VARIANT_POOL_INCLUDED
#include "platform.h"
#include "largest.h"
#include "pool.h"
#include "static_assert.h"
#include "type_traits.h"
#include <stddef.h>
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_;
//*************************************************************************
//*************************************************************************
variant_pool() {}
//*************************************************************************
//*************************************************************************
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)...);
}
//*************************************************************************
//*************************************************************************
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);
}
//*************************************************************************
//*************************************************************************
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;
//*************************************************************************
//*************************************************************************
variant_pool_ext(typename base_t::element* buffer, size_t size)
: base_t(buffer, size)
{
}
//*************************************************************************
//*************************************************************************
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)...);
}
//*************************************************************************
//*************************************************************************
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);
}
//*************************************************************************
//*************************************************************************
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
#include "private/variant_pool_cpp03.h"
#endif
} // namespace etl
#endif
etl::variant_pool_ext
Definition variant_pool.h:201
etl::variant_pool_ext::create
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:220
etl::variant_pool_ext::destroy
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:287
etl::variant_pool_ext::variant_pool_ext
variant_pool_ext(typename base_t::element *buffer, size_t size)
Default constructor.
Definition variant_pool.h:210
etl::variant_pool_ext::max_size
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:313
etl::variant_pool
Definition variant_pool.h:56
etl::variant_pool::max_size
size_t max_size() const
Returns the maximum number of items in the variant_pool.
Definition variant_pool.h:170
etl::variant_pool::destroy
void destroy(const T *const p)
Destroys the object.
Definition variant_pool.h:144
etl::variant_pool::create
T * create()
Creates the object. Default constructor.
Definition variant_pool.h:77
etl::variant_pool::variant_pool
variant_pool()
Default constructor.
Definition variant_pool.h:68
variant_pool_ext
Definition variant_pool_cpp03.h:200
etl::largest
Definition largest.h:45
etl::ipool::size
size_t size() const
Returns the number of allocated items in the pool.
Definition ipool.h:523
etl::generic_pool_ext
Definition generic_pool.h:216
largest.h
etl
Definition absolute.h:40
platform.h
pool.h
type_traits.h