system_cxx.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * This file contains helper classes for commonly used IDF types. The classes make the use of these types easier and
  8. * safer.
  9. * In particular, their usage provides greater type-safety of function arguments and "correctness by construction".
  10. */
  11. #pragma once
  12. #ifndef __cpp_exceptions
  13. #error system C++ classes only usable when C++ exceptions enabled. Enable CONFIG_COMPILER_CXX_EXCEPTIONS in Kconfig
  14. #endif
  15. #include "esp_exception.hpp"
  16. /**
  17. * This is a "Strong Value Type" base class for types in IDF C++ classes.
  18. * The idea is that subclasses completely check the contained value during construction.
  19. * After that, it's trapped and encapsulated inside and cannot be changed anymore.
  20. * Consequently, the API functions receiving a correctly implemented sub class as parameter
  21. * don't need to check it anymore. Only at API boundaries the valid value will be retrieved
  22. * with get_value().
  23. */
  24. template<typename ValueT>
  25. class StrongValue {
  26. protected:
  27. constexpr StrongValue(ValueT value_arg) : value(value_arg) { }
  28. ValueT get_value() const {
  29. return value;
  30. }
  31. private:
  32. ValueT value;
  33. };
  34. /**
  35. * This class adds comparison properties to StrongValue, but no sorting and ordering properties.
  36. */
  37. template<typename ValueT>
  38. class StrongValueComparable : public StrongValue<ValueT> {
  39. protected:
  40. constexpr StrongValueComparable(ValueT value_arg) : StrongValue<ValueT>(value_arg) { }
  41. public:
  42. using StrongValue<ValueT>::get_value;
  43. bool operator==(const StrongValueComparable<ValueT> &other_gpio) const
  44. {
  45. return get_value() == other_gpio.get_value();
  46. }
  47. bool operator!=(const StrongValueComparable<ValueT> &other_gpio) const
  48. {
  49. return get_value() != other_gpio.get_value();
  50. }
  51. };
  52. namespace idf {
  53. /**
  54. * This class adds ordering and sorting properties to StrongValue.
  55. */
  56. template<typename ValueT>
  57. class StrongValueOrdered : public StrongValueComparable<ValueT> {
  58. public:
  59. StrongValueOrdered(ValueT value) : StrongValueComparable<ValueT>(value) { }
  60. using StrongValueComparable<ValueT>::get_value;
  61. bool operator>(const StrongValueOrdered<ValueT> &other) const
  62. {
  63. return get_value() > other.get_value();
  64. }
  65. bool operator<(const StrongValueOrdered<ValueT> &other) const
  66. {
  67. return get_value() < other.get_value();
  68. }
  69. bool operator>=(const StrongValueOrdered<ValueT> &other) const
  70. {
  71. return get_value() >= other.get_value();
  72. }
  73. bool operator<=(const StrongValueOrdered<ValueT> &other) const
  74. {
  75. return get_value() <= other.get_value();
  76. }
  77. };
  78. /**
  79. * A general frequency class to be used whereever an unbound frequency value is necessary.
  80. */
  81. class Frequency : public StrongValueOrdered<size_t> {
  82. public:
  83. explicit Frequency(size_t frequency) : StrongValueOrdered<size_t>(frequency)
  84. {
  85. if (frequency == 0) {
  86. throw ESPException(ESP_ERR_INVALID_ARG);
  87. }
  88. }
  89. Frequency(const Frequency&) = default;
  90. Frequency &operator=(const Frequency&) = default;
  91. using StrongValueOrdered<size_t>::get_value;
  92. static Frequency Hz(size_t frequency)
  93. {
  94. return Frequency(frequency);
  95. }
  96. static Frequency KHz(size_t frequency)
  97. {
  98. return Frequency(frequency * 1000);
  99. }
  100. static Frequency MHz(size_t frequency)
  101. {
  102. return Frequency(frequency * 1000 * 1000);
  103. }
  104. };
  105. /**
  106. * Queue size mainly for operating system queues.
  107. */
  108. class QueueSize {
  109. public:
  110. explicit QueueSize(size_t q_size) : queue_size(q_size) { }
  111. size_t get_size()
  112. {
  113. return queue_size;
  114. }
  115. private:
  116. size_t queue_size;
  117. };
  118. }