00001 /* 00002 * Application.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Abstract application. 00006 * 00007 * Copyright (c) 2004 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/tools/Application.h> 00025 00026 #include <stdlib.h> 00027 00028 #include <stlib/Character.h> 00029 #include <stlib/EncodedStream.h> 00030 #include <stlib/ExternalEncodedStreamFactory.h> 00031 #include <stlib/FileAccessor.h> 00032 #include <stlib/GenericException.h> 00033 #include <stlib/tools/CmdLineParserError.h> 00034 00035 using namespace Tools; 00036 00037 Application *Application::_current = nil; 00038 00039 Application::Application(void) 00040 { 00041 _current = this; 00042 cin = (new FileAccessor(0))->withEncoding("default")->readStream(); 00043 cout = (new FileAccessor(1))->withEncoding("default")->writeStream(); 00044 dynamic_cast<EncodedStream *>(cout)->replacementCharacter(Character::value('?')); 00045 cerr = (new FileAccessor(2))->withEncoding("default")->writeStream(); 00046 dynamic_cast<EncodedStream *>(cerr)->replacementCharacter(Character::value('?')); 00047 } 00048 00049 Application::~Application(void) 00050 { 00051 _current = nil; 00052 } 00053 00054 /* Class-accessing protocol */ 00055 Application *Application::current(void) 00056 { 00057 return _current; 00058 } 00059 00060 /* Initialize/release protocol */ 00061 void Application::initialize(void) 00062 { 00063 /* Nothing */ 00064 } 00065 00066 void Application::initialize(int argc, char **argv) 00067 { 00068 /* Let the application initialize */ 00069 initialize(); 00070 /* Parse command line arguments */ 00071 CommandLineParser *parser = new CommandLineParser; 00072 try { 00073 initializeCommandLineParser(parser); 00074 parser->process(argc, argv); 00075 } 00076 catch (CommandLineParserError *ex) { 00077 cerr->print(ex); 00078 cerr->flush(); 00079 exit(1); 00080 } 00081 } 00082 00083 void Application::initializeCommandLineParser(CommandLineParser *parser) 00084 { 00085 /* nothing */ 00086 } 00087 00088 void Application::release(void) 00089 { 00090 cout->flush(); 00091 cerr->flush(); 00092 } 00093 00094 /* Error handling protocol */ 00095 void Application::defaultExceptionHandler(GenericException *ex) 00096 { 00097 ex->printOn(cerr); 00098 ex->printStackOn(cerr); 00099 cerr->flush(); 00100 } 00101 00102 /* Processing protocol */ 00103 void Application::run(void) 00104 { 00105 try { 00106 startUp(); 00107 execute(); 00108 shutDown(); 00109 } 00110 catch (GenericException *ex) { 00111 defaultExceptionHandler(ex); 00112 } 00113 }