Caret7:Development/EnumeratedTypes

From Van Essen Lab

Jump to: navigation, search

Enumerated Types

Java's enumerated types, which are essentially a class with a private constructor were found to be very useful. In particular, one could add attributes that were associated with each enumerated value. The goal is to emulate this behavior.

Place each enumerated type in its own class. Add static methods that convert to and from an enumerated value and its text. Other static methods can be added for other information associated with each enumerated value. Name these static methods toName(), fromName() etc. Each of the fromXXX() methods should have a second parameter that is a pointer to a boolean value. If this pointer is not NULL, the fromXXX() method should set the value of the boolean to indicate that the value passed in was successfully associated with an enumerated value.

The enumerated values are placed in an enum named Enum.

Example Header

/**
 * GIFTI Endian Types.
 */
class GiftiEndian {

public:
    /**  GIFTI Endian Types. */
    enum Enum {
        /** Data is in Big Endian byte order   */
        ENDIAN_BIG,
        /** Data is in Little Endian byte order   */
        ENDIAN_LITTLE
    };


    ~GiftiEndian();
    static std::string toName(Enum e);

    static Enum fromName(const std::string& s, bool* isValidOut);

    static std::string toGiftiName(Enum e);

    static Enum fromGiftiName(const std::string& s, bool* isValidOut);

private:
    GiftiEndian(const Enum e, const int32_t integerCode, const std::string& name, const std::string& giftiName);

    static const GiftiEndian* findData(const Enum e);

    static std::vector<GiftiEndian> enumData;

    static void initialize();

    static bool initializedFlag;

    Enum e;

    int32_t integerCode;

    std::string name;

    std::string giftiName;


};

Example Implementation

/**
 * Constructor.
 *
 * @param e
 *    An enumerated value.
 * @param name
 *    Name of enumberated value.
 */
GiftiEndian::GiftiEndian(
                   const Enum e,
                   const int32_t integerCode,
                   const std::string& name,
                   const std::string& giftiName)
{
    this->e = e;
    this->integerCode = integerCode;
    this->name = name;
    this->giftiName = giftiName;
}

/**
 * Destructor.
 */
GiftiEndian::~GiftiEndian()
{
}

void
GiftiEndian::initialize()
{
    if (initializedFlag) {
        return;
    }
    initializedFlag = true;

    enumData.push_back(GiftiEndian(ENDIAN_BIG, 0, "ENDIAN_BIG", "BigEndian"));
    enumData.push_back(GiftiEndian(ENDIAN_LITTLE, 1, "ENDIAN_LITTLE", "LittleEndian"));
}

/**
 * Find the data for and enumerated value.
 * @param e
 *     The enumerated value.
 * @return Pointer to data for this enumerated type
 * or NULL if no data for type or if type is invalid.
 */
const GiftiEndian*
GiftiEndian::findData(const Enum e)
{
    initialize();

    size_t num = enumData.size();
    for (size_t i = 0; i < num; i++) {
        const GiftiEndian* d = &enumData[i];
        if (d->e == e) {
            return d;
        }
    }

    return NULL;
}

/**
 * Get a string representation of the enumerated type.
 * @param e 
 *     Enumerated value.
 * @return 
 *     String representing enumerated value.
 */
std::string 
GiftiEndian::toName(Enum e) {
    initialize();
    
    const GiftiEndian* gaio = findData(e);
    return gaio->name;
}

/**
 * Get an enumerated value corresponding to its name.
 * @param s 
 *     Name of enumerated value.
 * @param isValidOut 
 *     If not NULL, it is set indicating that a
 *     enum value exists for the input name.
 * @return 
 *     Enumerated value.
 */
GiftiEndian::Enum 
GiftiEndian::fromName(const std::string& s, bool* isValidOut)
{
    initialize();
    
    bool validFlag = false;
    Enum e;
    
    for (std::vector<GiftiEndian>::iterator iter = enumData.begin();
         iter != enumData.end();
         iter++) {
        const GiftiEndian& d = *iter;
        if (d.name == s) {
            e = d.e;
            validFlag = true;
            break;
        }
    }
    
    if (isValidOut != 0) {
        *isValidOut = validFlag;
    }
    return e;
}

/**
 * Get a string representation of the enumerated type.
 * @param e 
 *     Enumerated value.
 * @return 
 *     String representing enumerated value.
 */
std::string 
GiftiEndian::toGiftiName(Enum e) {
    initialize();
    
    const GiftiEndian* gaio = findData(e);
    return gaio->giftiName;
}

/**
 * Get an enumerated value corresponding to its name.
 * @param s 
 *     Name of enumerated value.
 * @param isValidOut 
 *     If not NULL, it is set indicating that a
 *     enum value exists for the input name.
 * @return 
 *     Enumerated value.
 */
GiftiEndian::Enum 
GiftiEndian::fromGiftiName(const std::string& s, bool* isValidOut)
{
    initialize();
    
    bool validFlag = false;
    Enum e;
    
    for (std::vector<GiftiEndian>::iterator iter = enumData.begin();
         iter != enumData.end();
         iter++) {
        const GiftiEndian& d = *iter;
        if (d.giftiName == s) {
            e = d.e;
            validFlag = true;
            break;
        }
    }
    
    if (isValidOut != 0) {
        *isValidOut = validFlag;
    }
    return e;
}

Personal tools
Sums Database