monotone

monotone Mtn Source Tree

Root/cleanup.hh

1#ifndef __CLEANUP_HH__
2#define __CLEANUP_HH__
3
4// Copyright (C) 2002 Graydon Hoare <graydon@pobox.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// 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// Local Variables:
39// mode: C++
40// fill-column: 76
41// c-file-style: "gnu"
42// indent-tabs-mode: nil
43// End:
44// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
45
46#endif // __CLEANUP_HH__

Archive Download this file

Branches

Tags

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