00001 /* 00002 * Integer.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Small integer (native long wrapper). 00006 * 00007 * Copyright (c) 2004 Milan Cermak, Martin Dvorak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <stlib/Integer.h> 00025 00026 #include <stlib/Array.h> 00027 #include <stlib/Real.h> 00028 #include <stlib/String.h> 00029 #include <stlib/Stream.h> 00030 00031 using namespace Core; 00032 00033 #define FLY_INSTANCES 512 00034 00035 Array Integer::_instances(FLY_INSTANCES); 00036 00037 Integer::Integer(long long val) 00038 { 00039 _value = val; 00040 } 00041 00042 /* Class-accessing protocol */ 00043 String *Integer::className(void) const 00044 { 00045 return new String("Integer"); 00046 } 00047 00048 /* Instance creation protocol */ 00049 Integer *Integer::value(long long val) 00050 { 00051 if (val >= 0 && val < FLY_INSTANCES) { 00052 Integer *inst = dynamic_cast<Integer *>(_instances.at(val)); 00053 if (inst == nil) { 00054 inst = new Integer(val); 00055 _instances.put(val, inst); 00056 } 00057 return inst; 00058 } else 00059 return new Integer(val); 00060 } 00061 00062 /* Converting protocol */ 00063 double Integer::asDouble(void) const 00064 { 00065 return (double) _value; 00066 } 00067 00068 long long Integer::asLongLong(void) const 00069 { 00070 return _value; 00071 } 00072 00073 /* Comparing protocol */ 00074 long Integer::hash(void) const 00075 { 00076 return (long) _value; 00077 } 00078 00079 bool Integer::isEqual(const Object *object) const 00080 { 00081 if (!object->isNumber()) return false; 00082 00083 return ((Number *) object)->isEqualToLongLong(_value); 00084 } 00085 00086 /* Double dispatching protocol */ 00087 bool Integer::isEqualToLongLong(long long arg) const 00088 { 00089 return _value == arg; 00090 } 00091 00092 bool Integer::isEqualToDouble(double arg) const 00093 { 00094 return _value == arg; 00095 } 00096 00097 /* Evaluating protocol */ 00098 Number* Integer::negate(void) 00099 { 00100 if (isZero()) return this; 00101 return Integer::value(-_value); 00102 } 00103 00104 /* Printing protocol */ 00105 void Integer::printOn(Stream *stream) const 00106 { 00107 char number[25]; 00108 sprintf(number, "%lld", _value); 00109 stream->nextPutAll(number); 00110 } 00111 00112 /* Testing protocol */ 00113 bool Integer::isInteger(void) const 00114 { 00115 return true; 00116 } 00117 00118 bool Integer::isPositive(void) const 00119 { 00120 return _value >= 0; 00121 } 00122 00123 bool Integer::isZero(void) const 00124 { 00125 return _value == 0; 00126 }