Botan 2.19.1
Crypto and TLS for C&
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
18namespace Botan {
19
20inline std::vector<uint8_t> to_byte_vector(const std::string& s)
21 {
22 return std::vector<uint8_t>(s.cbegin(), s.cend());
23 }
24
25inline 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*/
33template<typename K, typename V>
34std::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*/
51template<typename K, typename V>
52inline 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
62template<typename K, typename V, typename R>
63inline 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*/
75template<typename K, typename V>
76void 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*/
85template<typename T>
86bool 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
95template<typename T, typename Pred>
96void 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
fe T
Definition: ge.cpp:37
Definition: alg_id.cpp:13
void map_remove_if(Pred pred, T &assoc)
Definition: stl_util.h:96
std::set< K > map_keys_as_set(const std::map< K, V > &kv)
Definition: stl_util.h:34
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
Definition: stl_util.h:52
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
std::vector< uint8_t > to_byte_vector(const std::string &s)
Definition: stl_util.h:20
bool value_exists(const std::vector< T > &vec, const T &val)
Definition: stl_util.h:86
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65