system_cxx.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifndef __cpp_exceptions
  8. #error system C++ classes only usable when C++ exceptions enabled. Enable CONFIG_COMPILER_CXX_EXCEPTIONS in Kconfig
  9. #endif
  10. /**
  11. * This is a "Strong Value Type" base class for types in IDF C++ classes.
  12. * The idea is that subclasses completely check the contained value during construction.
  13. * After that, it's trapped and encapsulated inside and cannot be changed anymore.
  14. * Consequently, the API functions receiving a correctly implemented sub class as parameter
  15. * don't need to check it anymore. Only at API boundaries the valid value will be retrieved
  16. * with get_value().
  17. */
  18. template<typename ValueT>
  19. class StrongValue {
  20. protected:
  21. StrongValue(ValueT value_arg) : value(value_arg) { }
  22. ValueT get_value() const {
  23. return value;
  24. }
  25. private:
  26. ValueT value;
  27. };
  28. /**
  29. * This class adds comparison properties to StrongValue, but no sorting properties.
  30. */
  31. template<typename ValueT>
  32. class StrongValueComparable : public StrongValue<ValueT> {
  33. protected:
  34. StrongValueComparable(ValueT value_arg) : StrongValue<ValueT>(value_arg) { }
  35. using StrongValue<ValueT>::get_value;
  36. bool operator==(const StrongValueComparable<ValueT> &other_gpio) const
  37. {
  38. return get_value() == other_gpio.get_value();
  39. }
  40. bool operator!=(const StrongValueComparable<ValueT> &other_gpio) const
  41. {
  42. return get_value() != other_gpio.get_value();
  43. }
  44. };