Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mapplic.cc

00001 #define private public
00002 #include "magic/mobject.h"
00003 #include "magic/mapplic.h"
00004 #include "magic/mclass.h"
00005 
00006 #include <time.h>
00007 
00008 impl_dynamic (Application, {Object});
00009 
00010 /*
00011   Does not work with EGCS
00012   
00013 void classDump (const Object& obj) {
00014     const type_info& tir=typeid(obj);
00015     int nodetype = tir.__rtti_get_node_type ();
00016     if (nodetype != type_info::_RTTI_CLASS_TYPE) {
00017         serr.print ("Node type requested for a non-class object\n");
00018         return;
00019     }
00020     
00021     const __class_type_info& ci = (const __class_type_info&) tir;
00022     serr.printf ("class %s ", ci.name ());
00023     for (int i=0; i<ci.n_bases; i++) {
00024         const type_info& clbase = *ci.base_list[i];
00025         serr.printf (": %s ", clbase.name());
00026     }
00027     serr.print ("\n");
00028 }
00029 */
00030 
00032 //                                                                          //
00033 //               _             | o                 o                        //
00034 //              / \   --   --  |    ___   ___   |           _               //
00035 //             /   \ |  ) |  ) | | |   \  ___| -+- |  __  |/ \              //
00036 //             |---| |--  |--  | | |     (   |  |  | /  \ |   |             //
00037 //             |   | |    |    | |  \__/  \__|   \ | \__/ |   |             //
00038 //                                                                          //
00040 
00041 void myterminate () {
00042     printf ("%%ERR: Program terminated for unspecified reason\n"
00043             "%%ERR: This is possibly due to a bad exception\n"
00044             "%%ERR: or some other internal error\n");
00045     exit (1);
00046 }
00047 
00048 void myunexpected () {
00049     printf ("%%ERR: Bad exception was thrown. Exiting...\n");
00050     exit (1);
00051 }
00052 
00053 // Defined in mobject.cc
00054 extern int newtrace_disabled;
00055 
00056 int main (int argc, char** argv) {
00057     set_terminate (myterminate);
00058     set_unexpected (myunexpected);
00059 
00060     randomize;
00061 
00062 #ifndef DISABLE_ALL_MEMORY_DEBUGGING
00063     // Don't trace allocations before this point, since we DON'T want
00064     // to trace global objects, like class and module objects
00065     newtrace_disabled = false;
00066 #endif
00067     
00068     if (appclass) {
00069         // Create instance of the (user-inherited) application class
00070         newComment ("Application instance");
00071         Application* app = static_cast <Application*> (
00072             &(ClassLib::getinstance (appclass->getname())));
00073 
00074         // If such was found...
00075         if (app) {
00076             app->setParams (argc, argv);
00077 
00078             // Run any one of it's main-methods
00079             try {
00080                 app->main (app->params());
00081                 if (app->getTried()==1)
00082                     app->main ();
00083             } catch (assertion_failed e) {
00084                 // Catch any uncaught exceptions
00085                 fprintf (stderr, "%%EXCEPTION: Caught at main level\n %s\n",
00086                          (CONSTR) e.what());
00087                 exit (1);
00088             } catch (exception e) {
00089                 fprintf (stderr,
00090                          "%%exception: Caught at main level\n %s\n", e.what());
00091                 exit (1);
00092             } catch (Exception e) {
00093                 fprintf (stderr,
00094                          "%%Exception: Caught at main level\n %s\n", (CONSTR) e.what());
00095                 exit (1);
00096             }
00097             delete app;
00098         } else {
00099             // No user-defined Application inheritor was found
00100             serr.printf ("%%ERR: Internal error in Application "
00101                          "class creation\n"
00102                          "%%ERR: An Application class was found "
00103                          "with name '%s',\n"
00104                          "%%ERR: but it was not possible to get an instance "
00105                          "for it\n",
00106                          (CONSTR) appclass->getname());
00107             // classDump (*appclass);
00108         }
00109     } else {
00110         fprintf (stderr, "%%ERR: No Application decendant found\n");
00111     }
00112 
00113 #ifndef DISABLE_ALL_MEMORY_DEBUGGING
00114     // Check for memory leaks
00115     if (dbgnewmalloced>0) {
00116         fprintf (stderr, "%%ERR: Detected memory leaks!!!!\n");
00117         list_allocations ();
00118     }
00119 #endif
00120 
00121     return 0;
00122 }
00123 
00124 void Application::setParams (int argc, char** argv) {
00125     mArgc = argc;
00126     mArgv = argv;
00127 
00128     // Move the parameters to String array
00129     
00130     Array<String>   params;
00131     if (argc>1)
00132         params.resize (argc-1);
00133     
00134     for (int i=0; i<params.size; i++)
00135         params [i] = argv[i+1];
00136 
00137     // Parse any parameters of the format "-xxxx=yyyyy" into a parameter map
00138     StringMap paramMap;
00139     for (int p=0; p<params.size; p++)
00140         if (params[p].length()>1 && params[p][0]=='-') {
00141             String prm2 = params[p].mid(1);
00142             Array<String> lr;
00143             prm2.split (lr, '=');
00144             if (lr.size==1) { // A normal flag - not handled here
00145                 mParams.add (params[p]); // (Same as below)
00146             } else if (lr.size>2) { // Multiple =-signs are not allowed
00147                 serr.printf ("Parameters must follow format: -param=value\n");
00148                 exit (1);
00149             } else // Add to parameter map
00150                 mParamMap.set (lr[0],lr[1]);
00151         } else
00152             mParams.add (params[p]);
00153 }
00154 
00155 void Application::readConfig (const String& filename) {
00156     StringMap paramMap = readStringMap (filename);
00157 
00158     // Then add the command-line parameters on top of those
00159     paramMap += mParamMap;
00160 
00161     mParamMap = paramMap;
00162     
00163     // Parameters are from now on REQUIRED
00164     mParamMap.failByThrow ();
00165 }
00166 
00171 

Generated at Tue Dec 4 19:53:24 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001