StdMakeUnique.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. // VS 2013 defines __cplusplus as 199711L but has std::make_unique.
  6. #if __cplusplus > 201103L || _MSC_VER >= 1800
  7. #include <memory>
  8. #else
  9. // Implementation of C++14 std::make_unique, which is not supported by all the
  10. // compilers we care about yet.
  11. //
  12. // NOTE: This code is based on the libc++ implementation of std::make_unique.
  13. //
  14. // Copyright (c) 2009-2014 by the libc++ contributors.
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining a copy
  17. // of this software and associated documentation files (the "Software"), to
  18. // deal in the Software without restriction, including without limitation the
  19. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  20. // sell copies of the Software, and to permit persons to whom the Software is
  21. // furnished to do so, subject to the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be included in
  24. // all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. // IN THE SOFTWARE.
  33. #include <cstddef>
  34. #include <memory>
  35. #include <type_traits>
  36. namespace std
  37. {
  38. struct unspecified {};
  39. template<class T>
  40. struct unique_if
  41. {
  42. typedef std::unique_ptr<T> unique_single;
  43. };
  44. template<class T>
  45. struct unique_if<T[]>
  46. {
  47. typedef std::unique_ptr<T[]> unique_array_unknown_bound;
  48. };
  49. template<class T, size_t N>
  50. struct unique_if<T[N]>
  51. {
  52. typedef void unique_array_known_bound;
  53. };
  54. template<class T, class... Args>
  55. inline typename unique_if<T>::unique_single make_unique(Args&&... args)
  56. {
  57. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  58. }
  59. template<class T>
  60. inline typename unique_if<T>::unique_array_unknown_bound make_unique(size_t n)
  61. {
  62. typedef typename std::remove_extent<T>::type U;
  63. return std::unique_ptr<T>(new U[n]());
  64. }
  65. template<class T, class... Args>
  66. typename unique_if<T>::unique_array_known_bound make_unique(Args&&...) = delete;
  67. } // namespace std
  68. #endif // __cplusplus