17
17
18
18
/* *
19
19
* @file
20
- * This file contains definitions for working with CHIP values.
20
+ * This file contains definitions for working with CHIP Attributes
21
21
*
22
22
*/
23
23
@@ -45,38 +45,16 @@ class Attribute
45
45
46
46
public:
47
47
AttributeId_t mAttrId ;
48
- Value mValue ;
49
- Value mMin ;
50
- Value mMax ;
51
-
52
- Attribute (AttributeId_t attrId, ValueTypes type) : mDeque (this ), mAttrId (attrId), mValue (type), mMin (type), mMax (type) {}
53
- Attribute (AttributeId_t attrId, Value value) :
54
- mDeque (this ), mAttrId (attrId), mValue (value), mMin (value.mType ), mMax (value.mType )
55
- {}
56
- Attribute (AttributeId_t attrId, ValueTypes type, uint64_t min, uint64_t max) :
57
- mDeque (this ), mAttrId (attrId), mValue (type), mMin (type, min), mMax (type, max)
58
- {}
48
+
49
+ Attribute (AttributeId_t attrId) : mDeque (this ), mAttrId (attrId) {}
59
50
60
51
/* *
61
52
* @brief
62
53
* Set this attribute to a value
63
54
*
64
55
* @param value the new value that this attribute should be updated with
65
56
*/
66
- CHIP_ERROR Set (const Value & newValue)
67
- {
68
- /* We have to check the element type match in this case */
69
- if (mValue .mType != newValue.mType )
70
- {
71
- return CHIP_ERROR_INTERNAL;
72
- }
73
- if (withinRange (newValue))
74
- {
75
- mValue = newValue;
76
- return CHIP_NO_ERROR;
77
- }
78
- return CHIP_ERROR_INTERNAL;
79
- }
57
+ virtual CHIP_ERROR Set (const Value & newValue) = 0;
80
58
81
59
/* Need to define the behaviour when Value contains pointers
82
60
* to allocated data
@@ -86,32 +64,51 @@ class Attribute
86
64
* Get the value of this attribute
87
65
*
88
66
*/
89
- Value Get () { return mValue ; }
67
+ virtual Value Get (void ) = 0;
68
+ };
90
69
91
- protected:
92
- bool withinRange (const uint64_t & value) { return (value >= mMin .Int64 ) && (value <= mMax .Int64 ); }
70
+ template <typename ValueType>
71
+ class AttributeSimple : public Attribute
72
+ {
73
+ private:
74
+ ValueType mValue ;
93
75
94
- bool withinRange (const Value value)
76
+ public:
77
+ AttributeSimple (AttributeId_t attrId) : Attribute(attrId) {}
78
+ AttributeSimple (AttributeId_t attrId, ValueType value) : Attribute(attrId), mValue (value) {}
79
+
80
+ CHIP_ERROR Set (const Value & newValue) { return ValueToType (newValue, mValue ); }
81
+
82
+ Value Get (void ) { return TypeToValue (mValue ); }
83
+ };
84
+
85
+ template <typename ValueType, ValueType min, ValueType max>
86
+ class AttributeWithRange : public Attribute
87
+ {
88
+ private:
89
+ ValueType mValue ;
90
+ const ValueType mMin = min;
91
+ const ValueType mMax = max;
92
+
93
+ public:
94
+ AttributeWithRange (AttributeId_t attrId) : Attribute(attrId) {}
95
+ AttributeWithRange (AttributeId_t attrId, ValueType value) : Attribute(attrId), mValue (value) {}
96
+
97
+ CHIP_ERROR Set (const Value & newValue)
95
98
{
96
- switch (mValue .mType )
99
+ ValueType tmp;
100
+ if (ValueToType (newValue, tmp) == CHIP_NO_ERROR)
97
101
{
98
- case kCHIPValueType_Int8 :
99
- case kCHIPValueType_Int16 :
100
- case kCHIPValueType_Int32 :
101
- case kCHIPValueType_Int64 :
102
- case kCHIPValueType_UInt8 :
103
- case kCHIPValueType_UInt16 :
104
- case kCHIPValueType_UInt32 :
105
- case kCHIPValueType_UInt64 :
106
- return withinRange (value.Int64 );
107
- break ;
108
- case kCHIPValueType_Bool :
109
- return true ;
110
- default :
111
- return false ;
102
+ if ((tmp >= mMin ) && (tmp <= mMax ))
103
+ {
104
+ mValue = tmp;
105
+ return CHIP_NO_ERROR;
106
+ }
112
107
}
113
- return false ;
108
+ return CHIP_ERROR_INTERNAL ;
114
109
}
110
+
111
+ Value Get (void ) { return TypeToValue (mValue ); }
115
112
};
116
113
117
114
} // namespace DataModel
0 commit comments