|
| 1 | +/* |
| 2 | + Copyright (c) 2001-2011 by Marcel Weiher. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions |
| 6 | +are met: |
| 7 | +
|
| 8 | + Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in |
| 13 | + the documentation and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + Neither the name Marcel Weiher nor the names of contributors may |
| 16 | + be used to endorse or promote products derived from this software |
| 17 | + without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 29 | +THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +#ifndef ACCESSOR_MACROS |
| 34 | +#define ACCESSOR_MACROS |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +//--- This file expands the accessor macros |
| 39 | + |
| 40 | +#define newBool(x) (x ? [[NSNumber numberWithBool:x] retain] : nil) |
| 41 | +#define newInt(x) [[NSNumber numberWithInt:x] retain] |
| 42 | +#define newChar(x) [[NSNumber numberWithChar:x] retain] |
| 43 | +#define makeBool(x) (x ? [NSNumber numberWithBool:x] : nil) |
| 44 | +#define makeInt(x) [NSNumber numberWithInt:x] |
| 45 | +#define makeChar(x) [NSNumber numberWithChar:x] |
| 46 | +#define toInt(x) [x intValue] |
| 47 | +#define toBool(x) [x boolValue] |
| 48 | +//#define toString(x) [x stringValue] |
| 49 | + |
| 50 | +#define ASSIGN_ID(var,value)\ |
| 51 | + {\ |
| 52 | + id tempValue=(value);\ |
| 53 | + if ( tempValue!=var) { \ |
| 54 | + if ( tempValue!=(id)self ) \ |
| 55 | + [tempValue retain]; \ |
| 56 | + if ( var && var!=(id)self) \ |
| 57 | + [var release]; \ |
| 58 | + var = tempValue; \ |
| 59 | + } \ |
| 60 | + }\ |
| 61 | + |
| 62 | + |
| 63 | +#define setAccessor( type, var,setVar ) \ |
| 64 | +-(void)setVar:(type)newVar { \ |
| 65 | + ASSIGN_ID(var,newVar)\ |
| 66 | +} \ |
| 67 | + |
| 68 | +#define readAccessor( type, var )\ |
| 69 | +-(type)var { return var; } |
| 70 | + |
| 71 | +#define relayReadAccessor( var, delegate ) \ |
| 72 | +-var\ |
| 73 | +{\ |
| 74 | + if ( var ) {\ |
| 75 | + return var;\ |
| 76 | + } else {\ |
| 77 | + return [delegate var];\ |
| 78 | + }\ |
| 79 | +}\ |
| 80 | + |
| 81 | +#define objectAccessor( objectType, var, setVar ) \ |
| 82 | +readAccessor( objectType, var )\ |
| 83 | +setAccessor( objectType, var,setVar ) |
| 84 | + |
| 85 | +#define idAccessor( var, setVar ) objectAccessor( id, var, setVar ) |
| 86 | + |
| 87 | +#define relayAccessor( var, setVar, delegate )\ |
| 88 | +relayReadAccessor( var , delegate )\ |
| 89 | +setAccessor( id, var, setVar ) |
| 90 | + |
| 91 | +#define idAccessor_h( var,setVar ) -(void)setVar:newVar; \ |
| 92 | +-var; |
| 93 | + |
| 94 | +#define scalarAccessor( scalarType, var, setVar ) \ |
| 95 | +-(void)setVar:(scalarType)newVar { var=newVar; } \ |
| 96 | +-(scalarType)var { return var; } |
| 97 | +#define scalarAccessor_h( scalarType, var, setVar ) \ |
| 98 | +-(void)setVar:(scalarType)newVar; \ |
| 99 | +-(scalarType)var; |
| 100 | + |
| 101 | +#define objectAccessor_h( objectType, var, setVar ) scalarAccessor_h( objectType, var, setVar ) |
| 102 | + |
| 103 | +#define intAccessor( var, setVar ) scalarAccessor( int, var, setVar ) |
| 104 | +#define intAccessor_h( var, setVar ) scalarAccessor_h( int, var, setVar ) |
| 105 | +#define floatAccessor(var,setVar ) scalarAccessor( float, var, setVar ) |
| 106 | +#define floatAccessor_h(var,setVar ) scalarAccessor_h( float, var, setVar ) |
| 107 | +#define boolAccessor(var,setVar ) scalarAccessor( BOOL, var, setVar ) |
| 108 | +#define boolAccessor_h(var,setVar ) scalarAccessor_h( BOOL, var, setVar ) |
| 109 | + |
| 110 | +#define lazyAccessor( type, var ,setVar, computeVar ) \ |
| 111 | + readAccessorName( type,var, _##var ) \ |
| 112 | + setAccessor( type, var, setVar ) \ |
| 113 | +-(type)var { \ |
| 114 | + if ( ![self _##var] ) { \ |
| 115 | + [self setVar:[self computeVar]]; \ |
| 116 | + } \ |
| 117 | + return [self _##var]; \ |
| 118 | +} \ |
| 119 | + |
| 120 | + |
| 121 | +//--- compatibility: |
| 122 | + |
| 123 | +//#define accessor idAccessor |
| 124 | +//#define accessor_h idAccessor_h |
| 125 | + |
| 126 | + |
| 127 | +//---- RETAIN/RELEASE Macros for GNUStep compatibility |
| 128 | + |
| 129 | +#ifndef RETAIN |
| 130 | +#define RETAIN(x) ([(x) retain]) |
| 131 | +#endif |
| 132 | + |
| 133 | +#ifndef RELEASE |
| 134 | +#define RELEASE(x) ([(x) release]) |
| 135 | +#endif |
| 136 | + |
| 137 | +#ifndef AUTORELEASE |
| 138 | +#define AUTORELEASE(x) ([(x) autorelease]) |
| 139 | +#endif |
| 140 | + |
| 141 | +#ifndef DESTROY |
| 142 | +#define DESTROY(x) ([(x) release]) |
| 143 | +#endif |
| 144 | + |
| 145 | +#ifndef ASSIGN |
| 146 | +#define ASSIGN(var,value) ASSIGN_ID(var,value) |
| 147 | +#endif |
| 148 | + |
| 149 | +#ifndef ASSIGNCOPY |
| 150 | +#define ASSIGNCOPY(var,value) ASSIGN(var,[(value) copy]) |
| 151 | +#endif |
| 152 | + |
| 153 | +#endif |
0 commit comments