1 mike 1.8 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.10 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.8 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.10 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.8 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.10 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.8 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.10 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.8 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Mike Brasher (mbrasher@bmc.com)
25 //
26 // Modified By:
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #ifndef Pegasus_Destroyer_h
31 #define Pegasus_Destroyer_h
32
33 #include <Pegasus/Common/Config.h>
34
35 PEGASUS_NAMESPACE_BEGIN
36
37 /**
38 This class provides a convenient way of disposing of a heap object.
39 It automatically deletes the enclosed pointer on destruction. For
40 mike 1.8 example:
41
42 <pre>
43 A* a = new A;
44 Destroyer<A> dummy = a;
45 </pre>
46
47 When the destroyer object destructs, it frees the instance of A.
48 This is particularly useful when a function has multiple returns.
49
50 There are two classes here: Destroyer<> and ArrayDestroyer<>. The
51 ArrayDestroyer<> class is used when a pointer must be deleted using the
52 array form as shown below:
53
54 <pre>
55 delete [] ptr;
56 <pre>
57 */
58 template<class T>
59 class Destroyer
60 {
61 mike 1.8 public:
62
63 Destroyer(T* ptr) : _ptr(ptr) { }
64
65 ~Destroyer() { delete _ptr; }
66
67 T* getPointer() { return _ptr; }
68
69 private:
70
|
71 mike 1.9 #ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM
72 Destroyer(const Destroyer<T>&) { }
73
74 Destroyer<T>& operator=(const Destroyer<T>&) { return *this; }
75 #else
|
76 mike 1.8 Destroyer(const Destroyer&) { }
77
78 Destroyer& operator=(const Destroyer&) { return *this; }
|
79 mike 1.9 #endif
|
80 mike 1.8
81 T* _ptr;
82 };
83
84 template<class T>
85 class ArrayDestroyer
86 {
87 public:
88
89 ArrayDestroyer(T* ptr) : _ptr(ptr) { }
90
91 ~ArrayDestroyer() { delete [] _ptr; }
92
93 T* getPointer() { return _ptr; }
94
95 ArrayDestroyer(const ArrayDestroyer<T>&) { }
96
97 ArrayDestroyer<T>& operator=(const ArrayDestroyer<T>&) { return *this; }
98
99 private:
100
101 mike 1.8 T* _ptr;
102 };
103
104 PEGASUS_NAMESPACE_END
105
106 #endif /* Pegasus_Destroyer_h */
|