monotone

monotone Mtn Source Tree

Root/src/cleanup.hh

1// Copyright (C) 2002 Graydon Hoare <graydon@pobox.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 __CLEANUP_HH__
11#define __CLEANUP_HH__
12
13// This is a little "auto-cleanup" container, used to ensure things
14// from our helper C libraries are deallocated when we leave a scope.
15
16template <typename T, typename R>
17struct cleanup_ptr {
18 T ptr;
19 R (* cleanup)(T);
20 explicit cleanup_ptr(T p, R (*c)(T)) : ptr(p), cleanup(c) {}
21 ~cleanup_ptr()
22 {
23 if (cleanup && ptr)
24 cleanup(ptr);
25 }
26
27 T operator()()
28 {
29 return ptr;
30 }
31
32 T * paddr()
33 {
34 return &ptr;
35 }
36};
37
38#endif // __CLEANUP_HH__
39
40// Local Variables:
41// mode: C++
42// fill-column: 76
43// c-file-style: "gnu"
44// indent-tabs-mode: nil
45// End:
46// 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