From 21f319c4d7a9fc5e892b14922c61b9c4f30aa646 Mon Sep 17 00:00:00 2001 From: Tomasz Frydrych Date: Tue, 4 Apr 2017 17:25:56 +0200 Subject: add Botan::make_unique --- src/lib/utils/info.txt | 1 + src/lib/utils/stl_compatibility.h | 77 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/lib/utils/stl_compatibility.h diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt index da84b64b4..193145c5d 100644 --- a/src/lib/utils/info.txt +++ b/src/lib/utils/info.txt @@ -22,6 +22,7 @@ parsing.h rotate.h types.h version.h +stl_compatibility.h diff --git a/src/lib/utils/stl_compatibility.h b/src/lib/utils/stl_compatibility.h new file mode 100644 index 000000000..178afed52 --- /dev/null +++ b/src/lib/utils/stl_compatibility.h @@ -0,0 +1,77 @@ +/* +* STL standards compatibility functions +* (C) 2017 Tomasz Frydrych +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_STL_COMPATIBILITY_H__ +#define BOTAN_STL_COMPATIBILITY_H__ + +#include + +#if __cplusplus < 201402L +#include +#include +#include +#endif + +namespace Botan +{ +/* +* std::make_unique functionality similar as we have in C++14. +* C++11 version based on proposal for C++14 implemenatation by Stephan T. Lavavej +* source: https://isocpp.org/files/papers/N3656.txt +*/ +#if __cplusplus >= 201402L +template +constexpr auto make_unique(Args&&... args) + { + return std::make_unique(std::forward(args)...); + } + +template +constexpr auto make_unique(std::size_t size) + { + return std::make_unique(size); + } + +#else +namespace stlCompatibilityDetails +{ +template struct _Unique_if + { + typedef std::unique_ptr _Single_object; + }; + +template struct _Unique_if + { + typedef std::unique_ptr _Unknown_bound; + }; + +template struct _Unique_if + { + typedef void _Known_bound; + }; +} // namespace stlCompatibilityDetails + +template +typename stlCompatibilityDetails::_Unique_if::_Single_object make_unique(Args&&... args) + { + return std::unique_ptr(new T(std::forward(args)...)); + } + +template +typename stlCompatibilityDetails::_Unique_if::_Unknown_bound make_unique(size_t n) + { + typedef typename std::remove_extent::type U; + return std::unique_ptr(new U[n]()); + } + +template +typename stlCompatibilityDetails::_Unique_if::_Known_bound make_unique(Args&&...) = delete; + +#endif + +} // namespace Botan +#endif -- cgit v1.2.3