VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gmemory.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gmemory.h
19 ///
20 
21 #ifndef G_MEMORY_H
22 #define G_MEMORY_H
23 
24 #include "gdef.h"
25 #include <memory>
26 
27 /// A portable fix for the problem of resetting an auto_ptr<>. Some compilers
28 /// do not have a reset() method, and some have non-const assignment operators.
29 ///
30 /// Usage:
31 /// \code
32 /// {
33 /// std::auto_ptr<Foo> ptr ;
34 /// for( int i = 0 ; i < 10 ; i++ )
35 /// {
36 /// ptr <<= new Foo ;
37 /// if( ptr->fn() )
38 /// eatFoo( ptr->release() ) ;
39 /// }
40 /// }
41 /// \endcode
42 ///
43 template <typename T>
44 void operator<<=( std::auto_ptr<T> & ap , T * p )
45 {
46  std::auto_ptr<T> temp( p ) ;
47  ap = temp ;
48 }
49 
50 ///
51 template <typename T>
52 void operator<<=( std::auto_ptr<T> & ap , int /* null_pointer */ )
53 {
54  T * p = 0 ;
55  ap <<= p ;
56 }
57 
58 #endif