EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egspp.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ main program for egs++ applications
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2009
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_application.h"
38 #include "egs_library.h"
39 #include "egs_functions.h"
40 
41 #include <string>
42 #include <iostream>
43 using namespace std;
44 
45 #ifdef WIN32
46  const char fs = 92;
47 #else
48  const char fs = '/';
49 #endif
50 
51 typedef EGS_Application *(*createAppFunction)(int argc, char **argv);
52 
58 int main(int argc, char **argv) {
59 
60  string app_name;
61  if (!EGS_Application::getArgument(argc,argv,"-a","--application",app_name))
62  egsFatal("\nUsage: %s -a application -p pegs_file [-i input_file] [-o output_file] "
63  "[-b] [-P number_of_parallel_jobs] [-j job_index]\n\n",argv[0]);
64 
65  string lib_dir;
66  EGS_Application::checkEnvironmentVar(argc,argv,"-e","--egs-home","EGS_HOME",lib_dir);
67  lib_dir += "bin";
68  lib_dir += fs;
69  lib_dir += CONFIG_NAME;
70  lib_dir += fs;
71 
72  EGS_Library egs_lib(app_name.c_str(),lib_dir.c_str());
73  if (!egs_lib.load()) egsFatal("\n%s: Failed to load the %s application library from %s\n\n",
74  argv[0],app_name.c_str(),lib_dir.c_str());
75 
76 
77  createAppFunction createApp = (createAppFunction) egs_lib.resolve("createApplication");
78  if (!createApp) egsFatal("\n%s: Failed to resolve the address of the 'createApplication' function"
79  " in the application library %s\n\n",argv[0],egs_lib.libraryFile());
80 
81  EGS_Application *app = createApp(argc,argv);
82  if (!app) {
83  egsFatal("\n%s: Failed to construct the application %s\n\n",argv[0],app_name.c_str());
84  }
85 
86  int err = app->initSimulation();
87  if (err) {
88  return err;
89  }
90  err = app->runSimulation();
91  if (err < 0) {
92  return err;
93  }
94  err = app->finishSimulation();
95 
96  delete app;
97 
98  return err;
99 
100 }
101 
102 
A class for dynamically loading shared libraries.
Definition: egs_library.h:52
int main(int argc, char **argv)
A main program for egspp applications.
Definition: egspp.cpp:58
static bool getArgument(int &argc, char **argv, const char *name1, const char *name2, string &arg)
Finds a command line argument.
Global egspp functions header file.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
EGS_Library class header file.
EGS_Application class header file.
Base class for advanced EGSnrc C++ applications.
static void checkEnvironmentVar(int &argc, char **argv, const char *env, const char *n1, const char *n2, string &var)
Finds a command line argument.