scoped_nsautorelease_pool.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_
  5. #define BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_
  6. #include "base/base_export.h"
  7. #include "base/macros.h"
  8. #if defined(__OBJC__)
  9. @class NSAutoreleasePool;
  10. #else // __OBJC__
  11. class NSAutoreleasePool;
  12. #endif // __OBJC__
  13. namespace base {
  14. namespace mac {
  15. // ScopedNSAutoreleasePool allocates an NSAutoreleasePool when instantiated and
  16. // sends it a -drain message when destroyed. This allows an autorelease pool to
  17. // be maintained in ordinary C++ code without bringing in any direct Objective-C
  18. // dependency.
  19. //
  20. // Use only in C++ code; use @autoreleasepool in Obj-C(++) code.
  21. class BASE_EXPORT ScopedNSAutoreleasePool {
  22. public:
  23. ScopedNSAutoreleasePool();
  24. ~ScopedNSAutoreleasePool();
  25. // Clear out the pool in case its position on the stack causes it to be
  26. // alive for long periods of time (such as the entire length of the app).
  27. // Only use then when you're certain the items currently in the pool are
  28. // no longer needed.
  29. void Recycle();
  30. private:
  31. NSAutoreleasePool* autorelease_pool_;
  32. private:
  33. DISALLOW_COPY_AND_ASSIGN(ScopedNSAutoreleasePool);
  34. };
  35. } // namespace mac
  36. } // namespace base
  37. #endif // BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_