BLCMDdo Class Reference

Inheritance diagram for BLCMDdo:

BLCommand

List of all members.


Detailed Description

class BLCMDdo implements the do command for loops

do i 1 10 [1] ... statements of the loop enddo

Sets i successively from 1 to 10, executing the statements of the loop. The increment is 1 by default; negative increments work as in FORTRAN.

NOTE: the enddo command is not a real command in that it is not a class. It is detected textually by this command class.

Public Member Functions

 BLCMDdo ()
 BLCMDdo (BLCMDdo &r)
G4String commandName ()
int command (BLArgumentVector &argv, BLArgumentMap &namedArgs)


Constructor & Destructor Documentation

BLCMDdo::BLCMDdo (  ) 

References BLCMDTYPE_CONTROL, BLCommand::registerCommand(), BLCommand::setDescription(), and BLCommand::setSynopsis().

00049 {
00050         registerCommand(BLCMDTYPE_CONTROL);
00051         setSynopsis("Do loop.");
00052         setDescription("Syntax:\n    do i 1 10 [1]\n        commands ...\n"
00053                 "    enddo\n\nSets the parameter i to "
00054                 "values 1, 2, 3, ... 10, and executes the commands. "
00055                 "The increment is 1 by default, and negative increments are "
00056                 "allowed (with limits reversed). 'do i 1 0' and 'do i 0 1 -1' "
00057                 "will execute no commands. After completion, i retains its "
00058                 "last value. Do-s and multi-line if-s can be "
00059                 "nested in any manner.\n\n"
00060                 "Note: the do command will not work from standard-input or "
00061                 "as a command in a single-line if.");
00062 }

BLCMDdo::BLCMDdo ( BLCMDdo r  )  [inline]

00039 : BLCommand() { }


Member Function Documentation

G4String BLCMDdo::commandName (  )  [inline, virtual]

Implements BLCommand.

00041 { return "do"; }

int BLCMDdo::command ( BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

Implements BLCommand.

References BLCommand::doCommand(), BLCommand::getNextCommand(), BLCommand::getPos(), BLCommandPos::isValid(), Param, BLCommand::printError(), BLParam::setParam(), and BLCommand::setPos().

00065 {
00066         if(argv.size() < 3 || argv.size() > 4) {
00067                 printError("Invalid do command");
00068                 return -1;
00069         }
00070 
00071         int i = atoi(argv[1].c_str());
00072         int n = atoi(argv[2].c_str());
00073         int incr = (argv.size() > 3 ? atoi(argv[3].c_str()) : 1);
00074 
00075         Param.setParam(argv[0],i);
00076 
00077         // special test to execute no commands
00078         if(incr>0 ? i > n : i < n) {
00079                 printf("do %s=%d,%d,%d -- no commands executed\n",
00080                                                 argv[0].c_str(),i,n,incr);
00081                 // skip to the matching enddo
00082                 int level = 1;
00083                 for(;;) {
00084                         G4String *line = BLCommand::getNextCommand(); // trimmed
00085                         if(!line) {
00086                                 printError("Unexpected EOF in do loop.");
00087                                 return -1;
00088                         }
00089                         if(line->find("enddo") == 0) {
00090                                 if(--level == 0)
00091                                         break;
00092                         } else if(line->find("do") == 0 &&
00093                                         line->find_first_of(" \t") == 2) {
00094                                 ++level;
00095                         }
00096                 }
00097                 return 0;
00098         }
00099 
00100         // get current pos so we can return to it each time through the loop
00101         BLCommandPos pos = BLCommand::getPos();
00102         if(!pos.isValid()) {
00103                 printError("do: cannot implement do loop on this input device.");
00104                 return -1;
00105         }
00106 
00107         printf("do %s=%d,%d,%d\n",argv[0].c_str(),i,n,incr);
00108 
00109         // here is the actual do loop
00110         while(incr>0 ? i <= n : i>= n) {
00111                 BLCommand::setPos(pos);
00112                 Param.setParam(argv[0],i);
00113                 printf("(do %s=%d)\n",argv[0].c_str(),i);
00114                 for(;;) {
00115                         G4String *line = BLCommand::getNextCommand(); // trimmed
00116                         if(!line) {
00117                                 printError("Unexpected EOF in do loop.");
00118                                 return -1;
00119                         }
00120                         if(line->find("enddo") == 0)
00121                                 break;
00122                         BLCommand::doCommand(*line);
00123                 }
00124                 i += incr;
00125         }
00126         printf("enddo\n");
00127         
00128         return 0;
00129 }


The documentation for this class was generated from the following file:
g4beamline