michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SCOPED_NSAUTORELEASE_POOL_H_ michael@0: #define BASE_SCOPED_NSAUTORELEASE_POOL_H_ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #if defined(OS_MACOSX) michael@0: #if defined(__OBJC__) michael@0: @class NSAutoreleasePool; michael@0: #else // __OBJC__ michael@0: class NSAutoreleasePool; michael@0: #endif // __OBJC__ michael@0: #endif // OS_MACOSX michael@0: michael@0: namespace base { michael@0: michael@0: // On the Mac, ScopedNSAutoreleasePool allocates an NSAutoreleasePool when michael@0: // instantiated and sends it a -drain message when destroyed. This allows an michael@0: // autorelease pool to be maintained in ordinary C++ code without bringing in michael@0: // any direct Objective-C dependency. michael@0: // michael@0: // On other platforms, ScopedNSAutoreleasePool is an empty object with no michael@0: // effects. This allows it to be used directly in cross-platform code without michael@0: // ugly #ifdefs. michael@0: class ScopedNSAutoreleasePool { michael@0: public: michael@0: #if !defined(OS_MACOSX) michael@0: ScopedNSAutoreleasePool() {} michael@0: void Recycle() { } michael@0: #else // OS_MACOSX michael@0: ScopedNSAutoreleasePool(); michael@0: ~ScopedNSAutoreleasePool(); michael@0: michael@0: // Clear out the pool in case its position on the stack causes it to be michael@0: // alive for long periods of time (such as the entire length of the app). michael@0: // Only use then when you're certain the items currently in the pool are michael@0: // no longer needed. michael@0: void Recycle(); michael@0: private: michael@0: NSAutoreleasePool* autorelease_pool_; michael@0: #endif // OS_MACOSX michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(ScopedNSAutoreleasePool); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_SCOPED_NSAUTORELEASE_POOL_H_