4
0

Injected.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <memory>
  21. namespace ableton
  22. {
  23. namespace util
  24. {
  25. // Utility type for aiding in dependency injection.
  26. // Base template and implementation for injected valued
  27. template <typename T>
  28. struct Injected
  29. {
  30. using type = T;
  31. Injected() = default;
  32. explicit Injected(T t)
  33. : val(std::move(t))
  34. {
  35. }
  36. Injected(const Injected&) = default;
  37. Injected& operator=(const Injected&) = default;
  38. Injected(Injected&& rhs)
  39. : val(std::move(rhs.val))
  40. {
  41. }
  42. Injected& operator=(Injected&& rhs)
  43. {
  44. val = std::move(rhs.val);
  45. return *this;
  46. }
  47. T* operator->() { return &val; }
  48. const T* operator->() const { return &val; }
  49. T& operator*() { return val; }
  50. const T& operator*() const { return val; }
  51. T val;
  52. };
  53. // Utility function for injecting values
  54. template <typename T>
  55. Injected<T> injectVal(T t)
  56. {
  57. return Injected<T>(std::move(t));
  58. }
  59. // Specialization for injected references
  60. template <typename T>
  61. struct Injected<T&>
  62. {
  63. using type = T;
  64. explicit Injected(T& t)
  65. : ref(std::ref(t))
  66. {
  67. }
  68. Injected(const Injected&) = default;
  69. Injected& operator=(const Injected&) = default;
  70. Injected(Injected&& rhs)
  71. : ref(std::move(rhs.ref))
  72. {
  73. }
  74. Injected& operator=(Injected&& rhs)
  75. {
  76. ref = std::move(rhs.ref);
  77. return *this;
  78. }
  79. T* operator->() { return &ref.get(); }
  80. const T* operator->() const { return &ref.get(); }
  81. T& operator*() { return ref; }
  82. const T& operator*() const { return ref; }
  83. std::reference_wrapper<T> ref;
  84. };
  85. // Utility function for injecting references
  86. template <typename T>
  87. Injected<T&> injectRef(T& t)
  88. {
  89. return Injected<T&>(t);
  90. }
  91. // Specialization for injected shared_ptr
  92. template <typename T>
  93. struct Injected<std::shared_ptr<T>>
  94. {
  95. using type = T;
  96. explicit Injected(std::shared_ptr<T> pT)
  97. : shared(std::move(pT))
  98. {
  99. }
  100. Injected(const Injected&) = default;
  101. Injected& operator=(const Injected&) = default;
  102. Injected(Injected&& rhs)
  103. : shared(std::move(rhs.shared))
  104. {
  105. }
  106. Injected& operator=(Injected&& rhs)
  107. {
  108. shared = std::move(rhs.shared);
  109. return *this;
  110. }
  111. T* operator->() { return shared.get(); }
  112. const T* operator->() const { return shared.get(); }
  113. T& operator*() { return *shared; }
  114. const T& operator*() const { return *shared; }
  115. std::shared_ptr<T> shared;
  116. };
  117. // Utility function for injected shared_ptr
  118. template <typename T>
  119. Injected<std::shared_ptr<T>> injectShared(std::shared_ptr<T> shared)
  120. {
  121. return Injected<std::shared_ptr<T>>(std::move(shared));
  122. }
  123. // Specialization for injected unique_ptr
  124. template <typename T>
  125. struct Injected<std::unique_ptr<T>>
  126. {
  127. using type = T;
  128. explicit Injected(std::unique_ptr<T> pT)
  129. : unique(std::move(pT))
  130. {
  131. }
  132. Injected(const Injected&) = default;
  133. Injected& operator=(const Injected&) = default;
  134. Injected(Injected&& rhs)
  135. : unique(std::move(rhs.unique))
  136. {
  137. }
  138. Injected& operator=(Injected&& rhs)
  139. {
  140. unique = std::move(rhs.unique);
  141. return *this;
  142. }
  143. T* operator->() { return unique.get(); }
  144. const T* operator->() const { return unique.get(); }
  145. T& operator*() { return *unique; }
  146. const T& operator*() const { return *unique; }
  147. std::unique_ptr<T> unique;
  148. };
  149. // Utility function for injected unique_ptr
  150. template <typename T>
  151. Injected<std::unique_ptr<T>> injectUnique(std::unique_ptr<T> unique)
  152. {
  153. return Injected<std::unique_ptr<T>>(std::move(unique));
  154. }
  155. } // namespace util
  156. } // namespace ableton