Home | History | Annotate | Download | only in Framework
      1 //
      2 //  ACNumber.m
      3 //  ST4
      4 //
      5 //  Created by Alan Condit on 3/19/12.
      6 //  Copyright 2012 Alan Condit. All rights reserved.
      7 //
      8 
      9 #import "ACNumber.h"
     10 
     11 
     12 @implementation ACNumber
     13 
     14 + (ACNumber *)numberWithBool:(BOOL)aBool
     15 {
     16     return [[ACNumber alloc] initWithBool:aBool];
     17 }
     18 
     19 + (ACNumber *)numberWithChar:(char)aChar
     20 {
     21     return [[ACNumber alloc] initWithChar:aChar];
     22 }
     23 
     24 + (ACNumber *)numberWithDouble:(double)aDouble
     25 {
     26     return [[ACNumber alloc] initWithDouble:aDouble];
     27 }
     28 
     29 + (ACNumber *)numberWithInt:(NSInteger)anInt
     30 {
     31     return [[ACNumber alloc] initWithInteger:anInt];
     32 }
     33 
     34 + (ACNumber *)numberWithInteger:(NSInteger)anInt
     35 {
     36     return [[ACNumber alloc] initWithInteger:anInt];
     37 }
     38 
     39 
     40 - (id)init
     41 {
     42     self = [super init];
     43     if (self) {
     44         // Initialization code here.
     45     }
     46     
     47     return self;
     48 }
     49 
     50 - (ACNumber *)initWithBool:(BOOL)aBool
     51 {
     52     self = [super init];
     53     if ( self != nil ) {
     54         fBOOL = YES;
     55         fChar = NO;
     56         fDouble = NO;
     57         fNSInt = NO;
     58         u.b = aBool;
     59     }
     60     return self;
     61 }
     62 
     63 - (ACNumber *)initWithChar:(char)aChar
     64 {
     65     self = [super init];
     66     if ( self != nil ) {
     67         fBOOL = NO;
     68         fChar = YES;
     69         fDouble = NO;
     70         fNSInt = NO;
     71         u.c = aChar;
     72     }
     73     return self;
     74 }
     75 
     76 - (ACNumber *)initWithDouble:(double)aDouble
     77 {
     78     self = [super init];
     79     if ( self != nil ) {
     80         fBOOL = NO;
     81         fChar = NO;
     82         fDouble = YES;
     83         fNSInt = NO;
     84         u.d = aDouble;
     85     }
     86     return self;
     87 }
     88 
     89 - (ACNumber *)initWithInteger:(NSInteger)anInt
     90 {
     91     self = [super init];
     92     if ( self != nil ) {
     93         fBOOL = NO;
     94         fChar = NO;
     95         fDouble = NO;
     96         fNSInt = YES;
     97         u.i = anInt;
     98     }
     99     return self;
    100 }
    101 
    102 - (void)dealloc
    103 {
    104     [super dealloc];
    105 }
    106 
    107 - (BOOL)boolValue
    108 {
    109     if (fBOOL)
    110         return u.b;
    111     else
    112         return NO;
    113 }
    114 
    115 - (char)charValue
    116 {
    117     if (fChar)
    118         return u.c;
    119     else
    120         return (char)-1;
    121 }
    122 
    123 - (double)doubleValue
    124 {
    125     if (fDouble)
    126         return u.d;
    127     else
    128         return 0.0;
    129 }
    130 
    131 - (NSInteger)intValue
    132 {
    133     if (fNSInt)
    134         return u.i;
    135     else
    136         return -1;
    137 }
    138 
    139 - (NSInteger)integerValue
    140 {
    141     if (fNSInt)
    142         return u.i;
    143     else
    144         return -1;
    145 }
    146 
    147 - (NSInteger)inc
    148 {
    149     return (u.i+=1);
    150 }
    151 
    152 - (NSInteger)add:(NSInteger)anInt
    153 {
    154     return (u.i+=anInt);
    155 }
    156 
    157 - (NSString *)description
    158 {
    159     if (fBOOL)
    160         return (u.b == YES) ? @"true" : @"false"; 
    161     else if (fChar)
    162         return [NSString stringWithFormat:@"%c", u.c];
    163     else if (fNSInt)
    164         return [NSString stringWithFormat:@"%Ld", u.i];
    165     else if (fDouble)
    166         return [NSString stringWithFormat:@"%Lf", u.d];
    167     return @"ACNumber not valid";
    168 }
    169 
    170 @end
    171