|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Distributed under the 3-Clause BSD License. See accompanying |
| 4 | +// file LICENSE or https://github.com/CLIUtils/CLI11 for details. |
| 5 | + |
| 6 | +#include <initializer_list> |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +// CLI Library includes |
| 12 | +#include "CLI/StringTools.hpp" |
| 13 | + |
| 14 | +namespace CLI { |
| 15 | + |
| 16 | +class Set { |
| 17 | + protected: |
| 18 | + using storage_type = std::vector<std::string>; |
| 19 | + std::shared_ptr<storage_type> items_{std::make_shared<storage_type>()}; |
| 20 | + std::shared_ptr<bool> ignore_case_{std::make_shared<bool>(false)}; |
| 21 | + std::shared_ptr<bool> ignore_underscore_{std::make_shared<bool>(false)}; |
| 22 | + |
| 23 | + public: |
| 24 | + using iterator = storage_type::iterator; |
| 25 | + using const_iterator = storage_type::iterator; |
| 26 | + |
| 27 | + Set(std::initializer_list<std::string> values) : items_(std::make_shared<storage_type>(values)) {} |
| 28 | + |
| 29 | + /// Ignore case when matching |
| 30 | + Set *ignore_case(bool value = true) { |
| 31 | + *ignore_case_ = value; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + /// Ignore underscores when matching |
| 36 | + Set *ignore_underscore(bool value = true) { |
| 37 | + *ignore_underscore_ = value; |
| 38 | + return this; |
| 39 | + } |
| 40 | + |
| 41 | + /// Get the status of ignore case |
| 42 | + bool get_ignore_case() const { return *ignore_case_; } |
| 43 | + |
| 44 | + /// Get the status of ignore underscore |
| 45 | + bool get_ignore_underscore() const { return *ignore_underscore_; } |
| 46 | + |
| 47 | + /// Get the underlying vector (provisional) |
| 48 | + const storage_type &get_items() const { return *items_; } |
| 49 | + |
| 50 | + /// Find a value in the set, taking into account ignore settings |
| 51 | + const_iterator find(const std::string &item) const { |
| 52 | + bool ignore_case = *ignore_case_; |
| 53 | + bool ignore_underscore = *ignore_underscore_; |
| 54 | + |
| 55 | + return std::find_if( |
| 56 | + items_->begin(), items_->end(), [ignore_case, ignore_underscore, &item](std::string item_in_set) { |
| 57 | + std::string item_local = item; |
| 58 | + |
| 59 | + if(ignore_case) { |
| 60 | + item_local = detail::to_lower(item_local); |
| 61 | + item_in_set = detail::to_lower(item_in_set); |
| 62 | + } |
| 63 | + |
| 64 | + if(ignore_underscore) { |
| 65 | + item_local = detail::remove_underscore(item_local); |
| 66 | + item_in_set = detail::remove_underscore(item_in_set); |
| 67 | + } |
| 68 | + |
| 69 | + return item_local == item_in_set; |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /// Check to see if an item is in the set |
| 74 | + bool contains(const std::string &item) const { return find(item) != items_->end(); } |
| 75 | + |
| 76 | + /// Insert an item at the end of the set |
| 77 | + bool insert(const std::string &item) { |
| 78 | + if(contains(item)) { |
| 79 | + return false; |
| 80 | + } else { |
| 81 | + items_->emplace_back(item); |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Remove an item by name from the set. Returns true if the item was originally in the set. |
| 87 | + /// |
| 88 | + /// Note that this is not fast, but CLI11 sets should be mostly small and static |
| 89 | + bool discard(const std::string &item) { |
| 90 | + auto result = std::find(items_->begin(), items_->end(), item); |
| 91 | + if(result != items_->end()) { |
| 92 | + items_->erase(result); |
| 93 | + return true; |
| 94 | + } else { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// Remove an item by name from the set, throw an error if the item is not present. |
| 100 | + void remove(const std::string &item); |
| 101 | + |
| 102 | + /// Get an item by number from the set |
| 103 | + const std::string &at(size_t n) const { return items_->at(n); } |
| 104 | + |
| 105 | + /// Get an item by number from the set (non-const) |
| 106 | + std::string &at(size_t n) { return items_->at(n); } |
| 107 | + |
| 108 | + /// Get an item by number from the set |
| 109 | + const std::string &operator[](size_t n) const { return (*items_)[n]; } |
| 110 | + |
| 111 | + /// Get an item by number from the set (non-const) |
| 112 | + std::string &operator[](size_t n) { return (*items_)[n]; } |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace CLI |
0 commit comments