2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "WOValueAssociation.h"
26 // TODO: check whether it makes sense to precalculate int/bool/?? values
27 // from the object value (add counters on how often these are called)
29 @implementation WOValueAssociation
32 return [super version] /* v2 */;
35 NSAssert2([super version] == 2,
36 @"invalid superclass (%@) version %i !",
37 NSStringFromClass([self superclass]), [super version]);
40 + (WOAssociation *)associationWithValue:(id)_value {
41 return [[[WOValueAssociation alloc] initWithValue:_value] autorelease];
45 return [self initWithValue:nil];
47 - (id)initWithValue:(id)_value {
48 if ((self = [super init])) {
49 self->value = [_value retain]; /* do not use copy, can be full objects */
53 - (id)initWithString:(NSString *)_s {
54 return [self initWithValue:_s];
58 [self->value release];
64 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
66 [NSException raise:@"AssociationException"
67 format:@"association value is not settable !"];
69 - (id)valueInComponent:(WOComponent *)_component {
73 - (BOOL)isValueConstant {
76 - (BOOL)isValueSettable {
82 - (unsigned int)unsignedIntValueInComponent:(WOComponent *)_component {
83 register unsigned int val;
85 if (self->cacheFlags.hasSmallValue)
86 return self->cacheFlags.smallValue;
88 val = [self->value unsignedIntValue];
89 if (self->cacheFlags.hasNoSmallValue)
93 self->cacheFlags.smallValue = val;
94 self->cacheFlags.hasSmallValue = 1;
97 self->cacheFlags.hasNoSmallValue = 1;
100 - (int)intValueInComponent:(WOComponent *)_component {
103 if (self->cacheFlags.hasSmallValue)
104 return self->cacheFlags.smallValue;
106 val = [self->value intValue];
107 if (self->cacheFlags.hasNoSmallValue)
110 if (val >= 0 && val < 65536) {
111 self->cacheFlags.smallValue = val;
112 self->cacheFlags.hasSmallValue = 1;
115 self->cacheFlags.hasNoSmallValue = 1;
118 - (BOOL)boolValueInComponent:(WOComponent *)_component {
119 switch (self->cacheFlags.boolValue) {
123 self->cacheFlags.boolValue = [self->value boolValue] ? 1 : 2;
124 return self->cacheFlags.boolValue == 1 ? YES : NO;
127 - (NSString *)stringValueInComponent:(WOComponent *)_component {
128 return [self->value stringValue];
133 - (void)encodeWithCoder:(NSCoder *)_coder {
134 [_coder encodeObject:self->value];
136 - (id)initWithCoder:(NSCoder *)_coder {
137 if ((self = [super init])) {
138 self->value = [[_coder decodeObject] retain];
145 - (id)copyWithZone:(NSZone *)_zone {
146 return [self retain];
151 - (NSString *)description {
152 NSMutableString *str;
154 str = [NSMutableString stringWithCapacity:64];
155 [str appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
156 [str appendString:@" value="];
157 if ([self->value isKindOfClass:[NSString class]]) {
158 NSString *v = self->value;
160 [str appendString:@"\""];
161 if ([self->value length] > 10) {
162 v = [v substringToIndex:9];
163 v = [v stringByApplyingCEscaping];
164 [str appendString:v];
165 [str appendFormat:@"...[len=%i]", [self->value length]];
168 v = [v stringByApplyingCEscaping];
169 [str appendString:v];
171 [str appendString:@"\""];
174 [str appendString:[self->value description]];
175 [str appendFormat:@"(%@)", NSStringFromClass([self->value class])];
177 [str appendString:@">"];
182 @end /* WOValueAssociation */
184 @implementation _WOBoolValueAssociation
186 static _WOBoolValueAssociation *yesAssoc = nil;
187 static _WOBoolValueAssociation *noAssoc = nil;
188 static NSNumber *yesNum = nil;
189 static NSNumber *noNum = nil;
192 if (yesNum == nil) yesNum = [[NSNumber numberWithBool:YES] retain];
193 if (noNum == nil) noNum = [[NSNumber numberWithBool:NO] retain];
196 + (WOAssociation *)associationWithBool:(BOOL)_value {
198 if (yesAssoc == nil) {
199 yesAssoc = [[_WOBoolValueAssociation alloc] init];
200 yesAssoc->value = YES;
205 if (noAssoc == nil) {
206 noAssoc = [[_WOBoolValueAssociation alloc] init];
212 + (WOAssociation *)associationWithValue:(id)_value {
213 return [self associationWithBool:[_value boolValue]];
218 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
220 [NSException raise:@"AssociationException"
221 format:@"association value is not settable !"];
223 - (id)valueInComponent:(WOComponent *)_component {
224 return self->value ? yesNum : noNum;
227 - (BOOL)isValueConstant {
230 - (BOOL)isValueSettable {
236 - (unsigned int)unsignedIntValueInComponent:(WOComponent *)_component {
237 return self->value ? 1 : 0;
239 - (int)intValueInComponent:(WOComponent *)_component {
240 return self->value ? 1 : 0;
242 - (BOOL)boolValueInComponent:(WOComponent *)_component {
245 - (NSString *)stringValueInComponent:(WOComponent *)_component {
246 return self->value ? @"YES" : @"NO";
251 - (id)copyWithZone:(NSZone *)_zone {
252 return [self retain];
257 - (NSString *)description {
258 return [NSString stringWithFormat:@"<%@[0x%08X]: %s",
259 NSStringFromClass([self class]), self,
260 self->value ? "YES" : "NO"];
263 @end /* _WOBoolValueAssociation */