Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // |
2 | // Copyright 2009-2010 Facebook |
||
3 | // |
||
4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
||
5 | // you may not use this file except in compliance with the License. |
||
6 | // You may obtain a copy of the License at |
||
7 | // |
||
8 | // http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | // |
||
10 | // Unless required by applicable law or agreed to in writing, software |
||
11 | // distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | // See the License for the specific language governing permissions and |
||
14 | // limitations under the License. |
||
15 | // |
||
16 | |||
17 | #import "TTGlobalCore.h" |
||
18 | |||
19 | #import <objc/runtime.h> |
||
20 | |||
21 | // No-ops for non-retaining objects. |
||
22 | static const void* TTRetainNoOp(CFAllocatorRef allocator, const void *value) { return value; } |
||
23 | static void TTReleaseNoOp(CFAllocatorRef allocator, const void *value) { } |
||
24 | |||
25 | |||
26 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
27 | NSMutableArray* TTCreateNonRetainingArray() { |
||
28 | CFArrayCallBacks callbacks = kCFTypeArrayCallBacks; |
||
29 | callbacks.retain = TTRetainNoOp; |
||
30 | callbacks.release = TTReleaseNoOp; |
||
31 | return (NSMutableArray*)CFArrayCreateMutable(nil, 0, &callbacks); |
||
32 | } |
||
33 | |||
34 | |||
35 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
36 | NSMutableDictionary* TTCreateNonRetainingDictionary() { |
||
37 | CFDictionaryKeyCallBacks keyCallbacks = kCFTypeDictionaryKeyCallBacks; |
||
38 | CFDictionaryValueCallBacks callbacks = kCFTypeDictionaryValueCallBacks; |
||
39 | callbacks.retain = TTRetainNoOp; |
||
40 | callbacks.release = TTReleaseNoOp; |
||
41 | return (NSMutableDictionary*)CFDictionaryCreateMutable(nil, 0, &keyCallbacks, &callbacks); |
||
42 | } |
||
43 | |||
44 | |||
45 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
46 | BOOL TTIsArrayWithItems(id object) { |
||
47 | return [object isKindOfClass:[NSArray class]] && [(NSArray*)object count] > 0; |
||
48 | } |
||
49 | |||
50 | |||
51 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
52 | BOOL TTIsSetWithItems(id object) { |
||
53 | return [object isKindOfClass:[NSSet class]] && [(NSSet*)object count] > 0; |
||
54 | } |
||
55 | |||
56 | |||
57 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
58 | BOOL TTIsStringWithAnyText(id object) { |
||
59 | return [object isKindOfClass:[NSString class]] && [(NSString*)object length] > 0; |
||
60 | } |
||
61 | |||
62 | |||
63 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
64 | void TTSwapMethods(Class cls, SEL originalSel, SEL newSel) { |
||
65 | Method originalMethod = class_getInstanceMethod(cls, originalSel); |
||
66 | Method newMethod = class_getInstanceMethod(cls, newSel); |
||
67 | method_exchangeImplementations(originalMethod, newMethod); |
||
68 | } |