Wired Networking  2.0
Objective-C implementation of the Wired 2.0 protocol
NSNumber-WINetworking.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredNetworking/NSNumber-WINetworking.h>
00030 
00031 @implementation NSNumber(WINetworking)
00032 
00033 + (NSNumber *)numberWithWiredNumber:(wi_number_t *)number {
00034                 if(!number)
00035                                 return NULL;
00036                 
00037                 switch(wi_number_type(number)) {
00038                                 case WI_NUMBER_BOOL:
00039                                                 return [[[self alloc] initWithBool:wi_number_bool(number)] autorelease];
00040                                                 break;
00041 
00042                                 case WI_NUMBER_CHAR:
00043                                                 return [[[self alloc] initWithChar:wi_number_char(number)] autorelease];
00044                                                 break;
00045 
00046                                 case WI_NUMBER_SHORT:
00047                                                 return [[[self alloc] initWithShort:wi_number_short(number)] autorelease];
00048                                                 break;
00049 
00050                                 case WI_NUMBER_INT:
00051                                                 return [[[self alloc] initWithInt:wi_number_int(number)] autorelease];
00052                                                 break;
00053 
00054                                 case WI_NUMBER_INT8:
00055                                 case WI_NUMBER_INT16:
00056                                 case WI_NUMBER_INT32:
00057                                                 return [[[self alloc] initWithInt:wi_number_int32(number)] autorelease];
00058                                                 break;
00059 
00060                                 case WI_NUMBER_INT64:
00061                                                 return [[[self alloc] initWithLongLong:wi_number_int64(number)] autorelease];
00062                                                 break;
00063 
00064                                 case WI_NUMBER_LONG:
00065                                                 return [[[self alloc] initWithLong:wi_number_long(number)] autorelease];
00066                                                 break;
00067 
00068                                 case WI_NUMBER_LONG_LONG:
00069                                                 return [[[self alloc] initWithLongLong:wi_number_long_long(number)] autorelease];
00070                                                 break;
00071 
00072                                 case WI_NUMBER_FLOAT:
00073                                                 return [[[self alloc] initWithFloat:wi_number_float(number)] autorelease];
00074                                                 break;
00075 
00076                                 case WI_NUMBER_DOUBLE:
00077                                                 return [[[self alloc] initWithDouble:wi_number_double(number)] autorelease];
00078                                                 break;
00079                 }
00080                 
00081                 return NULL;
00082 }
00083 
00084 
00085 
00086 #pragma mark -
00087 
00088 - (wi_number_t *)wiredNumber {
00089                 const char                      *type;
00090                 
00091                 type = [self objCType];
00092                 
00093                 if(strcmp(type, @encode(BOOL)) == 0)
00094                                 return wi_number_with_bool([self boolValue]);
00095                 else if(strcmp(type, @encode(char)) == 0 || strcmp(type, @encode(unsigned char)) == 0)
00096                                 return wi_number_with_char([self charValue]);
00097                 else if(strcmp(type, @encode(short)) == 0 || strcmp(type, @encode(unsigned short)) == 0)
00098                                 return wi_number_with_short([self shortValue]);
00099                 else if(strcmp(type, @encode(int)) == 0 || strcmp(type, @encode(unsigned int)) == 0)
00100                                 return wi_number_with_int([self intValue]);
00101                 else if(strcmp(type, @encode(int32_t)) == 0 || strcmp(type, @encode(uint32_t)) == 0)
00102                                 return wi_number_with_int32([self intValue]);
00103                 else if(strcmp(type, @encode(int64_t)) == 0 || strcmp(type, @encode(uint64_t)) == 0)
00104                                 return wi_number_with_int64([self longLongValue]);
00105                 else if(strcmp(type, @encode(NSInteger)) == 0 || strcmp(type, @encode(NSUInteger)) == 0)
00106                                 return wi_number_with_integer([self integerValue]);
00107                 else if(strcmp(type, @encode(long)) == 0 || strcmp(type, @encode(unsigned long)) == 0)
00108                                 return wi_number_with_long([self longValue]);
00109                 else if(strcmp(type, @encode(long long)) == 0 || strcmp(type, @encode(unsigned long long)) == 0)
00110                                 return wi_number_with_long_long([self longLongValue]);
00111                 
00112                 return NULL;
00113 }
00114 
00115 @end
 All Classes