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

GenericException.cc

Go to the documentation of this file.
00001 /*
00002  * GenericException.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Abstract exception.
00006  *
00007  * Copyright (c) 2003-05 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 <stdio.h>
00025 #include <stdlib.h>
00026 #include <execinfo.h>
00027 
00028 #include <stlib/GenericException.h>
00029 #include <stlib/Array.h>
00030 #include <stlib/String.h>
00031 #include <stlib/Stream.h>
00032 #include <stlib/Visitor.h>
00033 
00034 //void *GenericException::method_stack[];
00035 //int GenericException::stack_size = 0;
00036 GenericException *GenericException::last_exception = nil;
00037 
00038 GenericException::GenericException(String *message, String *selector,
00039                                    Object *param)
00040 {
00041     messageText = message;
00042     _selector   = selector;
00043     _parameter  = param;
00044     _originator = nil;
00045     stack_size = 0;
00046 }
00047 
00048 GenericException::~GenericException(void)
00049 {
00050 }
00051 
00052 /* Class-accessing protocol */
00053 String *GenericException::className(void) const
00054 {
00055     return new String("GenericException");
00056 }
00057 
00058 GenericException *GenericException::lastException(void)
00059 {
00060     return last_exception;
00061 }
00062 
00063 /* Accessing protocol */
00064 String *GenericException::description(void)
00065 {
00066     if (messageText == NULL)
00067         return notifierString();
00068     return messageText;
00069 }
00070 
00071 const Object *GenericException::originator(void)
00072 {
00073     if (_originator == nil) return this;
00074                        else return _originator;
00075 }
00076 
00077 void GenericException::originator(const Object *object)
00078 {
00079     _originator = object;
00080 }
00081 
00082 Object *GenericException::parameter(void)
00083 {
00084     return _parameter;
00085 }
00086 
00087 void GenericException::selector(String *sel)
00088 {
00089     _selector = sel;
00090 }
00091 
00092 void GenericException::selector(const char *sel)
00093 {
00094     _selector = new String(sel);
00095 }
00096 
00097 String *GenericException::selector(void)
00098 {
00099     return _selector;
00100 }
00101 
00102 /* Printing protocol */
00103 void GenericException::printStackOn(Stream *stream)
00104 {
00105     if (method_stack == NULL || stack_size <= 1) {
00106         stream->nextPutAll("No stack.");
00107         return;
00108     }
00109 
00110     char **names;
00111     String *line, *module, *mangledName;
00112     int startMark, endMark;
00113 
00114     names = backtrace_symbols(method_stack, stack_size);
00115     for (int i = 1; i < stack_size; i++) {
00116         line = new String(names[i]);
00117         startMark = line->indexOf('(');
00118         if (startMark < 0) startMark = line->indexOf(' ');
00119         endMark = line->nextIndexOf('+', startMark, line->size());
00120         module = dynamic_cast<String *>(line->copy(0, startMark));
00121         mangledName = dynamic_cast<String *>(line->copy(startMark+1, endMark));
00122 
00123         stream->nextPutAll("    ");
00124         try {
00125             stream->nextPutAll(mangledName->demangleMethodName());
00126         }
00127         catch (GenericException *ex) {
00128             stream->nextPutAll(mangledName);
00129         }
00130         stream->nextPutAll(" in ");
00131         stream->nextPutAll(module);
00132         stream->lf();
00133     }
00134     free((void *) names);
00135 }
00136 
00137 /* Raising protocol */
00138 void GenericException::pass(void)
00139 {
00140     privRaise();
00141 }
00142 
00143 void GenericException::raiseFrom(const Object *origin)
00144 {
00145     originator(origin);
00146     raise();
00147 }
00148 
00149 void GenericException::raise(void)
00150 {
00151     getExecutionStack();
00152     privRaise();
00153 }
00154 
00155 /* Visiting protocol */
00156 void GenericException::visitBy(Visitor *visitor)
00157 {
00158     visitor->visitException(this);
00159 }
00160 
00161 /* Private protocol */
00162 void GenericException::getExecutionStack(void)
00163 {
00164 //    if (last_exception == nil)
00165 //      GC_exclude_static_roots(method_stack, method_stack + MAXSTACKSIZE);
00166     last_exception = this;
00167     stack_size = backtrace(method_stack, MAXSTACKSIZE);
00168 }

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