Botan 1.10.17
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
13namespace 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*/
22template<typename InputIterator, typename OutputIterator, typename Predicate>
23OutputIterator 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*/
42template<typename K, typename V>
43inline 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*/
56template<class T>
57class 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*/
66template<typename Pair>
67void delete2nd(Pair& pair)
68 {
69 delete pair.second;
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#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
void operator()(T *ptr)
Definition stl_util.h:60
OutputIterator copy_if(InputIterator current, InputIterator end, OutputIterator dest, Predicate copy_p)
Definition stl_util.h:23
void delete2nd(Pair &pair)
Definition stl_util.h:67
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
Definition stl_util.h:43
void multimap_insert(std::multimap< K, V > &multimap, const K &key, const V &value)
Definition stl_util.h:76