monotone

monotone Mtn Source Tree

Root/botan/dl_cache.cpp

1/*************************************************
2* DL Group Cache Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/dl_param.h>
7#include <botan/mutex.h>
8#include <botan/init.h>
9#include <map>
10
11namespace Botan {
12
13/*************************************************
14* Try to obtain a particular DL group *
15*************************************************/
16extern DL_Group try_to_get_dl_group(const std::string&);
17
18namespace {
19
20/*************************************************
21* Cache for DL_Group objects *
22*************************************************/
23class DL_Group_Cache
24 {
25 public:
26 const DL_Group& get(const std::string&);
27 void add(const std::string&, const DL_Group&);
28
29 DL_Group_Cache() { groups_mutex = get_mutex(); }
30 ~DL_Group_Cache() { groups.clear(); delete groups_mutex; }
31 private:
32 std::map<std::string, DL_Group> groups;
33 Mutex* groups_mutex;
34 };
35
36/*************************************************
37* Get a DL_Group *
38*************************************************/
39const DL_Group& DL_Group_Cache::get(const std::string& name)
40 {
41 Mutex_Holder lock(groups_mutex);
42
43 std::map<std::string, DL_Group>::const_iterator group;
44 group = groups.find(name);
45 if(group != groups.end())
46 return group->second;
47
48 groups.insert(std::make_pair(name, try_to_get_dl_group(name)));
49
50 group = groups.find(name);
51 if(group != groups.end())
52 return group->second;
53
54 throw Lookup_Error("DL group \"" + name + "\" not found");
55 }
56
57/*************************************************
58* Add a new DL_Group *
59*************************************************/
60void DL_Group_Cache::add(const std::string& name, const DL_Group& group)
61 {
62 Mutex_Holder lock(groups_mutex);
63 groups.insert(std::make_pair(name, group));
64 }
65
66/*************************************************
67* Global state for DL_Group cache *
68*************************************************/
69DL_Group_Cache* dl_groups = 0;
70
71}
72
73/*************************************************
74* Retrieve a DL group by name *
75*************************************************/
76const DL_Group& get_dl_group(const std::string& name)
77 {
78 return dl_groups->get(name);
79 }
80
81/*************************************************
82* Register a named DL group *
83*************************************************/
84void add_dl_group(const std::string& name, const DL_Group& group)
85 {
86 dl_groups->add(name, group);
87 }
88
89namespace Init {
90
91/*************************************************
92* Create the cache *
93*************************************************/
94void startup_dl_cache()
95 {
96 dl_groups = new DL_Group_Cache;
97 }
98
99/*************************************************
100* Destroy the cache *
101*************************************************/
102void shutdown_dl_cache()
103 {
104 delete dl_groups;
105 dl_groups = 0;
106 }
107
108}
109
110}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status