1 /* 2 * libjingle 3 * Copyright 2008 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 // Automatically initialize and and free an autoreleasepool. Never allocate 29 // an instance of this class using "new" - that will result in a compile-time 30 // error. Only use it as a stack object. 31 // 32 // Note: NSAutoreleasePool docs say that you should not normally need to 33 // declare an NSAutoreleasePool as a member of an object - but there's nothing 34 // that indicates it will be a problem, as long as the stack lifetime of the 35 // pool exactly matches the stack lifetime of the object. 36 37 #ifndef TALK_BASE_SCOPED_AUTORELEASE_POOL_H__ 38 #define TALK_BASE_SCOPED_AUTORELEASE_POOL_H__ 39 40 #if defined(IOS) || defined(OSX) 41 42 #include "talk/base/common.h" 43 44 // This header may be included from Obj-C files or C++ files. 45 #ifdef __OBJC__ 46 @class NSAutoreleasePool; 47 #else 48 class NSAutoreleasePool; 49 #endif 50 51 namespace talk_base { 52 53 class ScopedAutoreleasePool { 54 public: 55 ScopedAutoreleasePool(); 56 ~ScopedAutoreleasePool(); 57 58 private: 59 // Declaring private overrides of new and delete here enforces the "only use 60 // as a stack object" discipline. 61 // 62 // Note: new is declared as "throw()" to get around a gcc warning about new 63 // returning NULL, but this method will never get called and therefore will 64 // never actually throw any exception. 65 void* operator new(size_t size) throw() { return NULL; } 66 void operator delete (void* ptr) {} 67 68 NSAutoreleasePool* pool_; 69 70 DISALLOW_EVIL_CONSTRUCTORS(ScopedAutoreleasePool); 71 }; 72 73 } // namespace talk_base 74 75 #endif // IOS || OSX 76 #endif // TALK_BASE_SCOPED_AUTORELEASE_POOL_H__ 77