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.

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

mercurial