A C++ toolbox repo until the pair uTL/dTL arives
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.
 
 
 

108 lines
3.5 KiB

  1. /*!
  2. * \file utils/shared.h
  3. * \brief
  4. * A CRTP base class to provide acquire/release functionality for shared resources
  5. * without handle pointers.
  6. *
  7. * \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  8. *
  9. * <dl class=\"section copyright\"><dt>License</dt><dd>
  10. * The MIT License (MIT)
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in all
  20. * copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. * </dd></dl>
  30. */
  31. #ifndef TBX_UTILS_SHARED_H_
  32. #define TBX_UTILS_SHARED_H_
  33. #include <core/core.h>
  34. #include <core/crtp.h>
  35. #include <utility>
  36. namespace tbx {
  37. /*!
  38. * A CRTP base class to provide acquire/release functionality for shared resources
  39. * without handle pointers.
  40. *
  41. * \example
  42. * \code
  43. * class GPIOClock : public shared<GPIOClock> {
  44. * friend shared<GPIOClock>;
  45. * void acquire_impl() { // HAL enable gpio clock }
  46. * void release_impl() { // HAL disable gpio clock }
  47. * };
  48. * GPIOClock clk;
  49. * class Pin {
  50. * Pin() {
  51. * clk.acquire();
  52. * // init pin
  53. * }
  54. * ~Pin() {
  55. * // de-init pin
  56. * clk.release();
  57. * }
  58. * };
  59. * \endcode
  60. *
  61. * \tparam Impl_t The derived class type
  62. */
  63. template <typename Impl_t>
  64. class shared {
  65. _CRTP_IMPL(Impl_t);
  66. int count {}; //!< acquisition counter
  67. protected:
  68. shared() noexcept = default;
  69. shared(const shared&) = delete; //!< No copies
  70. shared operator=(const shared&) = delete; //!< No copies
  71. public:
  72. /*!
  73. * Acquires the recourse. If it is the first call to acquire the resource we actually acquire it.
  74. * Otherwise we just keep track of how many acquisition have made.
  75. *
  76. * \tparam Ts The types of possible arguments
  77. * \param args Possible arguments to pass to acquire_impl() of derived class
  78. */
  79. template <typename ...Ts>
  80. void acquire (Ts&&... args) {
  81. if (!count++) impl().acquire_impl(std::forward<Ts>(args)...);
  82. }
  83. /*!
  84. * Release the recourse. On every call we decrease the count of acquisitions. If we reach zero
  85. * we actually release the resource.
  86. *
  87. * \tparam Ts The types of possible arguments
  88. * \param args Possible arguments to pass to release_impl() of derived class
  89. */
  90. template <typename ...Ts>
  91. void release (Ts&&... args) noexcept {
  92. if (--count <= 0) {
  93. impl().release_impl(std::forward<Ts>(args)...);
  94. count =0;
  95. }
  96. }
  97. };
  98. }
  99. #endif /* TBX_UTILS_SHARED_H_ */