libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #ifndef MOOST_GENERIC_MAP_POLICY_HPP 00029 #define MOOST_GENERIC_MAP_POLICY_HPP 00030 00031 #include <vector> 00032 #include <stdexcept> 00033 00034 namespace moost { namespace container { namespace policies { 00035 00039 template <typename TMap> 00040 class generic_map 00041 { 00042 public: 00043 00044 typedef TMap map_type; 00045 00046 virtual ~generic_map() {} 00047 00048 virtual void init(TMap& /*map*/) const 00049 {} 00050 00052 virtual void resize(TMap& /*map*/, size_t /*numKeys*/) const 00053 {} 00054 00055 virtual void swap(TMap& first, TMap& second) const 00056 { first.swap(second); } 00057 00059 virtual size_t size(const TMap& map) const 00060 { return map.size(); } 00061 00062 virtual void clear(TMap& map) const 00063 { map.clear(); } 00064 00065 template <typename TKey> 00066 bool remove(TMap& map, const TKey& key) const 00067 { 00068 typename TMap::iterator it = map.find(key); 00069 if ( it == map.end() ) 00070 return false; 00071 map.erase(it); 00072 return true; 00073 } 00074 00075 template <typename TKey> 00076 bool find(const TMap& map, const TKey& key) const 00077 { return map.find(key) != map.end(); } 00078 00079 template <typename TKey, typename TVal> 00080 TVal operator()( const TMap& map, const TKey& key ) const 00081 { 00082 typename TMap::const_iterator it = map.find(key); 00083 if ( it == map.end() ) 00084 throw std::runtime_error("generic_policy::operator(): key not found"); 00085 return get_value<TVal>(map, it); 00086 } 00087 00088 template <typename TKey, typename TVal> 00089 bool operator()( const TMap& map, const TKey& key, TVal& val ) const 00090 { 00091 typename TMap::const_iterator it = map.find(key); 00092 if ( it == map.end() ) 00093 return false; 00094 get_value(val, map, it); 00095 return true; 00096 } 00097 00098 template <typename Key, typename T> 00099 bool put( TMap& map, const Key& key, const T& val ) const 00100 { 00101 map[key] = val; 00102 return true; 00103 } 00104 00105 template <typename TKey> 00106 void get_keys( const TMap& map, std::vector<TKey>& keys ) const 00107 { 00108 typename TMap::const_iterator it; 00109 keys.resize( size(map) ); 00110 int i = 0; 00111 for ( it = map.begin(); it != map.end(); ++it, ++i) 00112 keys[i] = get_key<TKey>(map, it); 00113 } 00114 00116 // if not using maps with first and second, those need to be 00117 // overridden 00118 00119 template <typename TKey, typename TMapIterator> 00120 TKey get_key( const TMap& /*map*/, const TMapIterator& it ) const 00121 { return it->first; } 00122 00123 template <typename TVal, typename TMapIterator> 00124 TVal get_value( const TMap& /*map*/, const TMapIterator& it ) const 00125 { return it->second; } 00126 00127 template <typename TKey, typename TMapIterator> 00128 void get_key( TKey& key, const TMap& map, const TMapIterator& it ) const 00129 { key = get_key<TKey>(map, it); } 00130 00131 template <typename TVal, typename TMapIterator> 00132 void get_value( TVal& value, const TMap& map, const TMapIterator& it ) const 00133 { value = get_value<TVal>(map, it); } 00134 }; 00135 00136 // ----------------------------------------------------------------------------- 00137 00138 template <typename TKey, typename TVal, typename TMap> 00139 struct map_policy_selector 00140 { 00141 // will return generic_map 00142 typedef generic_map<TMap> policy_type; 00143 }; 00144 00145 00146 }}} 00147 00148 #endif // MOOST_GENERIC_MAP_POLICY_HPP