How To Use EnumWrapper

The EnumWrapper is a templated class that allows enumeration values to be used safely.

The EnumWrapper class contains both the enumerated value being wrapped and a boolean that indicates whether the contained enumerated value is valid and can be used. The following examples show usage of the class:

 enum TestEnumeration { ENUM_VALUE1, ENUM_VALUE2, ENUM_VALUE2 };
 EnumWrapper<TestEnumeration> test1;
 test1.isValid(); //this will return false
 //The following line is bad, it will convert EnumWrapper to TestEnumeration
 //but the enumVal1 value will be undefined since the EnumWrapper.isValid()
 //method returns false.
 TestEnumeration enumVal1 = test1;

 test1 < ENUM_VALUE1; //false because test1 is invalid
 ENUM_VALUE1 < test1; //true because test1 is invalid

 test1 == ENUM_VALUE1; //false because test1 is invalid
 ENUM_VALUE1 == test1; //false because test1 is invalid

 EnumWrapper<TestEnumeration> test2(ENUM_VALUE1);
 test2.isValid(); //this will return true
 TestEnumeration enumVal2 = test2; //enumVal2 == ENUM_VALUE1;

 EnumWrapper<TestEnumeration> test3(test2);
 TestEnumeration enumVal3 = test3; //enumVal3 == ENUM_VALUE1;

 EnumWrapper<TestEnumeration> test4;
 EnumWrapper<TestEnumeration> test5 = ENUM_VALUE2;
 TestEnumeration enumVal4 = test5; //enumVal4 == ENUM_VALUE2;
 test4.isValid(); //this will return false
 test4 = test5;
 test4.isValid(); //this will return true
 TestEnumeration enumVal5 = test4; //enumVal5 = ENUM_VALUE2;

 //It is generally recommended to create a typedef for your specific
 //usage of EnumWrapper, as shown below

 typedef EnumWrapper<TestEnumeration> TestEnum;

 //You can then do the following:

 bool testEnumValue(TestEnum val)
 {
    if (!val.isValid())
    {
       return false;
    }
    if ((val == ENUM_VALUE1) || (val == ENUM_VALUE3))
    {
       return true;
    }
    return false;
 }

 TestEnum getValue()
 {
    return ENUM_VALUE2;
 }

 TestEnum getInvalidValue()
 {
    TestEnum retVal;
    return retVal;
 }

 testEnumValue(ENUM_VALUE1); //the function returns true
 testEnumValue(ENUM_VALUE2); //the function returns false
 TestEnum test6 = ENUM_VALUE3;
 testEnumValue(test6); //the function returns true
 TestEnum test7;
 testEnumValue(test7); //the function returns false because test7.isValid() returns false

 TestEnum test8 = getValue();
 (test8 == ENUM_VALUE2); //evaluates to true
 test8.isValid(); //returns true

 TestEnum test9 = getInvalidValue();
 test9.isValid(); //returns false
 TestEnumeration enumVal6 = test9; //DON'T DO THIS.  The value of enumVal6 is undefined.

Software Development Kit - Opticks 4.9.0 Build 16218