Changeset 49:f6a48d9316b0 in rrlib_xml


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

Making use of different encodings in enum_strings to implement safe enum interpretation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tNode.h

    r47 r49  
    633633  } 
    634634 
    635   /*! Get an XML attribute as enum 
     635  /*! Get an XML attribute as enum (safe variant) 
    636636   * 
    637637   * If the XML node wrapped by this instance has an attribute with 
     
    640640   * to string mechanism. 
    641641   * 
    642    * \exception tException is thrown if the requested attribute's value is not available or the enum type is not covered by enum_string 
     642   * \exception tException is thrown if the requested attribute's value is not available 
     643   * \exception std::runtime_error is thrown if attribute's value can not be resolved into an enum value 
    643644   * 
    644645   * \param name         The name of the attribute 
     
    649650  inline const TEnum GetEnumAttribute(const std::string &name) const 
    650651  { 
    651     const make_builder::tEnumStrings &enum_strings(make_builder::GetEnumStrings<TEnum>()); 
    652     return this->GetEnumAttribute<TEnum>(name, enum_strings.strings, enum_strings.strings + enum_strings.size); 
    653   } 
    654  
    655   /*! Get an XML attribute as enum 
     652    return make_builder::GetEnumValueFromString<TEnum>(this->GetStringAttribute(name), make_builder::tEnumStringsFormat::LOWER); 
     653  } 
     654 
     655  /*! Get an XML attribute as enum (using an explicit list of strings) 
    656656   * 
    657657   * If the XML node wrapped by this instance has an attribute with 
     
    664664   * \exception tException is thrown if the requested attribute's value is not available or not a member of given vector 
    665665   * 
    666    * \param name         The name of the attribute 
    667    * \param enum_names   The names of the enumeration elements 
     666   * \param name               The name of the attribute 
     667   * \param enum_names_begin   Begin of possible enum strings 
     668   * \param enum_names_end     End of possible enum strings 
    668669   * 
    669670   * \returns The index of the matching element name as enum value 
    670671   */ 
    671   template <typename TEnum, typename TIterator> 
    672   inline const TEnum GetEnumAttribute(const std::string &name, const TIterator enum_names_begin, const TIterator enum_names_end) const 
     672  template <typename TIterator> 
     673  inline typename std::iterator_traits<TIterator>::difference_type GetEnumAttribute(const std::string &name, const TIterator enum_names_begin, const TIterator enum_names_end) const 
    673674  { 
    674675    const std::string value = this->GetStringAttribute(name); 
     
    678679      throw tException("Invalid value for " + this->Name() + "." + name + ": `" + value + "'"); 
    679680    } 
    680     return static_cast<TEnum>(std::distance(enum_names_begin, it)); 
     681    return std::distance(enum_names_begin, it); 
    681682  } 
    682683 
     
    695696  inline const bool GetBoolAttribute(const std::string &name) const 
    696697  { 
    697     const std::vector<std::string> bool_names = { "false", "true" }; 
    698     return this->GetEnumAttribute<bool>(name, bool_names.begin(), bool_names.end()); 
     698    static const std::vector<std::string> bool_names = { "false", "true" }; 
     699    return this->GetEnumAttribute(name, bool_names.begin(), bool_names.end()); 
    699700  } 
    700701 
Note: See TracChangeset for help on using the changeset viewer.