Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
6 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2020 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(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
  55. {
  56. value = std::forward<T>(newValue);
  57. }
  58. void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)
  59. {
  60. value = newValue;
  61. }
  62. using type = T;
  63. private:
  64. T value;
  65. };
  66. template<class T>
  67. class Wrapper<T&>
  68. {
  69. public:
  70. template<class TT, class G = NoopGuard, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
  71. Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<TT, T&>) : value(static_cast<T&>(v))
  72. {
  73. g.release();
  74. }
  75. T& get() noexcept
  76. {
  77. return value.get();
  78. }
  79. const T& get() const noexcept
  80. {
  81. return value.get();
  82. }
  83. void reset(Wrapper<T>&& other) noexcept
  84. {
  85. value = std::move(other.value);
  86. }
  87. void reset(T& newValue) noexcept
  88. {
  89. value = std::ref(newValue);
  90. }
  91. using type = std::reference_wrapper<std::remove_reference_t<T>>;
  92. private:
  93. type value;
  94. };
  95. }