1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/singleton_objc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,60 @@ 1.4 +// Copyright (c) 2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// Support for using the Singleton<T> pattern with Objective-C objects. A 1.9 +// SingletonObjC is the same as a Singleton, except the default traits are 1.10 +// appropriate for Objective-C objects. A typical Objective-C object of type 1.11 +// NSExampleType can be maintained as a singleton and accessed with: 1.12 +// 1.13 +// NSExampleType* exampleSingleton = SingletonObjC<NSExampleType>::get(); 1.14 +// 1.15 +// The first time this is used, it will create exampleSingleton as the result 1.16 +// of [[NSExampleType alloc] init]. Subsequent calls will return the same 1.17 +// NSExampleType* object. The object will be released by calling 1.18 +// -[NSExampleType release] when Singleton's atexit routines run 1.19 +// (see singleton.h). 1.20 +// 1.21 +// For Objective-C objects initialized through means other than the 1.22 +// no-parameter -init selector, DefaultSingletonObjCTraits may be extended 1.23 +// as needed: 1.24 +// 1.25 +// struct FooSingletonTraits : public DefaultSingletonObjCTraits<Foo> { 1.26 +// static Foo* New() { 1.27 +// return [[Foo alloc] initWithName:@"selecty"]; 1.28 +// } 1.29 +// } 1.30 +// ... 1.31 +// Foo* widgetSingleton = SingletonObjC<Foo, FooSingletonTraits>::get(); 1.32 + 1.33 +#ifndef BASE_SINGLETON_OBJC_H_ 1.34 +#define BASE_SINGLETON_OBJC_H_ 1.35 + 1.36 +#import <Foundation/Foundation.h> 1.37 +#include "base/singleton.h" 1.38 + 1.39 +// Singleton traits usable to manage traditional Objective-C objects, which 1.40 +// are instantiated by sending |alloc| and |init| messages, and are deallocated 1.41 +// in a memory-managed environment when their retain counts drop to 0 by 1.42 +// sending |release| messages. 1.43 +template<typename Type> 1.44 +struct DefaultSingletonObjCTraits : public DefaultSingletonTraits<Type> { 1.45 + static Type* New() { 1.46 + return [[Type alloc] init]; 1.47 + } 1.48 + 1.49 + static void Delete(Type* object) { 1.50 + [object release]; 1.51 + } 1.52 +}; 1.53 + 1.54 +// Exactly like Singleton, but without the DefaultSingletonObjCTraits as the 1.55 +// default trait class. This makes it straightforward for Objective-C++ code 1.56 +// to hold Objective-C objects as singletons. 1.57 +template<typename Type, 1.58 + typename Traits = DefaultSingletonObjCTraits<Type>, 1.59 + typename DifferentiatingType = Type> 1.60 +class SingletonObjC : public Singleton<Type, Traits, DifferentiatingType> { 1.61 +}; 1.62 + 1.63 +#endif // BASE_SINGLETON_OBJC_H_