// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ #define I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_ #if defined(I18N_PHONENUMBERS_USE_BOOST) #include <boost/scoped_ptr.hpp> using boost::scoped_ptr; #else // !I18N_PHONENUMBERS_USE_BOOST // This is an implementation designed to match the anticipated future TR2 // implementation of the scoped_ptr class and scoped_ptr_malloc (deprecated). #include <assert.h> #include <stddef.h> #include <stdlib.h> #include <algorithm> // For std::swap(). #include "phonenumbers/base/basictypes.h" #include "phonenumbers/base/template_util.h" namespace i18n { namespace phonenumbers { // Function object which deletes its parameter, which must be a pointer. // If C is an array type, invokes 'delete[]' on the parameter; otherwise, // invokes 'delete'. The default deleter for scoped_ptr<T>. template <class T> struct DefaultDeleter { … }; // Specialization of DefaultDeleter for array types. DefaultDeleter<T[]>; DefaultDeleter<T[n]>; // Function object which invokes 'free' on its parameter, which must be // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: // // scoped_ptr<int, base::FreeDeleter> foo_ptr( // static_cast<int*>(malloc(sizeof(int)))); struct FreeDeleter { … }; // Minimal implementation of the core logic of scoped_ptr, suitable for // reuse in both scoped_ptr and its specializations. template <class T, class D> class scoped_ptr_impl { … }; // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> // automatically deletes the pointer it holds (if any). // That is, scoped_ptr<T> owns the T object that it points to. // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. // Also like T*, scoped_ptr<T> is thread-compatible, and once you // dereference it, you get the thread safety guarantees of T. // // The size of scoped_ptr is small. On most compilers, when using the // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will // increase the size proportional to whatever state they need to have. See // comments inside scoped_ptr_impl<> for details. // // Current implementation targets having a strict subset of C++11's // unique_ptr<> features. Known deficiencies include not supporting move-only // deleteres, function pointers as deleters, and deleters with reference // types. template <class T, class D = DefaultDeleter<T> > class scoped_ptr { … }; scoped_ptr<T[], D>; // Free functions template <class T, class D> void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) { … } template <class T, class D> bool operator==(T* p1, const scoped_ptr<T, D>& p2) { … } template <class T, class D> bool operator!=(T* p1, const scoped_ptr<T, D>& p2) { … } // A function to convert T* into scoped_ptr<T> // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) template <typename T> scoped_ptr<T> make_scoped_ptr(T* ptr) { … } } // namespace phonenumbers } // namespace i18n #endif // !I18N_PHONENUMBERS_USE_BOOST #endif // I18N_PHONENUMBERS_BASE_MEMORY_SCOPED_PTR_H_