No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

132 líneas
3.2KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2021 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include <functional>
  24. #include <type_traits>
  25. namespace sr::detail
  26. {
  27. struct NoopGuard
  28. {
  29. constexpr void release() const
  30. {
  31. }
  32. };
  33. template<class T>
  34. class Wrapper
  35. {
  36. public:
  37. template<class TT, class G = NoopGuard, std::enable_if_t<std::is_constructible_v<T, TT>, int> = 0>
  38. Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<T, TT>) : value(std::forward<TT>(v))
  39. {
  40. g.release();
  41. }
  42. T& get() noexcept
  43. {
  44. return value;
  45. }
  46. const T& get() const noexcept
  47. {
  48. return value;
  49. }
  50. void reset(Wrapper<T>&& other) noexcept
  51. {
  52. value = std::move(other.value);
  53. }
  54. void reset(const Wrapper<T>& other) noexcept(std::is_nothrow_assignable_v<T, const T&>)
  55. {
  56. value = other.value;
  57. }
  58. void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
  59. {
  60. value = std::forward<T>(newValue);
  61. }
  62. void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)
  63. {
  64. value = newValue;
  65. }
  66. using type = T;
  67. private:
  68. T value;
  69. };
  70. template<class T>
  71. class Wrapper<T&>
  72. {
  73. public:
  74. template<class TT, class G = NoopGuard, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
  75. Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<TT, T&>) : value(static_cast<T&>(v))
  76. {
  77. g.release();
  78. }
  79. T& get() noexcept
  80. {
  81. return value.get();
  82. }
  83. const T& get() const noexcept
  84. {
  85. return value.get();
  86. }
  87. void reset(Wrapper<T>&& other) noexcept
  88. {
  89. value = std::move(other.value);
  90. }
  91. void reset(T& newValue) noexcept
  92. {
  93. value = std::ref(newValue);
  94. }
  95. using type = std::reference_wrapper<std::remove_reference_t<T>>;
  96. private:
  97. type value;
  98. };
  99. }