summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Miodrag Milanovic <mmicko@gmail.com>2016-01-08 13:38:18 +0100
committer Miodrag Milanovic <mmicko@gmail.com>2016-01-08 13:38:18 +0100
commit0da7a86a8c60d9772d2ebda150843779a5e17fd6 (patch)
tree3843278ab3f06a489932019dc7eb77ed45cbc723
parentfff0c4d0541b44958b8c0108dfe230ac42212a61 (diff)
make make make_unique_clear accept all kind of parameters (nw)
-rw-r--r--src/lib/util/corealloc.h56
-rw-r--r--src/lib/util/coretmpl.h29
2 files changed, 54 insertions, 31 deletions
diff --git a/src/lib/util/corealloc.h b/src/lib/util/corealloc.h
index d5eec557afa..0eab4b5123e 100644
--- a/src/lib/util/corealloc.h
+++ b/src/lib/util/corealloc.h
@@ -17,6 +17,7 @@
#include <new>
#include <type_traits>
#include <utility>
+#include <memory>
#include "osdcore.h"
@@ -35,7 +36,7 @@
template<typename _Tp, typename... _Args>
inline _Tp* global_alloc_clear(_Args&&... __args)
{
- UINT8* ptr = new UINT8[sizeof(_Tp)]; // allocate memory
+ unsigned char * ptr = new unsigned char[sizeof(_Tp)]; // allocate memory
memset(ptr, 0, sizeof(_Tp));
return new(ptr) _Tp(std::forward<_Args>(__args)...);
}
@@ -44,10 +45,61 @@ template<typename _Tp>
inline _Tp* global_alloc_array_clear(size_t __num)
{
auto size = sizeof(_Tp) * __num;
- UINT8* ptr = new UINT8[size]; // allocate memory
+ unsigned char* ptr = new unsigned char[size]; // allocate memory
memset(ptr, 0, size);
return new(ptr) _Tp[__num]();
}
+
+template<typename _Tp>
+struct _MakeUniqClear
+{
+ typedef std::unique_ptr<_Tp> __single_object;
+};
+
+template<typename _Tp>
+struct _MakeUniqClear<_Tp[]>
+{
+ typedef std::unique_ptr<_Tp[]> __array;
+};
+
+template<typename _Tp, size_t _Bound>
+struct _MakeUniqClear<_Tp[_Bound]>
+{
+ struct __invalid_type { };
+};
+
+/// make_unique_clear for single objects
+template<typename _Tp, typename... _Args>
+inline typename _MakeUniqClear<_Tp>::__single_object make_unique_clear(_Args&&... __args)
+{
+ unsigned char* ptr = new unsigned char[sizeof(_Tp)]; // allocate memory
+ memset(ptr, 0, sizeof(_Tp));
+ return std::unique_ptr<_Tp>(new(ptr) _Tp(std::forward<_Args>(__args)...));
+}
+
+/// make_unique_clear for arrays of unknown bound
+template<typename _Tp>
+inline typename _MakeUniqClear<_Tp>::__array make_unique_clear(size_t __num)
+{
+ auto size = sizeof(std::remove_extent_t<_Tp>) * __num;
+ unsigned char* ptr = new unsigned char[size]; // allocate memory
+ memset(ptr, 0, size);
+ return std::unique_ptr<_Tp>(new(ptr) std::remove_extent_t<_Tp>[__num]());
+}
+
+template<typename _Tp, unsigned char _F>
+inline typename _MakeUniqClear<_Tp>::__array make_unique_clear(size_t __num)
+{
+ auto size = sizeof(std::remove_extent_t<_Tp>) * __num;
+ unsigned char* ptr = new unsigned char[size]; // allocate memory
+ memset(ptr, _F, size);
+ return std::unique_ptr<_Tp>(new(ptr) std::remove_extent_t<_Tp>[__num]());
+}
+
+/// Disable make_unique_clear for arrays of known bound
+template<typename _Tp, typename... _Args>
+inline typename _MakeUniqClear<_Tp>::__invalid_type make_unique_clear(_Args&&...) = delete;
+
#endif /* __COREALLOC_H__ */
diff --git a/src/lib/util/coretmpl.h b/src/lib/util/coretmpl.h
index c97249de9f1..9201041b2d9 100644
--- a/src/lib/util/coretmpl.h
+++ b/src/lib/util/coretmpl.h
@@ -17,7 +17,6 @@
#include "corealloc.h"
#include <vector>
-#include <memory>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#include <yvals.h>
@@ -33,34 +32,6 @@
typedef std::vector<UINT8> dynamic_buffer;
-template<typename T>
-inline std::unique_ptr<T> make_unique_clear(std::size_t size)
-{
- auto ptr = std::make_unique<T>(size);
- static_assert(std::is_array<T>::value, "Type must be array");
- memset(ptr.get(), 0, sizeof(std::remove_extent<T>) * size);
- return ptr;
-}
-
-template<typename T,unsigned char F>
-inline std::unique_ptr<T> make_unique_clear(std::size_t size)
-{
- auto ptr = std::make_unique<T>(size);
- static_assert(std::is_array<T>::value, "Type must be array");
- memset(ptr.get(), F, sizeof(std::remove_extent<T>) * size);
- return ptr;
-}
-
-
-template<typename T>
-inline std::unique_ptr<T> make_unique_clear()
-{
- auto ptr = std::make_unique<T>();
- static_assert(std::is_pod<T>::value, "Type must be plain old data type");
- memset(ptr.get(), 0, sizeof(T));
- return ptr;
-}
-
// ======================> simple_list