Micro template library A library for building device drivers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

107 lignes
3.5 KiB

  1. /*!
  2. * \file utility/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 utl_utility_shared_h__
  32. #define utl_utility_shared_h__
  33. #include <utl/core/impl.h>
  34. #include <utility>
  35. namespace utl {
  36. /*!
  37. * A CRTP base class to provide acquire/release functionality for shared resources
  38. * without handle pointers.
  39. *
  40. * \example
  41. * \code
  42. * class GPIOClock : public shared<GPIOClock> {
  43. * friend shared<GPIOClock>;
  44. * void acquire_impl() { // HAL enable gpio clock }
  45. * void release_impl() { // HAL disable gpio clock }
  46. * };
  47. * GPIOClock clk;
  48. * class Pin {
  49. * Pin() {
  50. * clk.acquire();
  51. * // init pin
  52. * }
  53. * ~Pin() {
  54. * // de-init pin
  55. * clk.release();
  56. * }
  57. * };
  58. * \endcode
  59. *
  60. * \tparam Impl_t The derived class type
  61. */
  62. template <typename Impl_t>
  63. class shared {
  64. _CRTP_IMPL(Impl_t);
  65. int count {}; //!< acquisition counter
  66. protected:
  67. shared() noexcept = default;
  68. shared(const shared&) = delete; //!< No copies
  69. shared operator=(const shared&) = delete; //!< No copies
  70. public:
  71. /*!
  72. * Acquires the recourse. If it is the first call to acquire the resource we actually acquire it.
  73. * Otherwise we just keep track of how many acquisition have made.
  74. *
  75. * \tparam Ts The types of possible arguments
  76. * \param args Possible arguments to pass to acquire_impl() of derived class
  77. */
  78. template <typename ...Ts>
  79. void acquire (Ts&&... args) {
  80. if (!count++) impl().acquire_impl(std::forward<Ts>(args)...);
  81. }
  82. /*!
  83. * Release the recourse. On every call we decrease the count of acquisitions. If we reach zero
  84. * we actually release the resource.
  85. *
  86. * \tparam Ts The types of possible arguments
  87. * \param args Possible arguments to pass to release_impl() of derived class
  88. */
  89. template <typename ...Ts>
  90. void release (Ts&&... args) noexcept {
  91. if (--count <= 0) {
  92. impl().release_impl(std::forward<Ts>(args)...);
  93. count =0;
  94. }
  95. }
  96. };
  97. } // namespace utl;
  98. #endif /* utl_utility_shared_h__ */