monotone

monotone Mtn Source Tree

Root/botan_pipe_cache.hh

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

Archive Download this file

Branches

Tags

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