monotone

monotone Mtn Source Tree

Root/botan_pipe_cache.hh

1// Copyright (C) 2008 Zack Weinberg <zackw@panix.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef __BOTAN_PIPE_CACHE_HH__
11#define __BOTAN_PIPE_CACHE_HH__
12
13#include <botan/botan.h>
14#include <boost/scoped_ptr.hpp>
15
16#include "sanity.hh"
17
18// This file defines a simple lifetime-of-the-program caching strategy for
19// Botan::Pipe objects. Instead of writing
20//
21// Botan::Pipe p(...);
22//
23// you do
24//
25// static cached_botan_pipe p(new Botan::Pipe(...));
26//
27// and then use p like a Botan::Pipe object (except with -> instead of .).
28//
29// The global pipe_cache_cleanup object takes care of destroying all such
30// cached pipes before the Botan::LibraryInitializer object is destroyed.
31
32class pipe_cache_cleanup;
33
34class cached_botan_pipe
35{
36 friend class pipe_cache_cleanup;
37 cached_botan_pipe * next_tbd;
38 boost::scoped_ptr<Botan::Pipe> pipe;
39
40public:
41 cached_botan_pipe(Botan::Pipe * p);
42 ~cached_botan_pipe() { I(!pipe); }
43 Botan::Pipe & operator*()
44 { I(pipe); return *pipe; }
45 Botan::Pipe * operator->()
46 { I(pipe); return pipe.get(); }
47
48 // To avoid re-using a pipe that's thrown.
49 void reset(Botan::Pipe * p)
50 { pipe.reset(p); }
51
52 // ??? operator bool, operator! a la boost::scoped_ptr
53 // (what's with the bizarro unspecified_bool_type thing?)
54};
55
56extern Botan::Pipe * unfiltered_pipe;
57extern pipe_cache_cleanup * global_pipe_cleanup_object;
58
59class pipe_cache_cleanup
60{
61 friend class cached_botan_pipe;
62 struct cached_botan_pipe * to_be_destroyed;
63
64public:
65 pipe_cache_cleanup() : to_be_destroyed(0)
66 {
67 I(!global_pipe_cleanup_object);
68 global_pipe_cleanup_object = this;
69 }
70 ~pipe_cache_cleanup()
71 {
72 for (cached_botan_pipe * p = to_be_destroyed; p; p = p->next_tbd)
73 p->pipe.reset(0);
74 global_pipe_cleanup_object = 0;
75 }
76};
77
78// must be defined after class pipe_cache_cleanup
79inline cached_botan_pipe::cached_botan_pipe(Botan::Pipe * p)
80 : pipe(p)
81{
82 I(global_pipe_cleanup_object);
83 this->next_tbd = global_pipe_cleanup_object->to_be_destroyed;
84 global_pipe_cleanup_object->to_be_destroyed = this;
85}
86
87#endif // __BOTAN_PIPE_CACHE_HH__
88
89// Local Variables:
90// mode: C++
91// fill-column: 76
92// c-file-style: "gnu"
93// indent-tabs-mode: nil
94// End:
95// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

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