/** * @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 CxxYZ::enable_if::type ReserveImpl(T &container, size_t size) { container.reserve(size); } // --------------------------------------------------------------------------------------------------------------------------------- template typename CxxYZ::enable_if::type ReserveImpl(T &container, size_t size) { // Do nothing. } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template typename CxxYZ::enable_if::type EmplaceImpl(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value) { return container.emplace(CxxYZ::move(value)); } // --------------------------------------------------------------------------------------------------------------------------------- template typename CxxYZ::enable_if::type EmplaceImpl(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value) { return container.insert(value); } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template typename CxxYZ::enable_if::type EmplaceBackImpl(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value) { container.emplace_back(CxxYZ::move(value)); } // --------------------------------------------------------------------------------------------------------------------------------- template typename CxxYZ::enable_if::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 void Reserve(T &container, size_t size) { // Two-step for clang 3.7. Detail::ReserveImpl::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 ContainerTraits::EmplaceResultType::type Emplace(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value) { // Two-step for clang 3.7. return Detail::EmplaceImpl::type, ContainerTraits::HasEmplace::value>( container, CxxYZ::move(value)); } // --------------------------------------------------------------------------------------------------------------------------------- #if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) template typename ContainerTraits::EmplaceResultType::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 void EmplaceBack(T &container, ACTIV_BOOST_RV_REF(typename T::value_type) value) { // clang 3.7 is broken doing: // template // typename CxxYZ::enable_if::HasEmplace::value>::type EmplaceBack(T &container, ...); // So two-step to get the enable_if condition. Detail::EmplaceBackImpl::value>(container, CxxYZ::move(value)); } // --------------------------------------------------------------------------------------------------------------------------------- #if (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) template 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)