Botan
1.11.15
|
00001 /* 00002 * STL Utility Functions 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_STL_UTIL_H__ 00009 #define BOTAN_STL_UTIL_H__ 00010 00011 #include <vector> 00012 #include <string> 00013 #include <map> 00014 00015 namespace Botan { 00016 00017 inline std::vector<byte> to_byte_vector(const std::string& s) 00018 { 00019 return std::vector<byte>(reinterpret_cast<const byte*>(&s[0]), 00020 reinterpret_cast<const byte*>(&s[s.size()])); 00021 } 00022 00023 /* 00024 * Searching through a std::map 00025 * @param mapping the map to search 00026 * @param key is what to look for 00027 * @param null_result is the value to return if key is not in mapping 00028 * @return mapping[key] or null_result 00029 */ 00030 template<typename K, typename V> 00031 inline V search_map(const std::map<K, V>& mapping, 00032 const K& key, 00033 const V& null_result = V()) 00034 { 00035 auto i = mapping.find(key); 00036 if(i == mapping.end()) 00037 return null_result; 00038 return i->second; 00039 } 00040 00041 template<typename K, typename V, typename R> 00042 inline R search_map(const std::map<K, V>& mapping, const K& key, 00043 const R& null_result, const R& found_result) 00044 { 00045 auto i = mapping.find(key); 00046 if(i == mapping.end()) 00047 return null_result; 00048 return found_result; 00049 } 00050 00051 /* 00052 * Insert a key/value pair into a multimap 00053 */ 00054 template<typename K, typename V> 00055 void multimap_insert(std::multimap<K, V>& multimap, 00056 const K& key, const V& value) 00057 { 00058 #if defined(BOTAN_BUILD_COMPILER_IS_SUN_STUDIO) 00059 // Work around a strange bug in Sun Studio 00060 multimap.insert(std::make_pair<const K, V>(key, value)); 00061 #else 00062 multimap.insert(std::make_pair(key, value)); 00063 #endif 00064 } 00065 00066 /** 00067 * Existence check for values 00068 */ 00069 template<typename T> 00070 bool value_exists(const std::vector<T>& vec, 00071 const T& val) 00072 { 00073 for(size_t i = 0; i != vec.size(); ++i) 00074 if(vec[i] == val) 00075 return true; 00076 return false; 00077 } 00078 00079 template<typename T, typename Pred> 00080 void map_remove_if(Pred pred, T& assoc) 00081 { 00082 auto i = assoc.begin(); 00083 while(i != assoc.end()) 00084 { 00085 if(pred(i->first)) 00086 assoc.erase(i++); 00087 else 00088 i++; 00089 } 00090 } 00091 00092 } 00093 00094 #endif