add ScalarTypeID template

Define a compile time mapping from
type to ScalarType enum value.
This commit is contained in:
Michael Davidsaver
2013-07-08 09:54:13 -04:00
parent 2cb07a8490
commit d319e2ed7b
2 changed files with 55 additions and 14 deletions

View File

@@ -558,6 +558,19 @@ private:
*/
extern FieldCreatePtr getFieldCreate();
/** Define a compile time mapping from
* type to enum value.
@code
ScalarType code = (ScalarType)ScalarTypeID<int8>::value;
assert(code==pvByte);
@endcode
*
* For unspecified types this evaluates to an invalid ScalarType
* value (eg -1).
*/
template<typename T>
struct ScalarTypeID { enum {value=-1}; };
/**
* Static mapping from ScalarType enum to value type.
@code
@@ -567,19 +580,23 @@ extern FieldCreatePtr getFieldCreate();
template<ScalarType ID>
struct ScalarTypeTraits {};
#define OP(ENUM, TYPE) template<> struct ScalarTypeTraits<ENUM> {typedef TYPE type;}
OP(pvBoolean, boolean);
OP(pvByte, int8);
OP(pvShort, int16);
OP(pvInt, int32);
OP(pvLong, int64);
OP(pvUByte, uint8);
OP(pvUShort, uint16);
OP(pvUInt, uint32);
OP(pvULong, uint64);
OP(pvFloat, float);
OP(pvDouble, double);
OP(pvString, String);
#define OP(ENUM, TYPE) \
template<> struct ScalarTypeTraits<ENUM> {typedef TYPE type;}; \
template<> struct ScalarTypeID<TYPE> { enum {value=ENUM}; }; \
template<> struct ScalarTypeID<const TYPE> { enum {value=ENUM}; };
OP(pvBoolean, boolean)
OP(pvByte, int8)
OP(pvShort, int16)
OP(pvInt, int32)
OP(pvLong, int64)
OP(pvUByte, uint8)
OP(pvUShort, uint16)
OP(pvUInt, uint32)
OP(pvULong, uint64)
OP(pvFloat, float)
OP(pvDouble, double)
OP(pvString, String)
#undef OP
}}

View File

@@ -174,9 +174,32 @@ static void testError()
testOk1(fieldCreate->createStructure(names,fields).get()!=NULL);
}
static void testMapping()
{
#define OP(TYPE, ENUM) \
testOk1(typeid(ScalarTypeTraits<ENUM>::type)==typeid(TYPE)); \
testOk1(ENUM==(ScalarType)ScalarTypeID<TYPE>::value); \
testOk1(ENUM==(ScalarType)ScalarTypeID<const TYPE>::value);
OP(boolean, pvBoolean)
OP(int8, pvByte)
OP(int16, pvShort)
OP(int32, pvInt)
OP(int64, pvLong)
OP(uint8, pvUByte)
OP(uint16, pvUShort)
OP(uint32, pvUInt)
OP(uint64, pvULong)
OP(float, pvFloat)
OP(double, pvDouble)
OP(String, pvString)
#undef OP
testOk1((ScalarType)ScalarTypeID<PVField>::value==(ScalarType)-1);
}
MAIN(testIntrospect)
{
testPlan(124);
testPlan(161);
fieldCreate = getFieldCreate();
pvDataCreate = getPVDataCreate();
standardField = getStandardField();
@@ -184,5 +207,6 @@ MAIN(testIntrospect)
testScalarArray();
testStructure();
testError();
testMapping();
return testDone();
}