PCMSqVM.h

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00027 
00028 #ifndef __PCM_SQVM_H__
00029 #  define __PCM_SQVM_H__
00030 
00031 //
00032 // Inner includes
00033 //
00034 
00035 #  include "PCMPremeditations.h"
00036 
00037 #  include "PCMName.h"
00038 
00039 #  include "PCMLogger.h"
00040 
00041 #  include "PCMFileSystemManager.h"
00042 
00043 #  include "PCMException.h"
00044 
00045 #  include "PCMPointer.h"
00046 
00047 #  include "PCMContainer.h"
00048 
00049 //
00050 // Outer includes
00051 //
00052 
00053 #  include <squirrel/squirrel.h>
00054 
00055 #  include <squirrel/sqstdio.h>
00056 
00057 #  include <squirrel/sqstdaux.h>
00058 
00059 #  include <squirrel/sqplus.h>
00060 
00061 #  include <list>
00062 
00063 namespace ParCompMark
00064 {
00065 
00070   class SqVM:public Name
00071   {
00072 
00073         //
00074         // Unit test class
00075         //
00076 #  ifdef PARCOMPMARK_TEST
00077         friend class ParCompMarkTest::TestSqVM;
00078 #  endif
00079 
00080         //
00081         // Enumerations
00082         //
00083 
00084    public:
00085 
00087         enum ScriptOutput
00088         {
00089 
00091          LOG,
00092 
00094          STD
00095         };
00096 
00097    protected:
00098 
00100         enum ScriptState
00101         {
00102 
00104          UNCOMPILED,
00105 
00107          COMPILED,
00108 
00110          EXECUTED
00111         };
00112 
00113         //
00114         // Typedefs
00115         //
00116 
00117    public:
00118 
00120         typedef Pointer < SqVM,
00121         Mutex > Pointer;
00122 
00123    protected:
00124 
00126         typedef struct Script
00127 
00128         {                 
00130          bool dynamic;                                                                                           
00132           std::string name;                                                                                                                                                                              
00134           std::string scriptString;                                                                                                                                                                                                                                                                                      
00136          SquirrelObject scriptObject;                                                                                                                                                                                                                                                                                                                                              
00138          bool compiled;                                                                                                                                                                                                                                                                                                                                                                                                           
00140           std::string mainMethod;                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
00142          bool hasReturn;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
00144           std::string returnValue;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
00146          const SQChar *parameters[10];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
00148          u32 parameterCount;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
00149          typedef ParCompMark::Pointer < Script,
00150           DummyLock > Pointer;
00151         } Script;
00152 
00153         //
00154         // Class constants
00155         //
00156 
00158 
00159 
00160    public:
00161 
00167         static const std::string NOMAINMETHOD;
00168 
00169    protected:
00170 
00176         static const u32 mStringBufferSize;
00177 
00179 
00180         //
00181         // Class variables
00182         //
00183 
00185 
00186 
00187    protected:
00188 
00194         static SqVM::Pointer mCurrentVM;
00195 
00202         static Mutex mCurrentVMLock;
00203 
00209         static SqVM::ScriptOutput mScriptOutput;
00210 
00216         static char mStringBuffer[32768];
00217 
00219 
00220         //
00221         // Variables
00222         //
00223 
00225 
00226 
00227    protected:
00228 
00234         SqVM::Pointer mThis;
00235 
00241         SquirrelVMSys *mSquirrelVMSys;
00242 
00248         bool mError;
00249 
00255         bool mInitialized;
00256 
00262         Container < SqVM::Script, Mutex >::Pointer mScripts;
00263 
00265 
00266         //
00267         // Constructors & destructor
00268         //
00269 
00271 
00272 
00273    public:
00274 
00279         SqVM(const std::string & name);
00280 
00284         virtual ~ SqVM();
00285 
00287 
00288         //
00289         // Getters & setters
00290         //
00291 
00293 
00294 
00295    public:
00296 
00302         const bool & getError() const;
00303 
00309         void setError(const bool & error);
00310 
00316         const bool & getInitialized() const;
00317 
00323         static const SqVM::ScriptOutput & getScriptOutput();
00324 
00330         static void setScriptOutput(const SqVM::ScriptOutput & scriptoutput);
00331 
00337         static const u32 & getStringBufferSize();
00338 
00340 
00341         //
00342         // Class methods
00343         //
00344 
00346 
00347 
00348    protected:
00349 
00358         static void printFunction(::HSQUIRRELVM sqVM, const SQChar * s, ...);
00359 
00361 
00362         //
00363         // Methods
00364         //
00365 
00367 
00368 
00369    public:
00370 
00379         virtual std::string runScriptFromFile(const std::string & filename,
00380                                                 const std::string & mainMethod =
00381                                                 "main", const std::list < std::string > &parameters =
00382                                                 std::list < std::string > (), const bool & hasReturn = false);
00383 
00395         virtual std::string runScriptFromString(const std::string & scriptString,
00396                                                 const std::string & scriptName =
00397                                                 "", const std::string & mainMethod =
00398                                                 "main", const std::list < std::string > &parameters =
00399                                                 std::list < std::string > (), const bool & hasReturn = false);
00400 
00407         virtual std::string runScriptByName(const std::string & scriptName,
00408                                          const std::list < std::string > &parameters =
00409                                          std::list < std::string > ());
00410 
00414         virtual void finalize();
00415 
00416    protected:
00417 
00421         virtual void activate();
00422 
00426         virtual void deactivate();
00427 
00431         virtual void initialize();
00432 
00444         virtual SqVM::Script::Pointer createScript(const std::string & scriptName =
00445                                                  "", const bool & dynamic =
00446                                                  false, const std::string & mainMethod =
00447                                                  "main", const bool & hasReturn = false);
00448 
00454         virtual SqVM::Script::Pointer findScript(const std::string & scriptName);
00455 
00464         virtual SqVM::Script::Pointer findOrAddScript(const std::string & scriptName, const bool & dynamic =
00465                                                   false, const std::string & mainMethod =
00466                                                   "main", const bool & hasReturn = false);
00467 
00472         virtual void compileAndExecuteScript(SqVM::Script::Pointer & script);
00473 
00479         virtual void setParameters(SqVM::Script::Pointer & script, const std::list < std::string > &parameters);
00480 
00482 
00483   };
00484 
00485   //
00486   // Include header implementation
00487   //
00488 
00489 #  include "PCMSqVM_impl.h"
00490 
00491 }
00492 
00493 #endif