Documentation for OpenFLUID 2.2.0
openfluid::utils::Process Class Reference

Detailed Description

Management of processes to launch external programs

Example using the run() method with program and arguments

openfluid::utils::Process P("/path/to/program",{"--version"});
P.run();
const auto OutLines = P.stdOutLines(); // output stream lines
const auto ErrLines = P.stdErrLines(); // error stream lines
Definition: Process.hpp:66

Example using the run() method with fine tuning of command configuration

Cmd.Program = "/path/to/program"; // program to execute
Cmd.Args = {"launch","--option=value"}; // program arguments
Cmd.setOutErrFiles("/path/to/files","program-run"); // enable redirection of output and error streams to files
Env.Vars["VAR1"] = "value1"; // add variable VAR1
Env.Vars["VAR2"] = "value2"; // add variable VAR2
P.run();
std::cout << "Exit code: " << P.getExitCode() << std::endl;
// Note : output and error streams are redirected to files, output and error lines are empty
Definition: Process.hpp:93
std::string Program
Definition: Process.hpp:97
void setOutErrFiles(const std::string &DirPath, const std::string &NameBase)
std::vector< std::string > Args
Definition: Process.hpp:102
Definition: Process.hpp:73
std::map< std::string, std::string > Vars
Definition: Process.hpp:78

Example using the execute() method

openfluid::utils::Process::Command Cmd = {.Program = "/path/to/program", .Args = {"--version"}};
static int execute(const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())

#include <utils/Process.hpp>

Classes

struct  Command
 
struct  Environment
 

Public Member Functions

 Process (const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
 
 Process (const Command &Cmd, const Environment &Env={})
 
bool run ()
 
int getExitCode () const
 
std::string getErrorMessage () const
 
const std::vector< std::string > & stdOutLines () const
 
const std::vector< std::string > & stdErrLines () const
 
const Environmentenvironment () const
 
const Commandcommand () const
 

Static Public Member Functions

static int execute (const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
 
static int execute (const Command &Cmd, const Environment &Env=Environment())
 
static int system (const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
 
static int system (const Command &Cmd, const Environment &Env=Environment())
 

Constructor & Destructor Documentation

◆ Process() [1/2]

openfluid::utils::Process::Process ( const std::string &  Program,
const std::vector< std::string > &  Args = {},
const Environment Env = Environment() 
)

Constructor using program and arguments to configure the process

Parameters
[in]ProgramPath of the program to execute.
[in]ArgsProgram arguments (empty by default)
[in]EnvExecution environment (parent environment by default)

◆ Process() [2/2]

openfluid::utils::Process::Process ( const Command Cmd,
const Environment Env = {} 
)

Constructor using command structure to configure the process

Parameters
[in]CmdCommand configuration
[in]EnvExecution environment (parent environment by default)

Member Function Documentation

◆ command()

const Command& openfluid::utils::Process::command ( ) const
inline

Accesses the command configuration used for the process

Returns
The command configuration

◆ environment()

const Environment& openfluid::utils::Process::environment ( ) const
inline

Accesses the execution environment used for the process

Returns
The execution environment

◆ execute() [1/2]

static int openfluid::utils::Process::execute ( const Command Cmd,
const Environment Env = Environment() 
)
static

Executes a process using the given command configuration and execution environment

Parameters
[in]CmdCommand configuration
[in]EnvExecution environment (parent environment by default)

◆ execute() [2/2]

static int openfluid::utils::Process::execute ( const std::string &  Program,
const std::vector< std::string > &  Args = {},
const Environment Env = Environment() 
)
static

Executes a process using the given program, arguments and execution environment

Parameters
[in]ProgramPath of the program to execute.
[in]ArgsProgram arguments (empty by default)
[in]EnvExecution environment (parent environment by default)

◆ getErrorMessage()

std::string openfluid::utils::Process::getErrorMessage ( ) const
inline

Gets the error message in case of execution problem

Returns
The error message. Do not confuse with exit code

◆ getExitCode()

int openfluid::utils::Process::getExitCode ( ) const
inline

Gets the exit code returned by the executed program when finished

Returns
The exit code

◆ run()

bool openfluid::utils::Process::run ( )

Runs the process

Returns
true if the process has been run, false otherwise. Do not confuse with exit code

◆ stdErrLines()

const std::vector<std::string>& openfluid::utils::Process::stdErrLines ( ) const
inline

Accesses to the lines of the standard error stream once the process have run. These lines set is empty if the redirection of the standard error stream has been enabled

Returns
The error stream lines

◆ stdOutLines()

const std::vector<std::string>& openfluid::utils::Process::stdOutLines ( ) const
inline

Accesses to the lines of the standard output stream once the process have run. These lines set is empty if the redirection of the standard output stream has been enabled

Returns
The output stream lines

◆ system() [1/2]

static int openfluid::utils::Process::system ( const Command Cmd,
const Environment Env = Environment() 
)
static

Executes a process using the given command configuration and execution environment with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.

Parameters
[in]CmdCommand configuration
[in]EnvExecution environment (parent environment by default)

◆ system() [2/2]

static int openfluid::utils::Process::system ( const std::string &  Program,
const std::vector< std::string > &  Args = {},
const Environment Env = Environment() 
)
static

Executes a process using the given program, arguments and execution environment with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.

Parameters
[in]ProgramPath of the program to execute.
[in]ArgsProgram arguments (empty by default)
[in]EnvExecution environment (parent environment by default)

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