00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2010 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef INTERPRETERUTILITIES_H 00011 #define INTERPRETERUTILITIES_H 00012 00013 #include "Slot.h" 00014 00015 #include <string> 00016 #include <vector> 00017 00018 class Interpreter; 00019 class Progress; 00020 00021 /** 00022 * Utility functions to make executing commands with an given Interpreter 00023 * implementation easier. 00024 */ 00025 namespace InterpreterUtilities 00026 { 00027 /** 00028 * Executes the provided statement(s) with the given interpreter while 00029 * reporting output and error text from command execution. 00030 * This function blocks and will not return until all provided 00031 * statements have been executed to completion. 00032 * 00033 * Variables created while executing the provided command can be 00034 * created in a nested scope that is closed at command completion 00035 * causing local variables to be freed. 00036 * A given interpreter implementation can support 00037 * this nested scope behavior or can share the global and persistent 00038 * scope used by Interpreter::executeCommand(). 00039 * 00040 * @param interpreterName 00041 * The name of the interpreter to execute commands with. 00042 * This should correspond to PlugIn::getName() of the given 00043 * InterpreterManager instance. 00044 * 00045 * @param command 00046 * The command to execute. This may be a single statement 00047 * or multiple statements. The syntax for single statements and 00048 * multi-statements is defined by the given interpreter 00049 * implementation. 00050 * 00051 * @param output 00052 * This Slot will be called as output text is generated from 00053 * executing this command. The boost::any provided to the 00054 * slot will contain a std::string with the output text. 00055 * 00056 * @param error 00057 * This Slot will be called as error text is generated from 00058 * executing this command. The boost::any provided to the 00059 * slot will contain a std::string with the error text. 00060 * 00061 * @param pProgress 00062 * If a non-NULL progress is provided, the execution of the provided 00063 * command may report progress to this instance. A given 00064 * interpreter implementation is permitted to ignore this parameter. 00065 * An interpreter will only report progress to this progress instance 00066 * while executing the provided command. 00067 * 00068 * @return \c False if a InterpreterManager instance with the given name cannot 00069 * not be located or is not already started. Returns \c false if there 00070 * was a syntatic problem with the given command, or an exception. 00071 * was thrown from the interpreter. \c true will be returned otherwise. 00072 * 00073 * @see Interpreter::executeScopedCommand() 00074 */ 00075 bool executeScopedCommand(const std::string& interpreterName, const std::string& command, 00076 const Slot& output, const Slot& error, Progress* pProgress); 00077 00078 /** 00079 * Executes the provided statement(s) with the given interpreter while 00080 * reporting output and error text from command execution. 00081 * This function blocks and will not return until all provided 00082 * statements have been executed to completion. 00083 * 00084 * Variables created while executing the provided command can be 00085 * created in a nested scope that is closed at command completion 00086 * causing local variables to be freed. 00087 * A given interpreter implementation can support 00088 * this nested scope behavior or can share the global and persistent 00089 * scope used by Interpreter::executeCommand(). 00090 * 00091 * @param interpreterName 00092 * The name of the interpreter to execute commands with. 00093 * This should correspond to PlugIn::getName() of the given 00094 * InterpreterManager instance. 00095 * 00096 * @param command 00097 * The command to execute. This may be a single statement 00098 * or multiple statements. The syntax for single statements and 00099 * multi-statements is defined by the given interpreter 00100 * implementation. 00101 * 00102 * @param outputAndError 00103 * The provided string will be populated with all of the 00104 * output and error text reported while executing the command. 00105 * 00106 * @param hasErrorText 00107 * The provided bool will be set to \c true if any error 00108 * text was reported while executing the command will be 00109 * set to \c false if only output text was reported. 00110 * 00111 * @param pProgress 00112 * If a non-NULL progress is provided, the execution of the provided 00113 * command may report progress to this instance. A given 00114 * interpreter implementation is permitted to ignore this parameter. 00115 * An interpreter will only report progress to this progress instance 00116 * while executing the provided command. 00117 * 00118 * @return \c False if a InterpreterManager instance with the given name cannot 00119 * not be located or is not already started. Returns \c false if there 00120 * was a syntatic problem with the given command, or an exception. 00121 * was thrown from the interpreter. \c true will be returned otherwise. 00122 * 00123 * @see Interpreter::executeScopedCommand() 00124 */ 00125 bool executeScopedCommand(const std::string& interpreterName, const std::string& command, 00126 std::string& outputAndError, bool& hasErrorText, Progress* pProgress); 00127 00128 /** 00129 * Queries whether an interpreter is available and has been started. 00130 * 00131 * This function will NOT start the interpreter, it must 00132 * have already been started using InterpreterManager::start() 00133 * for this function to return \c true. 00134 * 00135 * @param interpreterName 00136 * The name of the interpreter to execute commands with. 00137 * This should correspond to PlugIn::getName() of the given 00138 * InterpreterManager instance. 00139 * 00140 * @return \c True if an InterpreterManager instance with the given 00141 * name can be located and InterpreterManager::isStarted() 00142 * returns \c true. Otherwise \c false is returned. 00143 */ 00144 bool isInterpreterAvailable(const std::string& interpreterName); 00145 00146 /** 00147 * Returns the names of all Interpreter implementations (e.g. 00148 * all plug-ins that return PlugInManagerServices::InterpreterManagerType() 00149 * from PlugIn::getType() ). The implementations will be 00150 * added to the list even if InterpreterManager::isStarted() 00151 * returns \c false. 00152 * 00153 * @return The names of all Interpreter implementations. 00154 */ 00155 std::vector<std::string> getInterpreters(); 00156 00157 /** 00158 * Returns an Interpreter instance for the given name that can 00159 * be used to execute commands. 00160 * 00161 * @param interpreterName 00162 * The name of the interpreter to execute commands with. 00163 * This should correspond to PlugIn::getName() of the given 00164 * InterpreterManager instance. 00165 * 00166 * @return An Interpreter instance if isInterpreterAvailable() 00167 * would return \c true, otherwise \c NULL is returned. 00168 */ 00169 Interpreter* getInterpreter(const std::string& interpreterName); 00170 }; 00171 00172 #endif