Botan: Botan::Algorithm_Cache< T > Class Template Reference

Botan 1.10.17

Public Member Functions | List of all members
Botan::Algorithm_Cache< T > Class Template Reference

`#include <algo_cache.h>`

## Public Member Functions
void | add (T *algo, const std::string &requested_name, const std::string &provider_name)
| Algorithm_Cache (Mutex *m)
void | clear_cache ()
const T * | get (const std::string &algo_spec, const std::string &pref_provider)
std::vector< std::string > | providers_of (const std::string &algo_name)
void | set_preferred_provider (const std::string &algo_spec, const std::string &provider)
| ~Algorithm_Cache ()

## Detailed Description

### template<typename T>
class Botan::Algorithm_Cache< T >

Algorithm_Cache (used by Algorithm_Factory)

Definition at line 29 of file algo_cache.h.

## Constructor & Destructor Documentation

## ◆ Algorithm_Cache()

template<typename T>

Botan::Algorithm_Cache< T >::Algorithm_Cache | ( | Mutex * | m | ) |
| inline

Constructor

Parameters

  m | a mutex to serialize internal access

Definition at line 74 of file algo_cache.h.

74 : mutex(m) {}

## ◆ ~Algorithm_Cache()

template<typename T>

Botan::Algorithm_Cache< T >::~Algorithm_Cache | ( |  | ) |
| inline

Definition at line 75 of file algo_cache.h.

75 { clear_cache(); delete mutex; }
Botan::Algorithm_Cache::clear_cache
void clear_cache()
Definition: algo_cache.h:228

## Member Function Documentation

## ◆ add()

template<typename T>

void Botan::Algorithm_Cache< T >::add | ( | T * | algo,
|  | const std::string & | requested_name,
|  | const std::string & | provider_name
| ) |  |

Add a new algorithm implementation to the cache

Parameters

  algo | the algorithm prototype object
  requested_name | how this name will be requested
  provider_name | is the name of the provider of this prototype

Definition at line 165 of file algo_cache.h.

168 {
169 if(!algo)
170 return;
171
172 Mutex_Holder lock(mutex);
173
174 if(algo->name() != requested_name &&
175 aliases.find(requested_name) == aliases.end())
176 {
177 aliases[requested_name] = algo->name();
178 }
179
180 if(!algorithms[algo->name()][provider])
181 algorithms[algo->name()][provider] = algo;
182 else
183 delete algo;
184 }

## ◆ clear_cache()

template<typename T >

void Botan::Algorithm_Cache< T >::clear_cache | ( |  | ) |

Clear the cache

Definition at line 228 of file algo_cache.h.

Referenced by Botan::Algorithm_Cache< Botan::MessageAuthenticationCode >::~Algorithm_Cache().

229 {
230 algorithms_iterator algo = algorithms.begin();
231
232 while(algo != algorithms.end())
233 {
234 provider_iterator provider = algo->second.begin();
235
236 while(provider != algo->second.end())
237 {
238 delete provider->second;
239 ++provider;
240 }
241
242 ++algo;
243 }
244
245 algorithms.clear();
246 }

## ◆ get()

template<typename T >

const T * Botan::Algorithm_Cache< T >::get | ( | const std::string & | algo_spec,
|  | const std::string & | pref_provider
| ) |  |

Parameters

  algo_spec | names the requested algorithm
  pref_provider | suggests a preferred provider

Returns
  prototype object, or NULL

Definition at line 117 of file algo_cache.h.

119 {
120 Mutex_Holder lock(mutex);
121
122 algorithms_iterator algo = find_algorithm(algo_spec);
123 if(algo == algorithms.end()) // algo not found at all (no providers)
124 return 0;
125
126 // If a provider is requested specifically, return it or fail entirely
127 if(requested_provider != "")
128 {
129 provider_iterator prov = algo->second.find(requested_provider);
130 if(prov != algo->second.end())
131 return prov->second;
132 return 0;
133 }
134
135 const T* prototype = 0;
136 std::string prototype_provider;
137 size_t prototype_prov_weight = 0;
138
139 const std::string pref_provider = search_map(pref_providers, algo_spec);
140
141 for(provider_iterator i = algo->second.begin(); i != algo->second.end(); ++i)
142 {
143 const std::string prov_name = i->first;
144 const size_t prov_weight = static_provider_weight(prov_name);
145
146 // preferred prov exists, return immediately
147 if(prov_name == pref_provider)
148 return i->second;
149
150 if(prototype == 0 || prov_weight > prototype_prov_weight)
151 {
152 prototype = i->second;
153 prototype_provider = i->first;
154 prototype_prov_weight = prov_weight;
155 }
156 }
157
158 return prototype;
159 }
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::static_provider_weight
size_t static_provider_weight(const std::string &prov_name)
Definition: prov_weight.cpp:15

## ◆ providers_of()

template<typename T >

std::vector< std::string > Botan::Algorithm_Cache< T >::providers_of | ( | const std::string & | algo_name | ) |

Return the list of providers of this algorithm

Parameters

  algo_name | names the algorithm

Returns
  list of providers of this algorithm

Definition at line 190 of file algo_cache.h.

191 {
192 Mutex_Holder lock(mutex);
193
194 std::vector<std::string> providers;
195
196 algorithms_iterator algo = find_algorithm(algo_name);
197
198 if(algo != algorithms.end())
199 {
200 provider_iterator provider = algo->second.begin();
201
202 while(provider != algo->second.end())
203 {
204 providers.push_back(provider->first);
205 ++provider;
206 }
207 }
208
209 return providers;
210 }

## ◆ set_preferred_provider()

template<typename T >

void Botan::Algorithm_Cache< T >::set_preferred_provider | ( | const std::string & | algo_spec,
|  | const std::string & | provider
| ) |  |

Set the preferred provider

Parameters

  algo_spec | names the algorithm
  provider | names the preferred provider

Definition at line 216 of file algo_cache.h.

218 {
219 Mutex_Holder lock(mutex);
220
221 pref_providers[algo_spec] = provider;
222 }

The documentation for this class was generated from the following file:

- src/algo_factory/algo_cache.h