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

  1 martin 1.15 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.16 //
  3 martin 1.15 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.16 //
 10 martin 1.15 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.16 //
 17 martin 1.15 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.16 //
 20 martin 1.15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.16 //
 28 martin 1.15 //////////////////////////////////////////////////////////////////////////
 29 a.arora 1.1  //
 30              //%/////////////////////////////////////////////////////////////////////////////
 31              
 32              #ifndef Pegasus_AutoPtr_h
 33              #define Pegasus_AutoPtr_h
 34              
 35              #include <Pegasus/Common/Config.h>
 36              
 37              PEGASUS_NAMESPACE_BEGIN
 38              
 39 kumpf   1.13 /** Function object for deleting pointer referring to a single object.
 40              */
 41              template<class T>
 42              struct DeletePtr
 43              {
 44                  void operator()(T* ptr)
 45                  {
 46                      delete ptr;
 47                  }
 48              };
 49              
 50 mike    1.14 /** Function object for freeing (rather than deleting) pointer to char*. For
 51                  example:
 52              
 53                      <pre>
 54                      char* ptr = (char*)malloc(80);
 55                      AutoPtr<char, FreeCharPtr> ap(ptr);
 56                      </pre>
 57              */
 58              struct FreeCharPtr
 59              {
 60 kumpf   1.17     void operator()(char* ptr)
 61                  {
 62                      free(ptr);
 63 mike    1.14     }
 64              };
 65              
 66 kumpf   1.13 /** Function object for deleting a pointer referring to an array.
 67              */
 68              template<class T>
 69              struct DeleteArrayPtr
 70              {
 71                  void operator()(T* ptr)
 72                  {
 73                      delete [] ptr;
 74                  }
 75              };
 76              
 77 a.arora 1.1  /**
 78 kumpf   1.13     This class provides a convenient way of disposing of a heap object.
 79 a.arora 1.1      It automatically deletes the enclosed pointer on destruction. For
 80                  example:
 81              
 82                  <pre>
 83 david.dillard 1.10         A* a = new A;
 84 kumpf         1.13         AutoPtr<A> dummy(a);
 85 a.arora       1.1      </pre>
 86                    
 87                        When the AutoPtr object destructs, it frees the instance of A.
 88 kumpf         1.13     This is particularly useful when a function has multiple returns or
 89                        exception conditions.
 90 a.arora       1.1  
 91                        There are two classes here: AutoPtr<> and AutoArrayPtr<>. The
 92 david.dillard 1.10     AutoArrayPtr<> class is used when a pointer must be deleted using the
 93 kumpf         1.13     array form of the delete operator ("delete []").
 94 a.arora       1.1  */
 95                    
 96 kumpf         1.13 template<class X, class D = DeletePtr<X> > class AutoPtr
 97                    {
 98 a.arora       1.1  public:
 99                    
100                        // This constructor helps this AutoPtr to take ownership of the memory
101                        // object pointed by p. It also acts as a default constructor (if no
102                        // argument is passed, it assigns a value of "0" to _ptr.
103                        // Example:  AutoPtr<SSLContext> sslContextA(new SSLContext());
104                        //           AutoPtr<SSLContext> sslContextB;
105                        //   sslContextB here has _ptr set to "0".
106 kumpf         1.13     explicit AutoPtr(X* p = 0) throw()
107 david.dillard 1.10         : _ptr(p)
108                        {
109                        }
110                    
111                        // Destructor makes sure to delete the object pointed by _ptr thus
112                        // avoiding memory leaks
113 kumpf         1.13     ~AutoPtr() throw()
114 david.dillard 1.10     {
115 kumpf         1.13         d(_ptr);
116 david.dillard 1.10     }
117                    
118                        // This method can be used to get the pointer to heap object encapsulated
119                        // in 'this' AutoPtr object
120                        // Example:   AutoPtr<classA> objA;
121                        //            func1(objA.get(), NULL);
122                        // Here func1() is a function which takes first argument as pointer to the
123                        // object of classA.
124 kumpf         1.13     inline X* get() const throw()
125 david.dillard 1.10     {
126 kumpf         1.12         return _ptr;
127 david.dillard 1.10     }
128                    
129                        // Returns the heap object itself (not the pointer to it)
130 kumpf         1.13     inline X& operator*() const throw()
131 david.dillard 1.10     {
132 kumpf         1.12         return *_ptr;
133 a.arora       1.3      }
134 a.arora       1.1  
135 david.dillard 1.10     // A very important overloading, which allows you to directly use 'this'
136                        // object as a pointer, whenever accessing a member variable/function of
137                        // the object pointed to by _ptr.
138                        // Example:   AutoPtr<classA> objA;
139                        //            objA->funcA();
140                        // funcA() is a function in the classA. Although objA is an AutoPtr, still
141                        // "->" operator would result in calling _ptr->funcA() because of this
142                        // overloading only.
143 kumpf         1.13     inline X* operator->() const throw()
144 david.dillard 1.10     {
145 kumpf         1.12         return _ptr;
146 david.dillard 1.10     }
147 a.arora       1.1  
148 david.dillard 1.10     // Relase the ownership of the memory object without deleting it !
149                        // Return the pointer to the heap object and set _ptr to "0".
150                        inline X* release() throw()
151                        {
152 kumpf         1.13         X* t = _ptr;
153 david.dillard 1.10         _ptr = 0;
154 kumpf         1.12         return t;
155 david.dillard 1.10     }
156                    
157                        // Delete the heap object and thus release ownership
158 kumpf         1.13     inline void reset(X* p = 0) throw()
159 david.dillard 1.10     {
160                            if (p != _ptr)
161                            {
162 kumpf         1.13             d(_ptr);
163 david.dillard 1.10             _ptr = p;
164                            }
165                        }
166                    
167 kumpf         1.13 private:
168                        AutoPtr(const AutoPtr<X>&);
169                        AutoPtr<X>& operator=(const AutoPtr<X>&);
170 david.dillard 1.10 
171 kumpf         1.13     // An object that knows how to delete the dynamic memory correctly
172                        D d;
173 david.dillard 1.10 
174 a.arora       1.1      // A pointer to the heap object
175 kumpf         1.13     X* _ptr;
176 a.arora       1.1  };
177                    
178                    
179 kumpf         1.13 template<class X> class AutoArrayPtr : public AutoPtr<X, DeleteArrayPtr<X> >
180                    {
181 a.arora       1.1  public:
182 david.dillard 1.10 
183 kumpf         1.13     explicit AutoArrayPtr(X* p = 0) throw()
184                            : AutoPtr<X, DeleteArrayPtr<X> >(p)
185 david.dillard 1.10     {
186                        }
187                    
188 kumpf         1.13     X& operator[](Uint32 index) throw()
189 david.dillard 1.10     {
190 kumpf         1.13         return this->get()[index];
191 david.dillard 1.10     }
192                    
193 kumpf         1.13     const X& operator[](Uint32 index) const throw()
194 david.dillard 1.10     {
195 kumpf         1.13         return this->get()[index];
196 david.dillard 1.10     }
197                    
198 a.arora       1.1  private:
199 kumpf         1.13     AutoArrayPtr(const AutoArrayPtr<X>&);
200                        AutoArrayPtr<X>& operator=(const AutoArrayPtr<X>&);
201 a.arora       1.1  };
202                    
203                    PEGASUS_NAMESPACE_END
204                    
205                    #endif /* Pegasus_AutoPtr_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2