ipc/chromium/src/base/scoped_nsobject.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) 2009 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 #ifndef BASE_SCOPED_NSOBJECT_H_
     6 #define BASE_SCOPED_NSOBJECT_H_
     8 #import <Foundation/Foundation.h>
     9 #include "base/basictypes.h"
    11 // scoped_nsobject<> is patterned after scoped_ptr<>, but maintains ownership
    12 // of an NSObject subclass object.  Style deviations here are solely for
    13 // compatibility with scoped_ptr<>'s interface, with which everyone is already
    14 // familiar.
    15 //
    16 // When scoped_nsobject<> takes ownership of an object (in the constructor or
    17 // in reset()), it takes over the caller's existing ownership claim.  The
    18 // caller must own the object it gives to scoped_nsobject<>, and relinquishes
    19 // an ownership claim to that object.  scoped_nsobject<> does not call
    20 // -retain.
    21 template<typename NST>
    22 class scoped_nsobject {
    23  public:
    24   typedef NST* element_type;
    26   explicit scoped_nsobject(NST* object = nil)
    27       : object_(object) {
    28   }
    30   ~scoped_nsobject() {
    31     [object_ release];
    32   }
    34   void reset(NST* object = nil) {
    35     [object_ release];
    36     object_ = object;
    37   }
    39   bool operator==(NST* that) const {
    40     return object_ == that;
    41   }
    43   bool operator!=(NST* that) const {
    44     return object_ != that;
    45   }
    47   operator NST*() const {
    48     return object_;
    49   }
    51   NST* get() const {
    52     return object_;
    53   }
    55   void swap(scoped_nsobject& that) {
    56     NST* temp = that.object_;
    57     that.object_ = object_;
    58     object_ = temp;
    59   }
    61   // scoped_nsobject<>::release() is like scoped_ptr<>::release.  It is NOT
    62   // a wrapper for [object_ release].  To force a scoped_nsobject<> object to
    63   // call [object_ release], use scoped_nsobject<>::reset().
    64   NST* release() {
    65     NST* temp = object_;
    66     object_ = nil;
    67     return temp;
    68   }
    70  private:
    71   NST* object_;
    73   DISALLOW_COPY_AND_ASSIGN(scoped_nsobject);
    74 };
    76 #endif  // BASE_SCOPED_NSOBJECT_H_

mercurial