You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file make_unique.h
  25. * @brief Implementation of std::make_unique for pre C++14 compilation
  26. * environments
  27. */
  28. #ifndef MAKE_UNIQUE_H
  29. #define MAKE_UNIQUE_H
  30. // Define std::make_unique if the compiler is C++11, but not C++14
  31. // (which provides this itself)
  32. // When C++14 support is a KiCad pre-requisite, this entire file
  33. // can be removed.
  34. #if __cplusplus == 201103L
  35. #include <cstddef>
  36. #include <memory>
  37. #include <type_traits>
  38. #include <utility>
  39. // It's a bit naughty to add things to std::, but the point is to
  40. // "polyfill" this function in only if std:: doesn't have it
  41. //
  42. // This implementation is the one proposed by Stephan T. Lavavej
  43. // in N3656: https://isocpp.org/files/papers/N3656.txt
  44. // This is more or less exactly how it is implemented in GCC (e.g. 6.3.1)
  45. // when C++14 is enabled.
  46. namespace std
  47. {
  48. template<class T> struct _Unique_if {
  49. typedef unique_ptr<T> _Single_object;
  50. };
  51. template<class T> struct _Unique_if<T[]> {
  52. typedef unique_ptr<T[]> _Unknown_bound;
  53. };
  54. template<class T, size_t N> struct _Unique_if<T[N]> {
  55. typedef void _Known_bound;
  56. };
  57. /// std::make_unique for single objects
  58. template<class T, class... Args>
  59. typename _Unique_if<T>::_Single_object
  60. make_unique(Args&&... args) {
  61. return unique_ptr<T>(new T(std::forward<Args>(args)...));
  62. }
  63. /// std::make_unique for arrays of unknown bound
  64. template<class T>
  65. typename _Unique_if<T>::_Unknown_bound
  66. make_unique(size_t n) {
  67. typedef typename remove_extent<T>::type U;
  68. return unique_ptr<T>(new U[n]());
  69. }
  70. /// Disable std::make_unique for arrays of known bound
  71. template<class T, class... Args>
  72. typename _Unique_if<T>::_Known_bound
  73. make_unique(Args&&...) = delete;
  74. }
  75. #endif // __cplusplus == 201103L
  76. #endif // WXSTRUCT_H_