aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-04-04 11:47:54 -0400
committerJack Lloyd <[email protected]>2017-04-04 11:47:54 -0400
commitd4c3f6a3409ce0ad6520c13813310f6cb523fe6f (patch)
treebbcf59d0450e48ab833ef265456293ee34e07fc3 /src
parent6f153656b234c42a690ce898931ce534b29c3e69 (diff)
parent21f319c4d7a9fc5e892b14922c61b9c4f30aa646 (diff)
Merge GH #974 Add wrapper for make_unique
Diffstat (limited to 'src')
-rw-r--r--src/lib/utils/info.txt1
-rw-r--r--src/lib/utils/stl_compatibility.h77
2 files changed, 78 insertions, 0 deletions
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
</header:public>
<header:internal>
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 <memory>
+
+#if __cplusplus < 201402L
+#include <cstddef>
+#include <type_traits>
+#include <utility>
+#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 <typename T, typename ... Args>
+constexpr auto make_unique(Args&&... args)
+ {
+ return std::make_unique<T>(std::forward<Args>(args)...);
+ }
+
+template<class T>
+constexpr auto make_unique(std::size_t size)
+ {
+ return std::make_unique<T>(size);
+ }
+
+#else
+namespace stlCompatibilityDetails
+{
+template<class T> struct _Unique_if
+ {
+ typedef std::unique_ptr<T> _Single_object;
+ };
+
+template<class T> struct _Unique_if<T[]>
+ {
+ typedef std::unique_ptr<T[]> _Unknown_bound;
+ };
+
+template<class T, size_t N> struct _Unique_if<T[N]>
+ {
+ typedef void _Known_bound;
+ };
+} // namespace stlCompatibilityDetails
+
+template<class T, class... Args>
+typename stlCompatibilityDetails::_Unique_if<T>::_Single_object make_unique(Args&&... args)
+ {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+ }
+
+template<class T>
+typename stlCompatibilityDetails::_Unique_if<T>::_Unknown_bound make_unique(size_t n)
+ {
+ typedef typename std::remove_extent<T>::type U;
+ return std::unique_ptr<T>(new U[n]());
+ }
+
+template<class T, class... Args>
+typename stlCompatibilityDetails::_Unique_if<T>::_Known_bound make_unique(Args&&...) = delete;
+
+#endif
+
+} // namespace Botan
+#endif