Changeset 46:0e3ecaa4241b in rrlib_xml


Ignore:
Timestamp:
17.07.2012 17:30:36 (7 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Making use of make-builders enum_strings mechanism when reading enum attributes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tNode.h

    r45 r46  
    637637   * If the XML node wrapped by this instance has an attribute with 
    638638   * the given name, its value is interpreted as name of an element 
    639    * in an enumeration. Therefore, a vector with the enum's names must 
    640    * be provided. The method then returns the index of the name that 
    641    * was found in the attribute. 
     639   * in an enumeration. The names are taken from make-builders enum 
     640   * to string mechanism. 
    642641   * 
    643642   * \exception tException is thrown if the requested attribute's value is not available or the enum type is not covered by enum_string 
     643   * 
     644   * \param name         The name of the attribute 
     645   * 
     646   * \returns The enum value if valid 
     647   */ 
     648  template <typename TEnum> 
     649  inline const TEnum GetEnumAttribute(const std::string &name) const 
     650  { 
     651    const std::vector<const char *> *enum_names = make_builder::GetEnumStrings<TEnum>(); 
     652    if (enum_names == NULL) 
     653    { 
     654      throw tException("Could not find enum to string lookup table"); 
     655    } 
     656    return this->GetEnumAttribute<TEnum>(name, enum_names->begin(), enum_names->end()); 
     657  } 
     658 
     659  /*! Get an XML attribute as enum 
     660   * 
     661   * If the XML node wrapped by this instance has an attribute with 
     662   * the given name, its value is interpreted as name of an element 
     663   * in an enumeration. Therefore, the begin and end iterators of a 
     664   * list containing the enum names must be provided. The method 
     665   * then returns the index of the name that was found in the 
     666   * attribute as enum value. 
     667   * 
     668   * \exception tException is thrown if the requested attribute's value is not available or not a member of given vector 
    644669   * 
    645670   * \param name         The name of the attribute 
    646671   * \param enum_names   The names of the enumeration elements 
    647672   * 
    648    * \returns The index of the matching element name 
    649    */ 
    650   template <typename TEnum> 
    651   inline const TEnum GetEnumAttribute(const std::string &name, const std::vector<std::string> &enum_names) const 
     673   * \returns The index of the matching element name as enum value 
     674   */ 
     675  template <typename TEnum, typename TIterator> 
     676  inline const TEnum GetEnumAttribute(const std::string &name, const TIterator enum_names_begin, const TIterator enum_names_end) const 
    652677  { 
    653678    const std::string value = this->GetStringAttribute(name); 
    654     std::vector<std::string>::const_iterator it = std::find(enum_names.begin(), enum_names.end(), value); 
    655     if (it == enum_names.end()) 
     679    TIterator it = std::find(enum_names_begin, enum_names_end, value); 
     680    if (it == enum_names_end) 
    656681    { 
    657682      throw tException("Invalid value for " + this->Name() + "." + name + ": `" + value + "'"); 
    658683    } 
    659     return static_cast<TEnum>(std::distance(enum_names.begin(), it)); 
     684    return static_cast<TEnum>(std::distance(enum_names_begin, it)); 
    660685  } 
    661686 
     
    674699  inline const bool GetBoolAttribute(const std::string &name) const 
    675700  { 
    676     static const std::vector<std::string> bool_names = { "false", "true" }; 
    677     return this->GetEnumAttribute<bool>(name, bool_names); 
     701    const std::vector<std::string> bool_names = { "false", "true" }; 
     702    return this->GetEnumAttribute<bool>(name, bool_names.begin(), bool_names.end()); 
    678703  } 
    679704 
Note: See TracChangeset for help on using the changeset viewer.