Files

160 lines
5.6 KiB
C++

/**
* @file include/ActivMiddleware/Misc/ContainerHelper.h
*
* @brief Header for miscellaneous container helper functions.
*/
#if (!defined ACTIV_MISC_CONTAINER_HELPER_H)
#define ACTIV_MISC_CONTAINER_HELPER_H
#include "ActivMiddleware/Misc/ContainerTraits.h"
//! Activ namespace.
namespace Activ
{
//! ContainerHelper namespace.
namespace ContainerHelper
{
namespace Detail
{
template<typename T, bool HAS_RESERVE>
typename CxxYZ::enable_if<HAS_RESERVE>::type ReserveImpl(T &container, size_t size)
{
container.reserve(size);
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T, bool HAS_RESERVE>
typename CxxYZ::enable_if<!HAS_RESERVE>::type ReserveImpl(T &container, size_t size)
{
// Do nothing.
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename RESULT_TYPE, bool HAS_EMPLACE, typename T>
typename CxxYZ::enable_if<HAS_EMPLACE, RESULT_TYPE>::type EmplaceImpl(T &container,
ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
return container.emplace(CxxYZ::move(value));
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename RESULT_TYPE, bool HAS_EMPLACE, typename T>
typename CxxYZ::enable_if<!HAS_EMPLACE, RESULT_TYPE>::type EmplaceImpl(T &container,
ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
return container.insert(value);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool HAS_EMPLACE, typename T>
typename CxxYZ::enable_if<HAS_EMPLACE>::type EmplaceBackImpl(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
container.emplace_back(CxxYZ::move(value));
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<bool HAS_EMPLACE, typename T>
typename CxxYZ::enable_if<!HAS_EMPLACE>::type EmplaceBackImpl(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
container.push_back(value);
}
} // namespace Detail
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Call reserve(size) on a container if it provides such a call, otherwise do nothing.
template<typename T>
void Reserve(T &container, size_t size)
{
// Two-step for clang 3.7.
Detail::ReserveImpl<T, ContainerTraits::HasReserve<T>::value>(container, size);
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Try to emplace an item in a container. If the container doesn't support emplace(), fallback to insert().
*
* @tparam T the container's type.
*
* @param container
* @param value rvalue reference (or ACTIV_BOOST_RV_REF) to the value to emplace.
*/
template<typename T>
typename ContainerTraits::EmplaceResultType<T>::type Emplace(T &container,
ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
// Two-step for clang 3.7.
return Detail::EmplaceImpl<typename ContainerTraits::EmplaceResultType<T>::type, ContainerTraits::HasEmplace<T>::value>(
container, CxxYZ::move(value));
}
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
template<typename T>
typename ContainerTraits::EmplaceResultType<T>::type Emplace(T &container, const typename T::value_type &value)
{
// Isn't a Boost::move-enabled value_type (or wasn't Boost::move()d), so just insert() irrespective of whether emplace()
// is available.
return container.insert(value);
}
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Try to emplace an item on the end of a container. If the container doesn't support emplace_back(),
* fallback to push_back().
*
* @tparam T the container's type.
*
* @param container
* @param value rvalue reference (or ACTIV_BOOST_RV_REF) to the value to emplace.
*/
template<typename T>
void EmplaceBack(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value)
{
// clang 3.7 is broken doing:
// template<typename T>
// typename CxxYZ::enable_if<ContainerTraits<T>::HasEmplace::value>::type EmplaceBack(T &container, ...);
// So two-step to get the enable_if condition.
Detail::EmplaceBackImpl<ContainerTraits::HasEmplace<T>::value>(container, CxxYZ::move(value));
}
// ---------------------------------------------------------------------------------------------------------------------------------
#if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
template<typename T>
void EmplaceBack(T &container, const typename T::value_type &value)
{
// Isn't a Boost::move-enabled value_type (or wasn't Boost::move()d), so just push_back() irrespective of whether
// emplace_back() is available.
container.push_back(value);
}
#endif
} // namespace ContainerHelper
} // namespace Activ
#endif // (!defined ACTIV_MISC_CONTAINER_HELPER_H)