Shellp

shellinstance.h

00001 /*
00002     
00003   This file is a part of Shellp, a 'shell helper' library.
00004   
00005   Copyright (C) 2011-2012 Jori Liesenborgs
00006   
00007   Contact: jori.liesenborgs@gmail.com
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00022   USA
00023 
00024 */
00025 
00026 #ifndef SHELLP_SHELLINSTANCE_H
00027 
00028 #define SHELLP_SHELLINSTANCE_H
00029 
00030 #include "shellconfig.h"
00031 #include <jthread/jmutex.h>
00032 #include <errut/errorbase.h>
00033 #include <string>
00034 #include <vector>
00035 #include <list>
00036 #include <map>
00037 
00038 namespace mu
00039 {
00040         class Parser;
00041 }
00042 
00043 namespace shellp
00044 {
00045 
00046 class IOSystem;
00047 class ShellCommand;
00048 
00049 typedef double (*FunctionWith1Arg)(double x0);
00050 typedef double (*FunctionWith2Arg)(double x0, double x1);
00051 typedef double (*FunctionWith3Arg)(double x0, double x1, double x2);
00052 typedef double (*FunctionWith4Arg)(double x0, double x1, double x2, double x3);
00053 typedef double (*FunctionWith5Arg)(double x0, double x1, double x2, double x3, double x4);
00054 
00055 class SHELLP_IMPORTEXPORT ShellInstance : public errut::ErrorBase
00056 {
00057 public:
00058         ShellInstance();
00059         ~ShellInstance();
00060 
00061         static ShellInstance *getInstance();
00062 
00063         static int run(const std::string &title, const std::string &shellPrefix, int argc, char **argv);
00064 
00065         void setIOSystem(IOSystem *pSys)                                        { m_pIOSys = pSys; }
00066         void setInteractive(bool interactive)                                   { m_interactive = interactive; }
00067         void setRequestExit()                                                   { m_requestExit = true; }
00068         void setErrorExit()                                                     { m_errExit = true; m_requestExit = true; }
00069 
00070         IOSystem *getIOSystem()                                                 { return m_pIOSys; }
00071         bool isInteractive() const                                              { return m_interactive; }
00072         bool isExitRequested() const                                            { return m_requestExit; }
00073         bool isErrorExit() const                                                { return m_errExit; }
00074 
00075         bool evaluateExpression(const std::string &expression, double &value);
00076         bool storeVariable(const std::string &name, double value);
00077         bool processCommandLine(const std::vector<std::string> &args);
00078         void getParserNamesAndValues(std::vector<std::string> &constNames, std::vector<double> &constValues,
00079                                      std::vector<std::string> &varNames, std::vector<double> &varValues);
00080         void clearVariables();
00081 
00082         bool defineConstant(const std::string &name, double value);
00083         void clearConstants();
00084 
00085         bool defineFunction(const std::string &name, FunctionWith1Arg callBack);
00086         bool defineFunction(const std::string &name, FunctionWith2Arg callBack);
00087         bool defineFunction(const std::string &name, FunctionWith3Arg callBack);
00088         bool defineFunction(const std::string &name, FunctionWith4Arg callBack);
00089         bool defineFunction(const std::string &name, FunctionWith5Arg callBack);
00090         
00091         std::vector<std::string> getCommandNames() const;
00092         ShellCommand *getCommand(const std::string &commandName);
00093 
00094         void appendCommandLine(const std::string &line);
00095         std::vector<std::string> getCommandLines() const;
00096 
00097         virtual bool isSaved() const                                            { return true; }
00098 
00099         bool setInterrupted(bool f = true);
00100         bool isInterrupted();
00101 
00102         bool registerUnitName(int unitID, const std::string &name);
00103         bool setUnitMultiplier(int initID, double value);
00104         bool getUnitName(int unitID, std::string &name) const;
00105         double getUnitMultiplier(int unitID) const;
00106 
00107         bool getPreprocessorVariableContent(const std::string &name, std::string &contents) const;
00108         bool registerPreprocessorVariable(const std::string &name, const std::string &contents);
00109         void getPreprocessorVariables(std::vector<std::string> &names) const;
00110         void clearPreprocessorVariables();
00111 protected:
00112         void unrecoverableError(const std::string &str);
00113 private:
00114         void registerCommand(ShellCommand *pCmd, const std::string &cmdName);
00115         void unregisterCommand(ShellCommand *pCmd);
00116 //      static mu::value_type* addParserVariable(const mu::char_type *a_szName, void *a_pUserData);
00117 
00118         class CommandName
00119         {
00120         public:
00121                 CommandName(ShellCommand *pCmd, const std::string &n)                   { m_pCmd = pCmd; m_name = n; }
00122                 std::string getName() const                                             { return m_name; }
00123                 ShellCommand *getCommand()                                              { return m_pCmd; }
00124         private:
00125                 ShellCommand *m_pCmd;
00126                 std::string m_name;
00127         };
00128 
00129         std::vector<CommandName> m_commandList;
00130         IOSystem *m_pIOSys;
00131         bool m_interactive, m_requestExit, m_errExit;
00132 
00133         mu::Parser *m_pMuParser;
00134         std::map<std::string, void *> m_variables;
00135 
00136         std::vector<std::string> m_commandLines;
00137         
00138         jthread::JMutex m_mutex;
00139         bool m_interrupted;
00140 
00141         static ShellInstance *m_pInstance;
00142 
00143         std::map<int, std::pair<std::string, double> > m_units;
00144         std::map<std::string, std::string> m_preprocessorVariables;
00145         
00146         friend class ShellCommand;      
00147 };
00148 
00149 } // end namespace
00150 
00151 #endif // SHELLP_SHELLINSTANCE_H
00152