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