00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef IXE_PatternMap_H
00026 #define IXE_PatternMap_H
00027
00028
00029 #include "platform.h"
00030
00031
00032 #include <algorithm>
00033 #include <fnmatch.h>
00034 #include <map>
00035
00036
00037 #include "text/less.h"
00038
00039 namespace IXE {
00040
00061 template <class T>
00062 class PatternMap : public std::map<char const*, T>
00063 {
00064 public:
00065 typedef std::map<char const*, T> Map;
00066 typedef typename Map::key_type key_type;
00067 typedef typename Map::value_type value_type;
00068 typedef typename Map::iterator iterator;
00069 typedef typename Map::const_iterator const_iterator;
00070
00071
00072
00073
00074 iterator find(char const* name) {
00075 return std::find_if(this->begin(), this->end(), pattern_match(name));
00076 }
00077 const_iterator find(char const* name) const {
00078 return std::find_if(this->begin(), this->end(), pattern_match(name));
00079 }
00080
00081 bool matches(char const* name) const {
00082 return find(name) != this->end();
00083 }
00084
00085 void insert(char const* pattern, T const& t) {
00086 (*this)[pattern] = t;
00087 }
00088 void insert(value_type const& n) { Map::insert(n); }
00089 private:
00090 class pattern_match : public std::unary_function<value_type const&, bool> {
00091 public:
00092 pattern_match(char const* name) :
00093 name(name) { }
00094 bool operator ()(value_type const& map_node) const {
00095 return !::fnmatch(map_node.first, name, 0);
00096 }
00097 private:
00098 char const* const name;
00099 };
00100 };
00101
00102 }
00103
00104 #endif