Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

stl_util.h
Go to the documentation of this file.
1 /*
2 * STL Utility Functions
3 * (C) 1999-2007 Jack Lloyd
4 * (C) 2015 Simon Warta (Kullo GmbH)
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9 #ifndef BOTAN_STL_UTIL_H__
10 #define BOTAN_STL_UTIL_H__
11
12 #include <vector>
13 #include <string>
14 #include <map>
15 #include <set>
16 #include <botan/secmem.h>
17
18 namespace Botan {
19
20 inline std::vector<uint8_t> to_byte_vector(const std::string& s)
21 {
22 return std::vector<uint8_t>(s.cbegin(), s.cend());
23 }
24
25 inline std::string to_string(const secure_vector<uint8_t> &bytes)
26 {
27 return std::string(bytes.cbegin(), bytes.cend());
28 }
29
30 /**
31 * Return the keys of a map as a std::set
32 */
33 template<typename K, typename V>
34 std::set<K> map_keys_as_set(const std::map<K, V>& kv)
35 {
36 std::set<K> s;
37 for(auto&& i : kv)
38 {
39 s.insert(i.first);
40 }
41 return s;
42 }
43
44 /*
45 * Searching through a std::map
46 * @param mapping the map to search
47 * @param key is what to look for
48 * @param null_result is the value to return if key is not in mapping
49 * @return mapping[key] or null_result
50 */
51 template<typename K, typename V>
52 inline V search_map(const std::map<K, V>& mapping,
53 const K& key,
54 const V& null_result = V())
55 {
56 auto i = mapping.find(key);
57 if(i == mapping.end())
58 return null_result;
59 return i->second;
60 }
61
62 template<typename K, typename V, typename R>
63 inline R search_map(const std::map<K, V>& mapping, const K& key,
64 const R& null_result, const R& found_result)
65 {
66 auto i = mapping.find(key);
67 if(i == mapping.end())
68 return null_result;
69 return found_result;
70 }
71
72 /*
73 * Insert a key/value pair into a multimap
74 */
75 template<typename K, typename V>
76 void multimap_insert(std::multimap<K, V>& multimap,
77 const K& key, const V& value)
78 {
79 multimap.insert(std::make_pair(key, value));
80 }
81
82 /**
83 * Existence check for values
84 */
85 template<typename T>
86 bool value_exists(const std::vector<T>& vec,
87 const T& val)
88 {
89 for(size_t i = 0; i != vec.size(); ++i)
90 if(vec[i] == val)
91 return true;
92 return false;
93 }
94
95 template<typename T, typename Pred>
96 void map_remove_if(Pred pred, T& assoc)
97 {
98 auto i = assoc.begin();
99 while(i != assoc.end())
100 {
101 if(pred(i->first))
102 assoc.erase(i++);
103 else
104 i++;
105 }
106 }
107
108 }
109
110 #endif
Botan::search_map
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
Definition: stl_util.h:52
Botan::map_keys_as_set
std::set< K > map_keys_as_set(const std::map< K, V > &kv)
Definition: stl_util.h:34
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan
Definition: alg_id.cpp:13
Botan::to_byte_vector
std::vector< uint8_t > to_byte_vector(const std::string &s)
Definition: stl_util.h:20
Botan::map_remove_if
void map_remove_if(Pred pred, T &assoc)
Definition: stl_util.h:96
Botan::value_exists
bool value_exists(const std::vector< T > &vec, const T &val)
Definition: stl_util.h:86
Botan::to_string
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25
Botan::multimap_insert
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76