comma_list.h

Go to the documentation of this file.
00001 
00011 #ifndef COMMA_LIST_H
00012 #define COMMA_LIST_H
00013 
00014 #include <string>
00015 #include <vector>
00016 
00017 #include "string_manip.h"
00018 
00022 template <class T>
00023 class comma_list
00024 {
00025 public:
00026     comma_list();
00027 
00035     void set(std::string const & str);
00036 
00038     bool is_set() const {
00039         return !is_all;
00040     }
00041 
00047     bool match(T const & value) const;
00048 
00049 private:
00050     typedef T value_type;
00051     typedef std::vector<value_type> container_type;
00052     typedef typename container_type::const_iterator const_iterator;
00053     bool is_all;
00054     container_type items;
00055 };
00056 
00057 
00058 template <class T>
00059 comma_list<T>::comma_list()
00060     : is_all(true)
00061 {
00062 }
00063 
00064 
00065 template <class T>
00066 void comma_list<T>::set(std::string const & str)
00067 {
00068     items.clear();
00069 
00070     is_all = false;
00071 
00072     std::vector<std::string> result = separate_token(str, ',');
00073     for (size_t i = 0 ; i < result.size() ; ++i) {
00074         if (result[i] == "all") {
00075             is_all = true;
00076             items.clear();
00077             break;
00078         }
00079         items.push_back(op_lexical_cast<T>(result[i]));
00080     }
00081 }
00082 
00083 
00084 template <class T>
00085 bool comma_list<T>::match(T const & value) const
00086 {
00087     if (is_all)
00088         return true;
00089 
00090     const_iterator cit = items.begin();
00091     const_iterator const end = items.end();
00092 
00093     for (; cit != end; ++cit) {
00094         if (value == *cit)
00095             return true;
00096     }
00097 
00098     return false;
00099 }
00100 
00101 
00102 #endif /* !COMMA_LIST_H */

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1