Skip to content

Commit

Permalink
safer map access
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin committed Jan 7, 2024
1 parent ae69fa8 commit d4f037a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/ucioption.cpp
Expand Up @@ -55,12 +55,15 @@ void OptionsMap::setoption(std::istringstream& is) {
sync_cout << "No such option: " << name << sync_endl;
}

Option OptionsMap::operator[](const std::string& name) const {
auto it = options_map.find(name);
return it != options_map.end() ? it->second : Option();
const Option& OptionsMap::operator[](const std::string& name) const {
// throws std::out_of_range if the option does not exist
return options_map.at(name);
}

Option& OptionsMap::operator[](const std::string& name) { return options_map[name]; }
Option& OptionsMap::operator[](const std::string& name) {
// throws std::out_of_range if the option does not exist
return options_map.at(name);
}

std::size_t OptionsMap::count(const std::string& name) const { return options_map.count(name); }

Expand Down
4 changes: 2 additions & 2 deletions src/ucioption.h
Expand Up @@ -39,8 +39,8 @@ class OptionsMap {

friend std::ostream& operator<<(std::ostream&, const OptionsMap&);

Option operator[](const std::string&) const;
Option& operator[](const std::string&);
const Option& operator[](const std::string&) const;
Option& operator[](const std::string&);

std::size_t count(const std::string&) const;

Expand Down

0 comments on commit d4f037a

Please sign in to comment.