Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 97 lines (76 sloc) 4.527 kB
032c8b8 @MrNoodle - Bumped project to use 10.6.
authored
1 //
2 // NoodleBlockAction.h
3 // NoodleKit
4 //
5 // Created by Paul Kim on 6/30/10.
6 // Copyright 2010 Noodlesoft, LLC. All rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 // THE SOFTWARE.
25
26 #import <Cocoa/Cocoa.h>
27
28 #if defined(NS_BLOCKS_AVAILABLE) && (NS_BLOCKS_AVAILABLE == 1)
29
30 @class NoodleGlue;
31
32 typedef void (^NoodleGlueBlock)(NoodleGlue *glue, id object);
33 typedef void (^NoodleGlueCleanupBlock)(NoodleGlue *glue);
34
35 /*
36 In those cases where you need to pass some target object that will get some method called on it, instead of defining a
37 new method or class to handle it, just use a block and stuff it into one of these objects and pass this object along
38 instead.
39
40 Common cases are for notifications (though blocks can be used there directly, you can provide a cleanup block here
41 such that the object automatically unregisters itself from notifications when it is dealloc'ed/finalized). Can
42 also be used with timers or other places that take a target/action.
43
44 Things to be aware of:
45 - Most of the time, you probably don't want this object retaining any objects it references (think about how much
46 of the glue code you write operates). Use "__block" on any objects you don't want to be retained.
47 - You still need to memory manage this object yourself. There's no magic about it. If you set it as a notification
48 observer, you need to retain it somewhere because the notification center won't (or if using GC, keep a strong
49 reference somewhere).
50
5ec8ec7 @MrNoodle - Fixed framework install paths to use @rpath.
authored
51 For more details, check out the related blog post at http://www.noodlesoft.com/blog/2010/07/01/playing-with-nstimer/
032c8b8 @MrNoodle - Bumped project to use 10.6.
authored
52 */
53 @interface NoodleGlue : NSObject
54 {
55 NoodleGlueBlock _glueBlock;
56 NoodleGlueCleanupBlock _cleanupBlock;
57 }
58
5ec8ec7 @MrNoodle - Fixed framework install paths to use @rpath.
authored
59 @property (readwrite, copy) NoodleGlueBlock glueBlock;
60 @property (readwrite, copy) NoodleGlueCleanupBlock cleanupBlock;
61
032c8b8 @MrNoodle - Bumped project to use 10.6.
authored
62 + (NoodleGlue *)glueWithBlock:(NoodleGlueBlock)glueBlock;
63 + (NoodleGlue *)glueWithBlock:(NoodleGlueBlock)glueBlock cleanupBlock:(NoodleGlueCleanupBlock)cleanupBlock;
64
5ec8ec7 @MrNoodle - Fixed framework install paths to use @rpath.
authored
65 // Initializes a glue object. glueBlock will be invoked when this object's -invoke: method is called with the argument
66 // to -invoke: passed on as a parameter. cleanupBlock is invoked when this object is dealloc'ed/finalized with the
032c8b8 @MrNoodle - Bumped project to use 10.6.
authored
67 // glue object being dealloc'ed sent in as a parameter.
68 - (id)initWithBlock:(NoodleGlueBlock)glueBlock cleanupBlock:(NoodleGlueCleanupBlock)cleanupBlock;
69
70 // Invokes the main block. When using this in a target/selector situation, use this as the selector.
71 - (void)invoke:(id)object;
72
73 @end
74
5ec8ec7 @MrNoodle - Fixed framework install paths to use @rpath.
authored
75 /*
76 NSObject category which, through the use of NoodleGlue and associative references, allows you to assign a block
77 to be invoked when the object is deallocated.
78
79 This code is more proof of concept than anything you'd want to use in production. For one, it's not threadsafe.
80
81 For more details, check out the related blog post at http://www.noodlesoft.com/blog/2010/07/05/fun-with-glue/
82 */
83 @interface NSObject (NoodleCleanupGlue)
84
85 // Sets a block to be invoked when the object is deallocated/collected. Will return an identifier that you can
86 // use to remove the block later. Note that you need to retain this identifier if you intend to use it later.
87 // Also, treat the identifier as an opaque object. Its actual type/formatting/structure may change in future
88 // versions.
89 - (id)addCleanupBlock:(void (^)(id object))block;
90
91 // Removes the cleanup block using the identifier returned from a previous call to -addCleanupBlock:
92 - (void)removeCleanupBlock:(id)identifier;
93
94 @end
95
96
032c8b8 @MrNoodle - Bumped project to use 10.6.
authored
97 #endif
Something went wrong with that request. Please try again.