KVUtils.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:24:33 2008 -0700 (2008-05-24)
changeset 11 e5976864dfe9
parent 3 8fad19466c59
permissions -rw-r--r--
* Added BSD license to more .m files.
* Updated some copyright notices.
* Added some #imports to make the MYNetwork classes build without using the MYUtilities.pch prefix header.
     1 //
     2 //  KVUtils.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 2/25/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "KVUtils.h"
    10 
    11 
    12 @implementation Observance
    13 
    14 - (id) initWithTarget: (id)target 
    15                action: (SEL)action
    16              observed: (id)observed
    17               keyPath: (NSString*)keyPath 
    18               options: (NSKeyValueObservingOptions)options
    19 {
    20     self = [super init];
    21     if (self != nil) {
    22         _target = target;
    23         _action = action;
    24         _observed = observed;
    25         _keyPath = [keyPath copy];
    26         _options = options;
    27         
    28         options &= ~(MYKeyValueObservingOptionOnce | MYKeyValueObservingOptionDelayed);
    29         
    30         [_observed addObserver: self forKeyPath: _keyPath options: options context: _action];
    31     }
    32     return self;
    33 }
    34 
    35 - (void) dealloc
    36 {
    37     [_observed removeObserver: self forKeyPath: _keyPath];
    38     [_keyPath release];
    39     [super dealloc];
    40 }
    41 
    42 
    43 @synthesize observed=_observed, keyPath=_keyPath;
    44 
    45 
    46 - (void) stopObserving
    47 {
    48     [_observed removeObserver: self forKeyPath: _keyPath];
    49     _observed = nil;
    50     _target = nil;
    51     _action = NULL;
    52 }
    53 
    54 
    55 - (void) _callTargetWithChange: (NSDictionary*)change
    56 {
    57     @try{
    58         [_target performSelector: _action withObject: _observed withObject: change];
    59     }@catch( NSException *x ) {
    60         Warn(@"Uncaught exception in -[%@<%p> %s] while observing change of key-path %@ in %@<%p>: %@",
    61              _target,_target, _action, _keyPath, _observed,_observed, x);
    62         [NSApp reportException: x];
    63     }
    64 }
    65 
    66 
    67 - (void)observeValueForKeyPath:(NSString *)keyPath 
    68                       ofObject:(id)object 
    69                         change:(NSDictionary *)change 
    70                        context:(void *)context
    71 {
    72     if( object == _observed ) {
    73         if( _options & MYKeyValueObservingOptionDelayed )
    74             [self performSelector: @selector(_callTargetWithChange:) withObject: change
    75                        afterDelay: 0.0];
    76         else
    77             [self _callTargetWithChange: change];
    78         if( _options & MYKeyValueObservingOptionOnce )
    79             [self stopObserving];
    80     }
    81 }
    82 
    83 
    84 @end
    85 
    86 
    87 
    88 
    89 @implementation Observer
    90 
    91 
    92 - (id) init
    93 {
    94     return [self initWithTarget: self];
    95 }
    96 
    97 
    98 
    99 - (id) initWithTarget: (id)target
   100 {
   101     Assert(target);
   102     self = [super init];
   103     if (self != nil) {
   104         _target = target;
   105         _observances = [[NSMutableArray alloc] init];
   106     }
   107     return self;
   108 }
   109 
   110 
   111 - (void) dealloc
   112 {
   113     [self stopObserving];
   114     [_observances release];
   115     [super dealloc];
   116 }
   117 
   118 
   119 @synthesize target=_target;
   120 
   121 
   122 - (void) observe: (id)observed 
   123          keyPath: (NSString*)keyPath 
   124          options: (NSKeyValueObservingOptions)options
   125           action: (SEL)action
   126 {
   127     Observance *o = [[Observance alloc] initWithTarget: _target
   128                                                 action: action
   129                                               observed: observed
   130                                                keyPath: keyPath
   131                                                options: options];
   132     [_observances addObject: o];
   133     [o release];
   134 }
   135 
   136 
   137 - (void) observe: (id)observed 
   138          keyPath: (NSString*)keyPath 
   139           action: (SEL)action
   140 {
   141     [self observe: observed keyPath: keyPath options: 0 action: action];
   142 }
   143 
   144 
   145 - (void) stopObserving
   146 {
   147     [_observances makeObjectsPerformSelector: @selector(stopObserving)];
   148     [_observances removeAllObjects];
   149 }
   150 
   151 
   152 - (void) stopObserving: (id)observed
   153 {
   154     [self stopObserving: observed keyPath: nil];
   155 }
   156 
   157 
   158 - (void) stopObserving: (id)observed keyPath: (NSString*)keyPath
   159 {
   160     // observed or keyPath may be nil
   161     for( int i=_observances.count-1; i>=0; i-- ) {
   162         Observance *o = [_observances objectAtIndex: i];
   163         if( (observed==nil || observed==o.observed) && (keyPath==nil || [keyPath isEqual: o.keyPath]) ) {
   164             [o stopObserving];
   165             [_observances removeObjectAtIndex: i];
   166         }
   167     }
   168 }
   169 
   170 @end
   171 
   172 
   173 /*
   174  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   175  
   176  Redistribution and use in source and binary forms, with or without modification, are permitted
   177  provided that the following conditions are met:
   178  
   179  * Redistributions of source code must retain the above copyright notice, this list of conditions
   180  and the following disclaimer.
   181  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   182  and the following disclaimer in the documentation and/or other materials provided with the
   183  distribution.
   184  
   185  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   186  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   187  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   188  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   189  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   190   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   191  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   192  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   193  */