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