Botan: src/utils/stl_util.h Source File

Botan 1.10.16

stl_util.h
Go to the documentation of this file.
1 /*
2 * STL Utility Functions
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_STL_UTIL_H__
9 #define BOTAN_STL_UTIL_H__
10
11 #include <map>
12
13 namespace Botan {
14
15 /**
16 * Copy-on-Predicate Algorithm
17 * @param current the first iterator value
18 * @param end the final iterator value
19 * @param dest an output iterator
20 * @param copy_p the predicate
21 */
22 template<typename InputIterator, typename OutputIterator, typename Predicate>
23 OutputIterator copy_if(InputIterator current, InputIterator end,
24 OutputIterator dest, Predicate copy_p)
25 {
26 while(current != end)
27 {
28 if(copy_p(*current))
29 *dest++ = *current;
30 ++current;
31 }
32 return dest;
33 }
34
35 /**
36 * Searching through a std::map
37 * @param mapping the map to search
38 * @param key is what to look for
39 * @param null_result is the value to return if key is not in mapping
40 * @return mapping[key] or null_result
41 */
42 template<typename K, typename V>
43 inline V search_map(const std::map<K, V>& mapping,
44 const K& key,
45 const V& null_result = V())
46 {
47 typename std::map<K, V>::const_iterator i = mapping.find(key);
48 if(i == mapping.end())
49 return null_result;
50 return i->second;
51 }
52
53 /**
54 * Function adaptor for delete operation
55 */
56 template<class T>
57 class del_fun : public std::unary_function<T, void>
58 {
59 public:
60 void operator()(T* ptr) { delete ptr; }
61 };
62
63 /**
64 * Delete the second half of a pair of objects
65 */
66 template<typename Pair>
67 void delete2nd(Pair& pair)
68 {
69 delete pair.second;
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 #if defined(BOTAN_BUILD_COMPILER_IS_SUN_STUDIO)
80 // Work around a strange bug in Sun Studio
81 multimap.insert(std::make_pair<const K, V>(key, value));
82 #else
83 multimap.insert(std::make_pair(key, value));
84 #endif
85 }
86
87 }
88
89 #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:43
Botan::delete2nd
void delete2nd(Pair &pair)
Definition: stl_util.h:67
Botan::copy_if
OutputIterator copy_if(InputIterator current, InputIterator end, OutputIterator dest, Predicate copy_p)
Definition: stl_util.h:23
Botan
Definition: algo_base.h:14
Botan::del_fun::operator()
void operator()(T *ptr)
Definition: stl_util.h:60
Botan::del_fun
Definition: stl_util.h:57
Botan::multimap_insert
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition: stl_util.h:76