ipc/chromium/src/base/singleton_objc.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // Support for using the Singleton<T> pattern with Objective-C objects. A
michael@0 6 // SingletonObjC is the same as a Singleton, except the default traits are
michael@0 7 // appropriate for Objective-C objects. A typical Objective-C object of type
michael@0 8 // NSExampleType can be maintained as a singleton and accessed with:
michael@0 9 //
michael@0 10 // NSExampleType* exampleSingleton = SingletonObjC<NSExampleType>::get();
michael@0 11 //
michael@0 12 // The first time this is used, it will create exampleSingleton as the result
michael@0 13 // of [[NSExampleType alloc] init]. Subsequent calls will return the same
michael@0 14 // NSExampleType* object. The object will be released by calling
michael@0 15 // -[NSExampleType release] when Singleton's atexit routines run
michael@0 16 // (see singleton.h).
michael@0 17 //
michael@0 18 // For Objective-C objects initialized through means other than the
michael@0 19 // no-parameter -init selector, DefaultSingletonObjCTraits may be extended
michael@0 20 // as needed:
michael@0 21 //
michael@0 22 // struct FooSingletonTraits : public DefaultSingletonObjCTraits<Foo> {
michael@0 23 // static Foo* New() {
michael@0 24 // return [[Foo alloc] initWithName:@"selecty"];
michael@0 25 // }
michael@0 26 // }
michael@0 27 // ...
michael@0 28 // Foo* widgetSingleton = SingletonObjC<Foo, FooSingletonTraits>::get();
michael@0 29
michael@0 30 #ifndef BASE_SINGLETON_OBJC_H_
michael@0 31 #define BASE_SINGLETON_OBJC_H_
michael@0 32
michael@0 33 #import <Foundation/Foundation.h>
michael@0 34 #include "base/singleton.h"
michael@0 35
michael@0 36 // Singleton traits usable to manage traditional Objective-C objects, which
michael@0 37 // are instantiated by sending |alloc| and |init| messages, and are deallocated
michael@0 38 // in a memory-managed environment when their retain counts drop to 0 by
michael@0 39 // sending |release| messages.
michael@0 40 template<typename Type>
michael@0 41 struct DefaultSingletonObjCTraits : public DefaultSingletonTraits<Type> {
michael@0 42 static Type* New() {
michael@0 43 return [[Type alloc] init];
michael@0 44 }
michael@0 45
michael@0 46 static void Delete(Type* object) {
michael@0 47 [object release];
michael@0 48 }
michael@0 49 };
michael@0 50
michael@0 51 // Exactly like Singleton, but without the DefaultSingletonObjCTraits as the
michael@0 52 // default trait class. This makes it straightforward for Objective-C++ code
michael@0 53 // to hold Objective-C objects as singletons.
michael@0 54 template<typename Type,
michael@0 55 typename Traits = DefaultSingletonObjCTraits<Type>,
michael@0 56 typename DifferentiatingType = Type>
michael@0 57 class SingletonObjC : public Singleton<Type, Traits, DifferentiatingType> {
michael@0 58 };
michael@0 59
michael@0 60 #endif // BASE_SINGLETON_OBJC_H_

mercurial