scoped_nsautorelease_pool.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #if defined(__OBJC__)
  8. @class NSAutoreleasePool;
  9. #else // __OBJC__
  10. class NSAutoreleasePool;
  11. #endif // __OBJC__
  12. namespace base::mac {
  13. // ScopedNSAutoreleasePool allocates an NSAutoreleasePool when instantiated and
  14. // sends it a -drain message when destroyed. This allows an autorelease pool to
  15. // be maintained in ordinary C++ code without bringing in any direct Objective-C
  16. // dependency.
  17. //
  18. // Use only in C++ code; use @autoreleasepool in Obj-C(++) code.
  19. class BASE_EXPORT ScopedNSAutoreleasePool {
  20. public:
  21. ScopedNSAutoreleasePool();
  22. ScopedNSAutoreleasePool(const ScopedNSAutoreleasePool&) = delete;
  23. ScopedNSAutoreleasePool& operator=(const ScopedNSAutoreleasePool&) = delete;
  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. };
  33. } // namespace base::mac
  34. #endif // BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_