Upstream | cc2ee17 | 1970-01-12 13:46:40 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * @file comma_list.h |
| 3 | * Container holding items from a list of comma separated items |
| 4 | * |
| 5 | * @remark Copyright 2003 OProfile authors |
| 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author Philippe Elie |
| 9 | */ |
| 10 | |
| 11 | #ifndef COMMA_LIST_H |
| 12 | #define COMMA_LIST_H |
| 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "string_manip.h" |
| 18 | |
| 19 | /** |
| 20 | * hold a list of item of type T, tracking also if item has been set. |
| 21 | */ |
| 22 | template <class T> |
| 23 | class comma_list |
| 24 | { |
| 25 | public: |
| 26 | comma_list(); |
| 27 | |
| 28 | /** |
| 29 | * @param str list of comma separated item |
| 30 | * |
| 31 | * setup items array according to str parameters. Implement PP:3.17 |
| 32 | * w/o restriction on charset and with the special string all which |
| 33 | * match anything. |
| 34 | */ |
| 35 | void set(std::string const & str); |
| 36 | |
| 37 | /// return true if a specific value is held by this container |
| 38 | bool is_set() const { |
| 39 | return !is_all; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param value the value to test |
| 44 | * |
| 45 | * return true if value match one the stored value in items |
| 46 | */ |
| 47 | bool match(T const & value) const; |
| 48 | |
| 49 | private: |
| 50 | typedef T value_type; |
| 51 | typedef std::vector<value_type> container_type; |
| 52 | typedef typename container_type::const_iterator const_iterator; |
| 53 | bool is_all; |
| 54 | container_type items; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | template <class T> |
| 59 | comma_list<T>::comma_list() |
| 60 | : is_all(true) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | |
| 65 | template <class T> |
| 66 | void comma_list<T>::set(std::string const & str) |
| 67 | { |
| 68 | items.clear(); |
| 69 | |
| 70 | is_all = false; |
| 71 | |
| 72 | std::vector<std::string> result = separate_token(str, ','); |
| 73 | for (size_t i = 0 ; i < result.size() ; ++i) { |
| 74 | if (result[i] == "all") { |
| 75 | is_all = true; |
| 76 | items.clear(); |
| 77 | break; |
| 78 | } |
| 79 | items.push_back(op_lexical_cast<T>(result[i])); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | template <class T> |
| 85 | bool comma_list<T>::match(T const & value) const |
| 86 | { |
| 87 | if (is_all) |
| 88 | return true; |
| 89 | |
| 90 | const_iterator cit = items.begin(); |
| 91 | const_iterator const end = items.end(); |
| 92 | |
| 93 | for (; cit != end; ++cit) { |
| 94 | if (value == *cit) |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | #endif /* !COMMA_LIST_H */ |