1 // Copyright 2010 Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 /// \file utils/auto_array.hpp 30 /// Provides the utils::auto_array class. 31 /// 32 /// The class is provided as a separate module on its own to minimize 33 /// header-inclusion side-effects. 34 35 #if !defined(UTILS_AUTO_ARRAY_HPP) 36 #define UTILS_AUTO_ARRAY_HPP 37 38 #include <cstddef> 39 40 namespace utils { 41 42 43 template< class > class auto_array; 44 45 46 namespace detail { 47 48 49 /// Wrapper class to provide reference semantics for utils::auto_array. 50 /// 51 /// This class is internally used, for example, to allow returning a 52 /// utils::auto_array from a function. 53 template< class T > 54 class auto_array_ref { 55 /// Internal pointer to the dynamically-allocated array. 56 T* _ptr; 57 58 template< class > friend class utils::auto_array; 59 60 public: 61 explicit auto_array_ref(T*); 62 }; 63 64 65 } // namespace detail 66 67 68 /// A simple smart pointer for arrays providing strict ownership semantics. 69 /// 70 /// This class is the counterpart of std::auto_ptr for arrays. The semantics of 71 /// the API of this class are the same as those of std::auto_ptr. 72 /// 73 /// The wrapped pointer must be NULL or must have been allocated using operator 74 /// new[]. 75 template< class T > 76 class auto_array { 77 /// Internal pointer to the dynamically-allocated array. 78 T* _ptr; 79 80 public: 81 auto_array(T* = NULL) throw(); 82 auto_array(auto_array< T >&) throw(); 83 auto_array(detail::auto_array_ref< T >) throw(); 84 ~auto_array(void) throw(); 85 86 T* get(void) throw(); 87 const T* get(void) const throw(); 88 89 T* release(void) throw(); 90 void reset(T* = NULL) throw(); 91 92 auto_array< T >& operator=(auto_array< T >&) throw(); 93 auto_array< T >& operator=(detail::auto_array_ref< T >) throw(); 94 T& operator[](int) throw(); 95 const T& operator[](int) const throw(); 96 operator detail::auto_array_ref< T >(void) throw(); 97 }; 98 99 100 } // namespace utils 101 102 103 #endif // !defined(UTILS_AUTO_ARRAY_HPP) 104