raii.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_RAII_HPP
  19. #define SOL_RAII_HPP
  20. #include <sol/traits.hpp>
  21. #include <sol/compatibility.hpp>
  22. #include <memory>
  23. namespace sol {
  24. namespace detail {
  25. struct default_construct {
  26. template <typename T, typename... Args>
  27. static void construct(T&& obj, Args&&... args) {
  28. typedef meta::unqualified_t<T> Tu;
  29. std::allocator<Tu> alloc {};
  30. std::allocator_traits<std::allocator<Tu>>::construct(alloc, std::forward<T>(obj), std::forward<Args>(args)...);
  31. }
  32. template <typename T, typename... Args>
  33. void operator()(T&& obj, Args&&... args) const {
  34. construct(std::forward<T>(obj), std::forward<Args>(args)...);
  35. }
  36. };
  37. struct default_destroy {
  38. template <typename T>
  39. static void destroy(T&& obj) {
  40. std::allocator<meta::unqualified_t<T>> alloc {};
  41. alloc.destroy(obj);
  42. }
  43. template <typename T>
  44. void operator()(T&& obj) const {
  45. destroy(std::forward<T>(obj));
  46. }
  47. };
  48. struct deleter {
  49. template <typename T>
  50. void operator()(T* p) const {
  51. delete p;
  52. }
  53. };
  54. struct state_deleter {
  55. void operator()(lua_State* L) const {
  56. lua_close(L);
  57. }
  58. };
  59. template <typename T, typename Dx, typename... Args>
  60. inline std::unique_ptr<T, Dx> make_unique_deleter(Args&&... args) {
  61. return std::unique_ptr<T, Dx>(new T(std::forward<Args>(args)...));
  62. }
  63. template <typename Tag, typename T>
  64. struct tagged {
  65. private:
  66. T value_;
  67. public:
  68. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, tagged>> = meta::enabler>
  69. tagged(Arg&& arg, Args&&... args) : value_(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  70. }
  71. T& value() & {
  72. return value_;
  73. }
  74. T const& value() const& {
  75. return value_;
  76. }
  77. T&& value() && {
  78. return std::move(value_);
  79. }
  80. };
  81. } // namespace detail
  82. template <typename... Args>
  83. struct constructor_list { };
  84. template <typename... Args>
  85. using constructors = constructor_list<Args...>;
  86. const auto default_constructor = constructors<types<>> {};
  87. struct no_construction { };
  88. const auto no_constructor = no_construction {};
  89. struct call_construction { };
  90. const auto call_constructor = call_construction {};
  91. template <typename... Functions>
  92. struct constructor_wrapper {
  93. std::tuple<Functions...> functions;
  94. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, constructor_wrapper>> = meta::enabler>
  95. constructor_wrapper(Arg&& arg, Args&&... args) : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  96. }
  97. };
  98. template <typename... Functions>
  99. inline auto initializers(Functions&&... functions) {
  100. return constructor_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  101. }
  102. template <typename... Functions>
  103. struct factory_wrapper {
  104. std::tuple<Functions...> functions;
  105. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, factory_wrapper>> = meta::enabler>
  106. factory_wrapper(Arg&& arg, Args&&... args) : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  107. }
  108. };
  109. template <typename... Functions>
  110. inline auto factories(Functions&&... functions) {
  111. return factory_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  112. }
  113. template <typename Function>
  114. struct destructor_wrapper {
  115. Function fx;
  116. destructor_wrapper(Function f) : fx(std::move(f)) {
  117. }
  118. };
  119. template <>
  120. struct destructor_wrapper<void> { };
  121. const destructor_wrapper<void> default_destructor {};
  122. template <typename Fx>
  123. inline auto destructor(Fx&& fx) {
  124. return destructor_wrapper<std::decay_t<Fx>>(std::forward<Fx>(fx));
  125. }
  126. } // namespace sol
  127. #endif // SOL_RAII_HPP