00001 /** @file tcl/scriptapp.h helper class used in main() to initialize 00002 and run a scripting application */ 00003 00004 /////////////////////////////////////////////////////////////////////// 00005 // 00006 // Copyright (c) 2005-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Mon Jun 27 13:23:46 2005 00010 // commit: $Id: scriptapp.h 11876 2009-10-22 15:53:06Z icore $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/scriptapp.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_TCL_SCRIPTAPP_H_UTC20050628162420_DEFINED 00035 #define GROOVX_TCL_SCRIPTAPP_H_UTC20050628162420_DEFINED 00036 00037 #include "rutz/fstring.h" 00038 #include "rutz/trace.h" 00039 00040 #include <exception> 00041 #include <tcl.h> 00042 00043 namespace tcl 00044 { 00045 class script_app; 00046 00047 struct package_info 00048 { 00049 const char* name; 00050 Tcl_PackageInitProc* init_proc; 00051 const char* version; 00052 bool requires_gui; 00053 }; 00054 } 00055 00056 /// Use inside main() to initialize and run a scripting application. 00057 class tcl::script_app 00058 { 00059 public: 00060 script_app(const char* appname_, int argc_, char** argv_) throw(); 00061 ~script_app() throw(); 00062 00063 /// Don't load any packages that require a windowing system. 00064 /** In particular, don't load Tk. */ 00065 void no_gui() { m_nowindow = true; } 00066 00067 /// Set a splash message to be shown at the beginning of run(). 00068 /** The message will shown only if the program is being run 00069 interactively (i.e. not from a script), and the message will be 00070 pretty-printed with line-wrapping. Also, empty lines in msg will 00071 be replaced with a line of hashes ("#####...etc."); to get an 00072 empty line with no hashes, just pass a line with some invisible 00073 whitespace (e.g. " \n"). */ 00074 void splash(const char* msg) { m_splashmsg = msg; } 00075 00076 /// Specify a directory that should be searched for pkg libraries. 00077 void pkg_dir(const char* dir) { m_pkgdir = dir; } 00078 00079 void packages(const package_info* pkgs_) { m_pkgs = pkgs_; } 00080 00081 void run(); 00082 00083 int exit_status() const { return m_exitcode; } 00084 00085 static void init_in_macro_only(); 00086 00087 static void handle_exception_in_macro_only(const std::exception* e); 00088 00089 private: 00090 script_app(const script_app&); // not implemented 00091 script_app& operator=(const script_app&); // not implemented 00092 00093 rutz::fstring const m_appname; 00094 int m_script_argc; 00095 char** m_script_argv; 00096 bool m_minimal; 00097 bool m_nowindow; 00098 rutz::fstring m_splashmsg; 00099 rutz::fstring m_pkgdir; 00100 const package_info* m_pkgs; 00101 int m_exitcode; 00102 }; 00103 00104 /// Call this macro at the beginning of main(). 00105 /** This will define a local variable of type script tcl::script_app, 00106 whose variable name is given by app. */ 00107 #define GVX_SCRIPT_PROG_BEGIN(app, name, argc, argv) \ 00108 try \ 00109 { \ 00110 GVX_TRACE(name); \ 00111 \ 00112 tcl::script_app::init_in_macro_only(); \ 00113 \ 00114 tcl::script_app app(name, argc, argv); 00115 00116 00117 /// Call this macro at the end of main(). 00118 #define GVX_SCRIPT_PROG_END(app) \ 00119 return app.exit_status(); \ 00120 } \ 00121 catch (std::exception& err) \ 00122 { \ 00123 tcl::script_app::handle_exception_in_macro_only(&err); \ 00124 } \ 00125 catch (...) \ 00126 { \ 00127 tcl::script_app::handle_exception_in_macro_only(0); \ 00128 } \ 00129 return -1; 00130 00131 static const char __attribute__((used)) vcid_groovx_tcl_scriptapp_h_utc20050628162420[] = "$Id: scriptapp.h 11876 2009-10-22 15:53:06Z icore $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/tcl/scriptapp.h $"; 00132 #endif // !GROOVX_TCL_SCRIPTAPP_H_UTC20050628162420_DEFINED