BLParam.hh

Go to the documentation of this file.
00001 //      Param.hh
00002 /*
00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com
00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved.
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 http://www.gnu.org/copyleft/gpl.html
00017 */
00018 
00019 #ifndef BLPARAM_HH
00020 #define BLPARAM_HH
00021 
00022 #include <math.h>
00023 #include "globals.hh"
00024 #include "G4Types.hh"
00025 #include "BLCommand.hh"
00026 
00027 /**     BLParam - parameter class
00028  *
00029  *      The global object Param is used to access parameter values defined
00030  *      via the param command. The param command and the setParam()
00031  *      functions create parameter name/value pairs known to the Param object.
00032  *
00033  *      The BLSetParam class can be used to set initial values of parameters.
00034  *
00035  *      The internal representation of every value is a G4String, but that
00036  *      can be converted to double or int via get...() functions.
00037  *
00038  *      NOTE: the get*() functions are intended to be used during 
00039  *      initialization, not during tracking. They perform a format conversion
00040  *      and can be expensive -- a single call to getDouble() in
00041  *      UserSteppingAction() slowed tracking down by MORE THAN A FACTOR OF TEN!
00042  *
00043  *      NOTE: static and global constructors CAN use Param; this is
00044  *      explicitly allowed because init() guarantees initialization order.
00045  **/
00046 class BLParam {
00047         static std::map<G4String,G4String> *paramMap;
00048         static std::map<G4String,G4String> *paramHelpText;
00049         static void init();
00050 public:
00051         /// Constructor. 
00052         BLParam() { init(); }
00053 
00054         /// printParam() will display all defined parameters on stdout.
00055         void printParam();
00056 
00057         /// getString() returns the G4String value of a parameter.
00058         /// If name is not defined as a parameter, the environment is searched.
00059         /// Returns "" for unknown parameters, and displays the error on stdout.
00060         /// DO NOT USE DURING TRACKING!
00061         G4String getString(G4String name);
00062 
00063         /// getDouble() returns the G4double value of a parameter.
00064         /// returns -HUGE_VAL for unknown or invalid-format parameters,
00065         /// and displays the error on stdout.
00066         /// DO NOT USE DURING TRACKING!
00067         G4double getDouble(G4String name);
00068 
00069         /// getInt() returns the G4int value of a parameter.
00070         /// returns 0x80000000 for unknown or invalid-format parameters, and
00071         /// displays the error on stdout.
00072         /// DO NOT USE DURING TRACKING!
00073         G4int getInt(G4String name);
00074 
00075         /// setParam() sets the value of a parameter.
00076         /// DO NOT USE DURING TRACKING!
00077         void setParam(G4String name, G4String value);
00078 
00079         /// setParam() sets the value of a parameter.
00080         /// DO NOT USE DURING TRACKING!
00081         void setParam(G4String name, G4double value);
00082 
00083         /// setParam() sets the value of a parameter.
00084         /// DO NOT USE DURING TRACKING!
00085         void setParam(G4String name, G4int value);
00086 
00087         /// isDefined() returns true if the name has a defined value.
00088         bool isDefined(G4String name);
00089 
00090         /// expand() will expand every $name in str with parameter values.
00091         /// DO NOT USE DURING TRACKING!
00092         G4String expand(G4String str);
00093 
00094         /// setHelpText() will set help text for a parameter
00095         /// DO NOT USE DURING TRACKING!
00096         void setHelpText(G4String name, G4String text);
00097 
00098         /// getHelpText() returns the (complete) help text string.
00099         /// DO NOT USE DURING TRACKING!
00100         G4String getHelpText();
00101 };
00102 
00103 extern BLParam Param;   /// the global Param object
00104 
00105 /**     class BLSetParam -- Parameter initialization class
00106  *
00107  *      Classes that use Parameters should declare a static instance of
00108  *      this class to provide a default value for each such parameter.
00109  *      Take care that the value is of the proper format.
00110  *
00111  *      These values are set initally, so "param -unset" will not set them.
00112  **/
00113 class BLSetParam {
00114 public:
00115         BLSetParam(G4String name, G4String value, G4String helpText="")
00116                 { Param.setParam(name,value); 
00117                   if(helpText.size() > 0) Param.setHelpText(name,helpText);
00118                 }
00119 };
00120 
00121 #endif // BLPARAM_HH
g4beamline