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.

120 lines
2.9KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2018 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. template<class T>
  28. class Wrapper
  29. {
  30. public:
  31. template<class TT, class G, std::enable_if_t<std::is_constructible_v<T, TT>, int> = 0>
  32. Wrapper(TT&& v, G&& g) noexcept(std::is_nothrow_constructible_v<T, TT>) : value(std::forward<TT>(v))
  33. {
  34. g.release();
  35. }
  36. T& get() noexcept
  37. {
  38. return value;
  39. }
  40. const T& get() const noexcept
  41. {
  42. return value;
  43. }
  44. void reset(Wrapper<T>&& other) noexcept
  45. {
  46. value = std::move(other.value);
  47. }
  48. void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
  49. {
  50. value = std::forward<T>(newValue);
  51. }
  52. void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)
  53. {
  54. value = newValue;
  55. }
  56. using type = T;
  57. private:
  58. T value;
  59. };
  60. template<class T>
  61. class Wrapper<T&>
  62. {
  63. public:
  64. template<class TT, class G, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
  65. Wrapper(TT&& v, G&& g) noexcept(std::is_nothrow_constructible_v<TT, T&>) : value(static_cast<T&>(v))
  66. {
  67. g.release();
  68. }
  69. T& get() noexcept
  70. {
  71. return value.get();
  72. }
  73. const T& get() const noexcept
  74. {
  75. return value.get();
  76. }
  77. void reset(Wrapper<T>&& other) noexcept
  78. {
  79. value = std::move(other.value);
  80. }
  81. void reset(T& newValue) noexcept
  82. {
  83. value = std::ref(newValue);
  84. }
  85. using type = std::reference_wrapper<std::remove_reference_t<T>>;
  86. private:
  87. type value;
  88. };
  89. }