bbtkInterpreterVirtual.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkInterpreter.h,v $
00004   Language:  C++
00005   Date:      $Date: 2010/09/14 07:18:47 $
00006   Version:   $Revision: 1.41 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00040 #ifndef __bbtkInterpreterVirtual_h__
00041 #define __bbtkInterpreterVirtual_h__
00042 
00043 //#include "bbtkSystem.h"
00044 
00045 #include "bbtkMessageManager.h"
00046 #include "bbtkException.h"
00047 #include "bbtkObject.h"
00048 
00049 
00050 #include <fstream>
00051 #include <deque>
00052 
00053 // Signal/slot mechanism for 'break' commands
00054 #include <boost/signal.hpp>
00055 #include <boost/bind.hpp>
00056 
00057 namespace bbtk
00058 {
00059 
00060   
00061   //=======================================================================
00062   class BBTK_EXPORT InterpreterUser
00063   {
00064   public: 
00065     InterpreterUser() {}
00066     virtual ~InterpreterUser() {}
00067 
00068     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
00069     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
00070 
00071 
00072   };
00073   //=======================================================================
00074 
00075 
00076 
00077   //=======================================================================
00078   class BBTK_EXPORT InterpreterException : public Exception
00079   {
00080   public:
00081     InterpreterException( const std::string& message,
00082                       bool in_script_file,
00083                       const std::string& script_file,
00084                       int script_line 
00085                       );
00086     InterpreterException( const Exception& excep,
00087                       bool in_script_file,
00088                       const std::string& script_file,
00089                       int script_line 
00090                       );
00091     ~InterpreterException() throw() {}
00092 
00093     bool IsInScriptFile() const { return mInScriptFile; }
00094     const std::string& GetScriptFile() const { return mScriptFile; }
00095     int GetScriptLine() const { return mScriptLine; }
00096   private:
00097     bool mInScriptFile;
00098     std::string mScriptFile;
00099     int mScriptLine;
00100   };
00101   //=======================================================================
00102 
00103   /*
00104   //=======================================================================
00105   // The "Quit" exception
00106   class BBTK_EXPORT QuitException : public InterpreterError
00107   {
00108   public:
00109     QuitException( bool in_script_file,
00110                    const std::string& script_file,
00111                    int script_line 
00112                    )
00113       : InterpreterError("QUIT",in_script_file,script_file,script_line) 
00114     {}
00115     ~QuitException() throw() {}
00116   };
00117   //=======================================================================
00118   const std::string BREAK("BREAK");
00119   //=======================================================================
00120   // The "Break" exception
00121   class BBTK_EXPORT BreakException : public InterpreterError
00122   {
00123   public:
00124     BreakException( bool in_script_file,
00125                     std::string script_file,
00126                     int script_line 
00127                     )
00128       : InterpreterError(BREAK,in_script_file,script_file,script_line) 
00129     { std::cout << "BUILDING BREAK"<<std::endl; }
00130     ~BreakException() throw() {}
00131   };
00132   //=======================================================================
00133   */
00134 
00135   //=======================================================================
00136   class BBTK_EXPORT InterpreterVirtual : public Object
00137   {
00138     BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
00139     typedef Object Superclass;
00140   public:
00141           static Pointer New();
00142 
00143     typedef enum 
00144       {
00145         Interpreter_OK,
00146         Interpreter_ERROR,
00147         Interpreter_QUIT,
00148         Interpreter_BREAK,
00149       }
00150       ExitStatus;
00151 
00153     ExitStatus InterpretFile( const std::string& filename, bool source = false);
00154 
00156     ExitStatus InterpretBuffer( std::stringstream* buffer );
00157 
00159     ExitStatus InterpretLine( const std::string& line );
00160 
00162     ExitStatus InterpretCurrentStreams();
00163 
00165     void CommandLineInterpreter();
00166 
00167 
00168 
00170     void SetCommandLine(bool v = true) { mCommandLine = v; }
00171 
00172     void SetThrow(bool b) { mThrow = b; }
00173 
00174 #ifdef USE_WXWIDGETS
00176     void SetUser(InterpreterUser* c) { mUser = c; }
00178     InterpreterUser* GetUser() { return mUser; }
00180     const InterpreterUser* GetUser() const { return mUser; }
00181 
00182 #endif
00183 
00184 
00185     /*
00186     // For 'break' commands observation
00187     typedef boost::signals::trackable BreakObserverType;
00188     typedef boost::signal<void ()>  BreakSignalType;
00189     typedef BreakSignalType::slot_function_type BreakCallbackType;
00190   
00191     // Adds a callback when 'break' command issued
00192     void AddBreakObserver( BreakCallbackType );
00193     */
00194 
00195   protected:
00196 
00198     typedef enum
00199     {
00200       cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging 
00201       cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
00202       cNew,
00203       cDelete,
00204       cConnect,
00205       cExec,
00206       cPackage,
00207       cEndPackage,
00208       cDefine,
00209       cEndDefine,
00210       cInput,
00211       cOutput,
00212       cSet,
00213       cConfig,  // JPR
00214       cReset,   // EED
00215       cAuthor, 
00216       cCategory, // JPR
00217       cDescription,
00218       cHelp,
00219       cMessage,
00220       cInclude,
00221       cQuit,
00222       cLoad,
00223       cUnload,
00224       cGraph,
00225       cPrint,
00226       cIndex,
00227       cKind, // LG
00228       cNewGUI, // LG
00229       cWorkspace, // LG
00230       cDebug // LG
00231     } CommandCodeType;
00232 
00234     typedef struct 
00235     {
00236       std::string keyword;
00237       int argmin, argmax;
00238       CommandCodeType code;
00239       std::string syntax;
00240       std::string help;
00241     } CommandInfoType;
00242 
00244     typedef std::map<std::string,CommandInfoType> CommandDictType;
00245 
00246 
00248     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
00249 
00251     void GetLineFromPrompt( std::string& line );
00252 
00254     void SplitLine ( const std::string& line,
00255                      std::vector<std::string>& words );
00256 
00258     void InterpretCommand( const std::vector<std::string>& words,
00259                            CommandInfoType& info );
00260 
00262     void SwitchToFile( const std::string& filename, bool source = false );
00263 
00265     void SwitchToStream( std::stringstream* stream );
00266 
00268     void CloseCurrentFile();
00269 
00271     void CloseAllFiles();
00272 
00274     virtual void commandReset();
00275 
00277     virtual void commandHelp(const std::string &words);
00278     virtual void commandHelp(const std::vector<std::string>& words);
00279 
00281     virtual void commandGraph(const std::vector<std::string>& words);
00282 
00284     virtual void commandConfig() const; 
00286     //    void Print(const std::string&);
00287 
00288     void commandIndex(const std::string& filename,const std::string& type = "Initials");
00290     void FindCommandsWithPrefix( char* buf,
00291                                  int n,
00292                                  std::vector<std::string>& commands );
00293 
00299       virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
00300       virtual void commandDebug(const std::string& arg);
00301           virtual void commandNew(const std::string &boxType, const std::string &boxName);
00302           virtual void commandDelete(const std::string &boxName);
00303           virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
00304           virtual void commandPackage(const std::string &packageName);
00305           virtual void commandEndPackage();
00306           virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
00307           virtual void commandEndDefine();
00308           virtual void commandKind(const std::string &kind);
00309           virtual void commandPrint(const std::string &value);
00310           virtual void commandExec(const std::string &word);
00311           virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string  &help);
00312           virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string  &help);
00313           virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
00314           virtual void commandAuthor(const std::string &author);
00315           virtual void commandCategory(const std::string &categorytype);
00316           virtual void commandDescription(const std::string &description);
00317           virtual void commandClear();
00318           virtual void commandInclude(const std::string &word, bool ok);
00319           virtual void commandLoad(const std::string &packageName);
00320           virtual void commandUnload(const std::string &packageName);
00321           virtual void commandBreak();
00322           virtual void commandQuit();
00323           virtual void commandMessage();
00324           virtual void commandMessage(const std::string &kind,const std::string &levelstr);
00325           virtual void SetCurrentFileName(const std::string &fullPathScriptName);
00326 
00328           InterpreterVirtual();
00329           
00330 
00331           void Init();
00332 
00333           
00335           std::vector<std::string> mFileName;
00336 
00337           
00339           std::vector<int> mLine;
00340           
00341           bool mThrow;
00342 
00344           bool mCommandLine;
00345 
00346           
00350           std::vector<std::string> mFileNameHistory;
00351 
00353           bbtk::InterpreterUser* mUser;
00354   
00356           CommandDictType mCommandDict;
00357           
00358           
00359   private:
00360 
00361 
00362 
00365     void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
00366 
00367   
00368     //==================================================================
00369     // ATTRIBUTES
00370 
00371 
00372 
00374     std::vector<std::istream*> mFile;
00375 
00376 
00378     std::vector<std::string> mIncludeFileName;
00379 
00380 
00382     bool mInsideComment;
00383     
00385     ExitStatus mStatus;
00386 
00388     std::deque< char* > mHistory;
00389 
00390     int bufferNb;
00391     
00392 
00394     //    BreakSignalType mBreakSignal;
00395 
00396   protected:
00397           ~InterpreterVirtual();
00398 
00400           virtual void CatchInterpreterException( const InterpreterException& e );
00401           virtual void CatchBbtkException( const bbtk::Exception& e );
00402           virtual void CatchStdException( const std::exception& e );
00403           virtual void CatchUnknownException();
00404           
00405           
00406   };
00407   // EO class Interpreter
00408 
00409 
00410 
00411 }
00412 #endif

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1