From: <mk...@us...> - 2003-08-14 22:50:47
|
Update of /cvsroot/csp/APPLICATIONS/SimData/Include/SimData In directory sc8-pr-cvs1:/tmp/cvs-serv22413 Modified Files: Enum.h Log Message: Index: Enum.h =================================================================== RCS file: /cvsroot/csp/APPLICATIONS/SimData/Include/SimData/Enum.h,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Enum.h 14 Aug 2003 21:50:30 -0000 1.12 --- Enum.h 14 Aug 2003 22:37:09 -0000 1.13 *************** *** 26,29 **** --- 26,31 ---- * * Examples: + * + * @code // C++ example ///////////////////////////////////////////////////// *************** *** 65,68 **** --- 67,71 ---- assert(items.cherry > "orange") + * @endcode */ *************** *** 124,134 **** --- 127,143 ---- typedef std::pair<std::string, int> Element; typedef std::vector<Element> Elements; + /// value to index lookup std::map<int, int> __i2idx; + /// string to index lookup std::map<std::string, int> __s2idx; + /// elements Elements __elements; + /// Parse enum string into lookup tables. + /// + /// @see EnumerationCore(std::string const &s) void __init(std::string const &s) { std::stringstream ss(s); *************** *** 158,167 **** --- 167,186 ---- if (__elements.size() <= 0) throw EnumError("Empty Enumeration"); } + + /// Return an element by index number. Element const &getElementByIndex(int idx) const { assert(validIndex(idx)); return __elements[idx]; } + + /// Return an element token by index number. std::string getTokenByIndex(int idx) const { return getElementByIndex(idx).first; } + + /// Return an element value by index number. int getValueByIndex(int idx) const { return getElementByIndex(idx).second; } + + /// Lookup the index of the element with the specified value. + /// + /// Throws EnumIndexError if the value does not exist. int getIndexByValue(int value) const { std::map<int, int>::const_iterator iter = __i2idx.find(value); *************** *** 173,179 **** --- 192,204 ---- return iter->second; } + + /// Get the token associated with a given value. std::string getTokenByValue(int value) const { return __elements[getIndexByValue(value)].first; } + + /// Lookup the index of the element with the specified token. + /// + /// Throws EnumIndexError if the token does not exist. int getIndexByToken(std::string const &token) const { std::map<std::string, int>::const_iterator iter = __s2idx.find(token); *************** *** 183,202 **** return iter->second; } int getValueByToken(std::string const &token) const { return __elements[getIndexByToken(token)].second; } int size() const { return __elements.size(); } bool containsToken(std::string const &token) const { return __s2idx.find(token) != __s2idx.end(); } bool containsValue(int value) const { return __i2idx.find(value) != __i2idx.end(); } bool validIndex(int idx) const { return idx >= 0 && idx <= size(); } ! /// Not defined; should never be called. EnumerationCore(); ! /// Construct a new enum from a white-space-separated string. EnumerationCore(std::string const &s): Referenced() { __init(s); --- 208,253 ---- return iter->second; } + + /// Get the value associated with a given token. int getValueByToken(std::string const &token) const { return __elements[getIndexByToken(token)].second; } + + /// Return the number of elements in the enumeration. int size() const { return __elements.size(); } + + /// Test if this enumeration contains a given token. bool containsToken(std::string const &token) const { return __s2idx.find(token) != __s2idx.end(); } + + /// Test if this enumeration contains a given value. bool containsValue(int value) const { return __i2idx.find(value) != __i2idx.end(); } + + /// Test if an index is valid. bool validIndex(int idx) const { return idx >= 0 && idx <= size(); } ! ! /// Private default constructor. ! /// ! /// This method is not defined and should never be called. EnumerationCore(); ! ! /// Construct a new enum from a string. ! /// ! /// The enumeration tokens are must be separated by ! /// white-space and can have an optional value assignment. ! /// Tokens without explicit values will be assigned ! /// sequential values following the last specified ! /// value. The initial default value is zero. ! /// ! /// Examples: ! /// @code ! /// "zero one two three" ! /// "one=1 two three ten=10 eleven" ! /// @endcode EnumerationCore(std::string const &s): Referenced() { __init(s); *************** *** 212,215 **** --- 263,267 ---- * static Enumeration as the template argument. For example: * + * @code * class Foo { * public: *************** *** 218,226 **** --- 270,281 ---- * ... * }; + * @endcode * * When defining the Enumeration, specify the items in order * as a white-space separated string, as in: * + * @code * Enumeration Foo::MyEnumeration("apple orange cherry"); + * @endcode */ class Enumeration { *************** *** 251,254 **** --- 306,310 ---- } + /// Return the index corresponding to a given token. int getIndexByToken(std::string const &token) const { assert(__core.valid()); *************** *** 256,259 **** --- 312,316 ---- } + /// Return the index corresponding to a given value. int getIndexByValue(int value) const { assert(__core.valid()); *************** *** 261,264 **** --- 318,322 ---- } + /// Return the token corresponding to a given index. std::string getTokenByIndex(int idx) const { assert(__core.valid()); *************** *** 266,269 **** --- 324,328 ---- } + /// Return the value corresponding to a given index. int getValueByIndex(int idx) const { assert(__core.valid()); *************** *** 271,275 **** } ! inline const std::vector<EnumLink> __range(int idx1, int idx2) const; --- 330,337 ---- } ! /// Return a list of enums in a range of indices. ! /// ! /// @param idx1 The lower index (inclusive). ! /// @param idx2 The upper index (inclusive). inline const std::vector<EnumLink> __range(int idx1, int idx2) const; *************** *** 278,281 **** --- 340,344 ---- } + /// Construct a new EnumLink bound to this Enumeration. inline EnumLink makeEnum(int idx) const; *************** *** 356,360 **** --- 419,426 ---- } + /// Return the last enum in the Enumeration set. inline EnumLink last() const; + + /// Return the first enum in the Enumeration set. inline EnumLink first() const; *************** *** 401,405 **** Enumeration __E; protected: ! /// integer index of the enum int _idx; --- 467,471 ---- Enumeration __E; protected: ! /// the index of the enum int _idx; *************** *** 410,419 **** --- 476,488 ---- EnumLink(): __E(), _idx(0) {} + /// Get the enumeration index of the enum. int getIndex() const { return _idx; } + /// Construct a new EnumLink bound to a given Enumeration. EnumLink(int idx, Enumeration const &E): __E(E), _idx(idx) { E.__checkIndex(idx); } + /// Construct a new EnumLink bound to the current Enumeration. EnumLink makeEnum(int idx) const { return EnumLink(idx, __E); *************** *** 429,433 **** } ! // create a new EnumLink bound to an existing Enumeration. EnumLink(Enumeration const &E, std::string const &token): __E(E), _idx(0) { _idx = __E.getIndexByToken(token); --- 498,502 ---- } ! // Create a new EnumLink bound to an existing Enumeration. EnumLink(Enumeration const &E, std::string const &token): __E(E), _idx(0) { _idx = __E.getIndexByToken(token); |