Skip to content

Commit

Permalink
Use enum instead of template
Browse files Browse the repository at this point in the history
  • Loading branch information
kedars committed Jul 8, 2020
1 parent 092281c commit 1076316
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 20 deletions.
36 changes: 29 additions & 7 deletions src/lib/core/CHIPAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,40 @@
namespace chip {
namespace DataModel {

template <typename ValueType, typename BaseValueType>

class CHIPAttribute
{
public:
ValueType mValue;
CHIPAttribute() {}
CHIPAttribute(BaseValueType value) : mValue(value) {}
CHIPAttribute(BaseValueType value, BaseValueType min, BaseValueType max) : mValue(value, min, max) {}
CHIPAttribute(uint16_t attrId, CHIPValueTypes type) : mAttrId(attrId), mValue(type), mMin(type), mMax(type) {}
CHIPAttribute(uint16_t attrId, CHIPValue min, CHIPValue max) : mAttrId(attrId), mValue(min.mType), mMin(min), mMax(max) {}

int Set(CHIPValue newValue)
{
if (mValue.mType != newValue.mType)
{
return FAIL;
}
if (newValue < mMin ||
newValue > mMax)
{
return FAIL;
}
mValue = newValue;
return SUCCESS;
}

CHIPValue Get()
{
return mValue;
}

private:
uint16_t mAttrId;
CHIPValue mValue;
CHIPValue mMin;
CHIPValue mMax;
};

typedef class CHIPAttribute<CHIPValue_UInt32_t, uint32_t> CHIPAttribute_UInt32_t;
typedef class CHIPAttribute<CHIPValue_Bool_t, bool> CHIPAttribute_Bool_t;

} // namespace DataModel
} // namespace chip
Expand Down
109 changes: 96 additions & 13 deletions src/lib/core/CHIPValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
namespace chip {
namespace DataModel {

/* XXX Remove later*/
#define SUCCESS 0
#define FAIL -1

enum CHIPValueTypes
{
kCHIPValueType_NotSpecified = -1,
Expand All @@ -53,25 +57,104 @@ enum CHIPValueTypes
kCHIPValueType_EndOfContainer,
};

template <enum CHIPValueTypes EnumType, typename ValueType>
class CHIPValue
{
public:
CHIPValueTypes mType;
ValueType mValue;
ValueType mMin;
ValueType mMax;

CHIPValue() : mType(EnumType) {}
CHIPValue(ValueType value) : mType(EnumType), mValue(value) {}
CHIPValue(ValueType value, ValueType min, ValueType max) : mType(EnumType),
mValue(value),
mMin(min),
mMax(max) {}
union {
uint64_t Int64;
};
CHIPValue(CHIPValueTypes type) : mType(type) {}
CHIPValue(uint32_t value) : mType(kCHIPValueType_UInt32), Int64((uint64_t)value) {}
CHIPValue(bool value) : mType(kCHIPValueType_Bool), Int64((uint64_t)value) {}

int Set(uint32_t &value)
{
if (mType != kCHIPValueType_UInt32)
return FAIL;
Int64 = (uint64_t)value;
return SUCCESS;
}

int Set(bool &value)
{
if (mType != kCHIPValueType_Bool)
return FAIL;
Int64 = (uint64_t)value;
return SUCCESS;
}

int Get(uint32_t &value)
{
if (mType != kCHIPValueType_UInt32)
return FAIL;
value = (uint32_t)Int64;
return SUCCESS;
}

int Get(bool &value)
{
if (mType != kCHIPValueType_Bool)
return FAIL;
value = (bool)Int64;
return SUCCESS;
}

bool operator <(const CHIPValue& value)
{
if (mType != value.mType)
{
return false;
}
switch (mType)
{
case kCHIPValueType_Int8:
case kCHIPValueType_Int16:
case kCHIPValueType_Int32:
case kCHIPValueType_Int64:
case kCHIPValueType_UInt8:
case kCHIPValueType_UInt16:
case kCHIPValueType_UInt32:
case kCHIPValueType_UInt64:
case kCHIPValueType_Bool:
return Int64 < value.Int64;
break;
default:
return false;
}
return false;
}

bool operator >(const CHIPValue& value)
{
if (mType != value.mType)
{
return false;
}
switch (mType)
{
case kCHIPValueType_Int8:
case kCHIPValueType_Int16:
case kCHIPValueType_Int32:
case kCHIPValueType_Int64:
case kCHIPValueType_UInt8:
case kCHIPValueType_UInt16:
case kCHIPValueType_UInt32:
case kCHIPValueType_UInt64:
case kCHIPValueType_Bool:
return Int64 > value.Int64;
break;
default:
return false;
}
return false;
}

private:
CHIPValue() {}

};

typedef class CHIPValue<kCHIPValueType_UInt32, uint32_t> CHIPValue_UInt32_t;
typedef class CHIPValue<kCHIPValueType_Bool, bool> CHIPValue_Bool_t;
} // namespace DataModel
} // namespace chip

Expand Down

0 comments on commit 1076316

Please sign in to comment.