Chromium Code Reviews| Index: mojo/edk/system/make_unique.h |
| diff --git a/mojo/edk/system/make_unique.h b/mojo/edk/system/make_unique.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ef2680bbe20ef87af135a33b1a62c244f3f00d40 |
| --- /dev/null |
| +++ b/mojo/edk/system/make_unique.h |
| @@ -0,0 +1,57 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This is almost the source from N3656 ("make_unique (Revision 1)"; |
| +// https://isocpp.org/files/papers/N3656.txt) almost verbatim, so that we have a |
| +// "make_unique" to use until we can use C++14. The following changes have been |
| +// made: |
| +// - It's called |MakeUnique| instead of |make_unique|. |
| +// - It's in the |mojo::system| namespace instead of |std|; this also |
| +// necessitates adding some |std::|s. |
| +// - It's been formatted. |
|
jamesr
2015/09/17 22:07:49
"It is been formatted"?
|
| + |
| +#ifndef MOJO_EDK_SYSTEM_MAKE_UNIQUE_H_ |
| +#define MOJO_EDK_SYSTEM_MAKE_UNIQUE_H_ |
| + |
| +#include <cstddef> |
| +#include <memory> |
| +#include <type_traits> |
| +#include <utility> |
| + |
| +namespace mojo { |
| +namespace system { |
| + |
| +template <class T> |
| +struct _Unique_if { |
|
kulakowski
2015/09/17 22:12:15
Nit: Identifiers starting with an underscore follo
|
| + 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; |
| +}; |
| + |
| +template <class T, class... Args> |
| +typename _Unique_if<T>::_Single_object MakeUnique(Args&&... args) { |
| + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| +} |
| + |
| +template <class T> |
| +typename _Unique_if<T>::_Unknown_bound MakeUnique(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 _Unique_if<T>::_Known_bound MakeUnique(Args&&...) = delete; |
| + |
| +} // namespace system |
| +} // namespace mojo |
| + |
| +#endif // MOJO_EDK_SYSTEM_MAKE_UNIQUE_H_ |