1 // Copyright 2010 The Kyua Authors. 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 "utils/auto_array_fwd.hpp" 39 40 #include <cstddef> 41 42 namespace utils { 43 44 45 namespace detail { 46 47 48 /// Wrapper class to provide reference semantics for utils::auto_array. 49 /// 50 /// This class is internally used, for example, to allow returning a 51 /// utils::auto_array from a function. 52 template< class T > 53 class auto_array_ref { 54 /// Internal pointer to the dynamically-allocated array. 55 T* _ptr; 56 57 template< class > friend class utils::auto_array; 58 59 public: 60 explicit auto_array_ref(T*); 61 }; 62 63 64 } // namespace detail 65 66 67 /// A simple smart pointer for arrays providing strict ownership semantics. 68 /// 69 /// This class is the counterpart of std::auto_ptr for arrays. The semantics of 70 /// the API of this class are the same as those of std::auto_ptr. 71 /// 72 /// The wrapped pointer must be NULL or must have been allocated using operator 73 /// new[]. 74 template< class T > 75 class auto_array { 76 /// Internal pointer to the dynamically-allocated array. 77 T* _ptr; 78 79 public: 80 auto_array(T* = NULL) throw(); 81 auto_array(auto_array< T >&) throw(); 82 auto_array(detail::auto_array_ref< T >) throw(); 83 ~auto_array(void) throw(); 84 85 T* get(void) throw(); 86 const T* get(void) const throw(); 87 88 T* release(void) throw(); 89 void reset(T* = NULL) throw(); 90 91 auto_array< T >& operator=(auto_array< T >&) throw(); 92 auto_array< T >& operator=(detail::auto_array_ref< T >) throw(); 93 T& operator[](int) throw(); 94 const T& operator[](int) const throw(); 95 operator detail::auto_array_ref< T >(void) throw(); 96 }; 97 98 99 } // namespace utils 100 101 102 #endif // !defined(UTILS_AUTO_ARRAY_HPP) 103