(file) Return to AutoPtr.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.9 //%2005////////////////////////////////////////////////////////////////////////
  2 a.arora 1.1 //
  3 karl    1.5 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4             // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5             // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 a.arora 1.1 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl    1.5 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8             // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl    1.9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10             // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 a.arora 1.1 //
 12             // Permission is hereby granted, free of charge, to any person obtaining a copy
 13             // of this software and associated documentation files (the "Software"), to
 14             // deal in the Software without restriction, including without limitation the
 15             // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16             // sell copies of the Software, and to permit persons to whom the Software is
 17             // furnished to do so, subject to the following conditions:
 18 david.dillard 1.10 //
 19 a.arora       1.1  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20                    // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21                    // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22                    // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23                    // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24                    // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25                    // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26                    // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27                    //
 28                    //==============================================================================
 29                    //
 30                    // Author: Amit Arora, IBM (amita@in.ibm.com) (based on PEP101 by David Dillard)
 31                    //
 32 a.arora       1.6  // Modified By: Amit Arora, IBM (amita@in.ibm.com) for Bug#2168
 33 a.arora       1.8  //              Amit Arora, IBM (amita@in.ibm.com) for Bug#2480
 34 david.dillard 1.10 //              David Dillard, VERITAS Software Corp.
 35                    //                  (david.dillard@veritas.com)
 36 a.arora       1.1  //
 37                    //%/////////////////////////////////////////////////////////////////////////////
 38                    
 39                    #ifndef Pegasus_AutoPtr_h
 40                    #define Pegasus_AutoPtr_h
 41                    
 42                    #include <Pegasus/Common/Config.h>
 43                    
 44                    PEGASUS_NAMESPACE_BEGIN
 45                    
 46                    /**
 47                        This class provides a convenient way of disposing off a heap object.
 48                        It automatically deletes the enclosed pointer on destruction. For
 49                        example:
 50                    
 51                        <pre>
 52 david.dillard 1.10         A* a = new A;
 53                            AutoPtr<A> dummy = a;
 54 a.arora       1.1      </pre>
 55                    
 56                        When the AutoPtr object destructs, it frees the instance of A.
 57                        This is particularly useful when a function has multiple returns.
 58                    
 59                        There are two classes here: AutoPtr<> and AutoArrayPtr<>. The
 60 david.dillard 1.10     AutoArrayPtr<> class is used when a pointer must be deleted using the
 61 a.arora       1.1      array form as shown below:
 62                    
 63                        <pre>
 64 david.dillard 1.10         delete [] ptr;
 65 a.arora       1.1      <pre>
 66                    */
 67                    
 68                    template<class X> class AutoPtr;
 69 david.dillard 1.10 
 70 a.arora       1.1  template<class X> class AutoPtrRef {
 71                    public:
 72 david.dillard 1.10     inline AutoPtrRef(AutoPtr<X> &ref) : _ref(ref)
 73                        {
 74 a.arora       1.1          // This block intentionally left blank
 75 david.dillard 1.10     }
 76 a.arora       1.1  
 77 david.dillard 1.10     inline AutoPtr<X> get()
 78                        {
 79                            return(_ref);
 80                        }
 81 a.arora       1.1  
 82                    private:
 83                        AutoPtr<X> &_ref;
 84                    };
 85                    
 86                    
 87                    template<class X> class AutoArrayPtr;
 88                    
 89                    template<class X> class AutoArrayPtrRef {
 90                    public:
 91 david.dillard 1.10     inline AutoArrayPtrRef(AutoArrayPtr<X> &ref) : _ref(ref)
 92                        {
 93                           // This block intentionally left blank
 94                        }
 95 a.arora       1.1  
 96 david.dillard 1.10     inline AutoArrayPtr<X> get()
 97                        {
 98                            return(_ref);
 99                        }
100 a.arora       1.1  
101                    private:
102 david.dillard 1.10     AutoArrayPtr<X> &_ref;
103 a.arora       1.1  };
104                    
105                    
106                    
107                    template<class X> class AutoPtr {
108                    public:
109                        // This constructor helps this AutoPtr to take ownership of the memory
110                        // object pointed by p. It also acts as a default constructor (if no
111                        // argument is passed, it assigns a value of "0" to _ptr.
112                        // Example:  AutoPtr<SSLContext> sslContextA(new SSLContext());
113                        //           AutoPtr<SSLContext> sslContextB;
114                        //   sslContextB here has _ptr set to "0".
115 david.dillard 1.10     inline explicit AutoPtr(X* p = 0) throw()
116                            : _ptr(p)
117                        {
118                            // This block intentionally left blank
119                        }
120                    
121                        // Destructor makes sure to delete the object pointed by _ptr thus
122                        // avoiding memory leaks
123                        inline ~AutoPtr() throw()
124                        {
125                            delete _ptr;
126                        }
127                    
128                        // The copy constructor takes the ownership of the heap object from
129                        // the source AutoPtr object. And since there should be only one
130                        // legitimate owner, it sets the _ptr of the source to "0"
131                        inline AutoPtr(AutoPtrRef<X> &a) throw()
132                            : _ptr((a.get()).release())
133                        {
134                            //a._ptr = 0;
135                        }
136 david.dillard 1.10 
137                        inline AutoPtr(AutoPtr<X> &a) throw() : _ptr(a.release())
138                        {
139                        }
140                    
141                        // Overloading of "=" operator makes sure that the ownership of the memory
142                        // gets transferred properly to 'this' AutoPtr object.
143                        // Example:   AutoPtr<HTTPConnection> httpConnectionB = httpConnectionA;
144                        AutoPtr<X> &operator=(AutoPtr<X>& a) throw()
145                        {
146                            if ( this != &a )
147                                reset(a.release());
148                            return(*this);
149                        }
150 a.arora       1.6  
151 a.arora       1.3      inline AutoPtr<X> &operator=(AutoPtrRef<X> &a) throw()
152                        {
153                            reset((a.get()).release());
154 david.dillard 1.10         return(*this);
155                        }
156                    
157                        // This method can be used to get the pointer to heap object encapsulated
158                        // in 'this' AutoPtr object
159                        // Example:   AutoPtr<classA> objA;
160                        //            func1(objA.get(), NULL);
161                        // Here func1() is a function which takes first argument as pointer to the
162                        // object of classA.
163                        inline X *get() const throw()
164                        {
165                            return(_ptr);
166                        }
167                    
168                        // Returns the heap object itself (not the pointer to it)
169                        inline X &operator*() const throw()
170                        {
171                            return(*_ptr);
172 a.arora       1.3      }
173 a.arora       1.1  
174 david.dillard 1.10     // A very important overloading, which allows you to directly use 'this'
175                        // object as a pointer, whenever accessing a member variable/function of
176                        // the object pointed to by _ptr.
177                        // Example:   AutoPtr<classA> objA;
178                        //            objA->funcA();
179                        // funcA() is a function in the classA. Although objA is an AutoPtr, still
180                        // "->" operator would result in calling _ptr->funcA() because of this
181                        // overloading only.
182                        inline X *operator->() const throw()
183                        {
184                            return(_ptr);
185                        }
186 a.arora       1.1  
187 david.dillard 1.10     // Relase the ownership of the memory object without deleting it !
188                        // Return the pointer to the heap object and set _ptr to "0".
189                        inline X* release() throw()
190                        {
191                            X *t = _ptr;
192                            _ptr = 0;
193                            return(t);
194                        }
195                    
196                        // Delete the heap object and thus release ownership
197                        inline void reset(X *p = 0) throw()
198                        {
199                            if (p != _ptr)
200                            {
201                                delete _ptr;
202                                _ptr = p;
203                            }
204                        }
205                    
206                        AutoPtr(AutoPtrRef<X> obj) throw()
207                            : _ptr((obj.get()).get())
208 david.dillard 1.10     {
209                            // This block intentionally left blank
210                        }
211                    
212                        template<class Y> operator AutoPtrRef<Y>() throw()
213                        {
214                            return(AutoPtrRef<Y>(*this));
215                        }
216                    
217                        template<class Y> operator AutoPtr<Y>() throw()
218                        {
219                            return(AutoPtr<Y>(release()));
220                        }
221 a.arora       1.1  
222                    private:
223                        // A pointer to the heap object
224                         X* _ptr;
225                    };
226                    
227                    
228                    template<class X> class AutoArrayPtr {
229                    public:
230 david.dillard 1.10     inline explicit AutoArrayPtr(X* p = 0) throw()
231                            : _ptr(p)
232                        {
233                            // This block intentionally left blank
234                        }
235                    
236                        inline ~AutoArrayPtr() throw()
237                        {
238                            delete [] _ptr;
239                        }
240                    
241                        inline AutoArrayPtr(AutoArrayPtrRef<X> &a) throw()
242                            : _ptr((a.get()).release())
243                        {
244                            //a._ptr = 0;
245                        }
246                    
247                        inline AutoArrayPtr(AutoArrayPtr<X> &a) throw() : _ptr(a.release())
248                        {
249                        }
250                    
251 david.dillard 1.10     AutoArrayPtr<X> &operator=(AutoArrayPtr<X>& a) throw()
252                        {
253                            reset(a.release());
254                            return(*this);
255                        }
256                    
257                        inline AutoArrayPtr<X> &operator=(AutoArrayPtrRef<X> &a) throw()
258                        {
259                            if ( this != a )
260                                reset((a.get()).release());
261                            return(*this);
262                        }
263                    
264                        inline X *get() const throw()
265                        {
266                            return(_ptr);
267                        }
268                    
269                        inline X &operator*() const throw()
270                        {
271                            return(*_ptr);
272 david.dillard 1.10     }
273                    
274                        inline X *operator->() const throw()
275                        {
276                            return(_ptr);
277                        }
278                    
279                        inline X* release() throw()
280                        {
281                            X *t = _ptr;
282                            _ptr = 0;
283                            return(t);
284                        }
285                    
286                        inline void reset(X *p = 0) throw()
287                        {
288                            if (p != _ptr)
289                            {
290                                // use "[]" since _ptr is pointer to an array
291                                delete [] _ptr;
292                                _ptr = p;
293 david.dillard 1.10         }
294                        }
295                    
296                        AutoArrayPtr(AutoArrayPtrRef<X> obj) throw()
297                            : _ptr((obj.get()).get())
298                        {
299                            // This block intentionally left blank
300                        }
301                    
302                        template<class Y> operator AutoArrayPtrRef<Y>() throw()
303                        {
304                            return(AutoArrayPtrRef<Y>(*this));
305                        }
306                    
307                        template<class Y> operator AutoArrayPtr<Y>() throw()
308                        {
309                            return(AutoArrayPtr<Y>(release()));
310                        }
311 a.arora       1.1  
312                    
313                    private:
314 david.dillard 1.10     X* _ptr;
315 a.arora       1.1  };
316                    
317                    
318                    PEGASUS_NAMESPACE_END
319                    
320                    #endif /* Pegasus_AutoPtr_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2