Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Object.cc

Go to the documentation of this file.
00001 /*
00002  * Object.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Base class.
00006  *
00007  * Copyright (c) 2003 Milan Cermak
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/Object.h>
00025 #include <stlib/Character.h>
00026 #include <stlib/Collection.h>
00027 #include <stlib/String.h>
00028 #include <stlib/Stream.h>
00029 #include <stlib/Visitor.h>
00030 #include <stlib/WriteStream.h>
00031 
00032 #include <stlib/Error.h>
00033 #include <stlib/NotFoundError.h>
00034 
00035 #include <stdio.h>
00036 
00037 static long objectCounter = 0;
00038 
00039 Object::Object(void)
00040 {
00041     oid = objectCounter++;
00042 #if defined(DEBUG) && defined(VERBOSE)
00043     fprintf(stderr, "%ld created (%p)\n", oid, this);
00044 #endif
00045 }
00046 
00047 Object::~Object(void)
00048 {
00049 #if defined(DEBUG) && defined(VERBOSE)
00050     fprintf(stderr, "%ld destroyed (%p)\n", oid, this);
00051 #endif
00052 }
00053 
00054 /* Class-accessing protocol */
00055 String *Object::className(void) const
00056 {
00057     return new String("Object");
00058 }
00059 
00060 /* Error handling protocol */
00061 void Object::error(String *message, String *selector, Object *parameter)
00062 {
00063     Error *ex = new Error(message, selector, parameter);
00064     ex->raiseFrom(this);
00065 }
00066 
00067 void Object::shouldNotImplement(String *selector)
00068 {
00069     error(new String("Message is not appropriate for this object"), selector);
00070 }
00071 
00072 void Object::shouldNotImplement(const char *selector)
00073 {
00074     shouldNotImplement(new String(selector));
00075 }
00076 
00077 /* Comparing protocol */
00078 long Object::hash(void) const
00079 {
00080     return (long) this;
00081 }
00082 
00083 bool Object::isEqual(const Object *object) const
00084 {
00085     return isIdentical(object);
00086 }
00087 
00088 bool Object::isEqual(const Object &object) const
00089 {
00090     return isEqual(&object);
00091 }
00092 
00093 bool Object::isEqualToByteArray(const Object *object) const
00094 {
00095     return false;
00096 }
00097 
00098 bool Object::operator==(const Object &object) const
00099 {
00100     return isEqual(&object);
00101 }
00102 
00103 bool Object::isIdentical(const Object *object) const
00104 {
00105     return this == object;
00106 }
00107 
00108 bool Object::isIdentical(const Object &object) const
00109 {
00110     return isIdentical(&object);
00111 }
00112 
00113 /* Printing protocol */
00114 void Object::printOn(Stream *stream) const
00115 {
00116     String *title = className();   // self class printString
00117     if (((Character *) title->first())->isVowel())
00118         stream->nextPutAll("an ");
00119     else stream->nextPutAll("a ");
00120     stream->nextPutAll(title);
00121 }
00122 
00123 String *Object::printString(void) const
00124 {
00125     WriteStream stream(new String(16));
00126     printOn(&stream);
00127     return (String *) stream.contents();
00128 }
00129 
00130 /* Testing protocol */
00131 bool Object::isAssociation(void) const
00132 {
00133     return false;
00134 }
00135 
00136 bool Object::isBoolean(void) const
00137 {
00138     return false;
00139 }
00140 
00141 bool Object::isCharacter(void) const
00142 {
00143     return false;
00144 }
00145 
00146 bool Object::isNumber(void) const
00147 {
00148     return false;
00149 }
00150 
00151 bool Object::isString(void) const
00152 {
00153     return false;
00154 }
00155 
00156 bool Object::isInstanceOf(String *className)
00157 {
00158     return(className->isEqual(this->className()));
00159 }
00160 
00161 bool Object::isInstanceOf(const char* className)
00162 {
00163     return(this->isInstanceOf(new String(className)));
00164 }
00165 
00166 /* Visiting protocol */
00167 void Object::visitBy(Visitor *visitor)
00168 {
00169     visitor->visitObject(this);
00170 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2