diff options
Diffstat (limited to 'base/mac')
-rw-r--r-- | base/mac/scoped_aedesc.h | 53 | ||||
-rw-r--r-- | base/mac/scoped_nsautorelease_pool.h | 55 | ||||
-rw-r--r-- | base/mac/scoped_nsautorelease_pool.mm | 32 |
3 files changed, 140 insertions, 0 deletions
diff --git a/base/mac/scoped_aedesc.h b/base/mac/scoped_aedesc.h new file mode 100644 index 0000000..83a38c6 --- /dev/null +++ b/base/mac/scoped_aedesc.h @@ -0,0 +1,53 @@ +// Copyright (c) 2010 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 BASE_MAC_SCOPED_AEDESC_H_ +#define BASE_MAC_SCOPED_AEDESC_H_ +#pragma once + +#import <CoreServices/CoreServices.h> + +#include "base/basictypes.h" + +namespace base { +namespace mac { + +// The ScopedAEDesc is used to scope AppleEvent descriptors. On creation, +// it will store a NULL descriptor. On destruction, it will dispose of the +// descriptor. +// +// This class is parameterized for additional type safety checks. You can use +// the generic AEDesc type by not providing a template parameter: +// ScopedAEDesc<> desc; +template <typename AEDescType = AEDesc> +class ScopedAEDesc { + public: + ScopedAEDesc() { + AECreateDesc(typeNull, NULL, 0, &desc_); + } + + ~ScopedAEDesc() { + AEDisposeDesc(&desc_); + } + + // Used for in parameters. + operator const AEDescType*() { + return &desc_; + } + + // Used for out parameters. + AEDescType* OutPointer() { + return &desc_; + } + + private: + AEDescType desc_; + + DISALLOW_COPY_AND_ASSIGN(ScopedAEDesc); +}; + +} // namespace mac +} // namespace base + +#endif // BASE_MAC_SCOPED_AEDESC_H_ diff --git a/base/mac/scoped_nsautorelease_pool.h b/base/mac/scoped_nsautorelease_pool.h new file mode 100644 index 0000000..0a95bd1b --- /dev/null +++ b/base/mac/scoped_nsautorelease_pool.h @@ -0,0 +1,55 @@ +// Copyright (c) 2010 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 BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ +#define BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ +#pragma once + +#include "base/basictypes.h" + +#if defined(OS_MACOSX) +#if defined(__OBJC__) +@class NSAutoreleasePool; +#else // __OBJC__ +class NSAutoreleasePool; +#endif // __OBJC__ +#endif // OS_MACOSX + +namespace base { +namespace mac { + +// On the Mac, ScopedNSAutoreleasePool allocates an NSAutoreleasePool when +// instantiated and sends it a -drain message when destroyed. This allows an +// autorelease pool to be maintained in ordinary C++ code without bringing in +// any direct Objective-C dependency. +// +// On other platforms, ScopedNSAutoreleasePool is an empty object with no +// effects. This allows it to be used directly in cross-platform code without +// ugly #ifdefs. +class ScopedNSAutoreleasePool { + public: +#if !defined(OS_MACOSX) + ScopedNSAutoreleasePool() {} + void Recycle() { } +#else // OS_MACOSX + ScopedNSAutoreleasePool(); + ~ScopedNSAutoreleasePool(); + + // Clear out the pool in case its position on the stack causes it to be + // alive for long periods of time (such as the entire length of the app). + // Only use then when you're certain the items currently in the pool are + // no longer needed. + void Recycle(); + private: + NSAutoreleasePool* autorelease_pool_; +#endif // OS_MACOSX + + private: + DISALLOW_COPY_AND_ASSIGN(ScopedNSAutoreleasePool); +}; + +} // namespace mac +} // namespace base + +#endif // BASE_MAC_SCOPED_NSAUTORELEASE_POOL_H_ diff --git a/base/mac/scoped_nsautorelease_pool.mm b/base/mac/scoped_nsautorelease_pool.mm new file mode 100644 index 0000000..e542ca8 --- /dev/null +++ b/base/mac/scoped_nsautorelease_pool.mm @@ -0,0 +1,32 @@ +// Copyright (c) 2010 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. + +#include "base/mac/scoped_nsautorelease_pool.h" + +#import <Foundation/Foundation.h> + +#include "base/logging.h" + +namespace base { +namespace mac { + +ScopedNSAutoreleasePool::ScopedNSAutoreleasePool() + : autorelease_pool_([[NSAutoreleasePool alloc] init]) { + DCHECK(autorelease_pool_); +} + +ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() { + [autorelease_pool_ drain]; +} + +// Cycle the internal pool, allowing everything there to get cleaned up and +// start anew. +void ScopedNSAutoreleasePool::Recycle() { + [autorelease_pool_ drain]; + autorelease_pool_ = [[NSAutoreleasePool alloc] init]; + DCHECK(autorelease_pool_); +} + +} // namespace mac +} // namespace base |