PixelLightAPI  .
Script.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Script.h                                       *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_SCRIPT_H__
00024 #define __PLCORE_SCRIPT_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Base/Object.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Classes                                               ]
00042 //[-------------------------------------------------------]
00043 /**
00044 *  @brief
00045 *    Abstract script base class
00046 *
00047 *  @remarks
00048 *    Each script should have the following properties:
00049 *    - "Language": Script language (for example: "JavaScript" or "Lua")
00050 *    - "Formats":  File format extensions this script can load in (for example: "js" or "lua")
00051 *
00052 *    Supported script features:
00053 *    - Global variables (with namespace support)
00054 *    - Global functions (with namespace support)
00055 *      - C++ calls script
00056 *      - Script calls C++
00057 *    - RTTI objects
00058 *      - Properties
00059 *      - Attributes
00060 *      - Methods
00061 *      - Signals
00062 *      - Slots
00063 *
00064 *    Supported primitive data types: bool, float, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, PLCore::Object*, PLCore::Object&
00065 *    Please note that not each script language/API may make such a detailed data type distinction.
00066 *    Because strings are fundamental within scripts, String is supported as well.
00067 */
00068 class Script : public Object {
00069 
00070 
00071     //[-------------------------------------------------------]
00072     //[ RTTI interface                                        ]
00073     //[-------------------------------------------------------]
00074     pl_class(PLCORE_RTTI_EXPORT, Script, "PLCore", PLCore::Object, "Abstract script base class")
00075         // Properties
00076         pl_properties
00077             pl_property("Language", "Unknown")
00078             pl_property("Formats",  "Unknown")
00079         pl_properties_end
00080         // Attributes
00081         pl_attribute(Name,  String, "", ReadWrite,  DirectValue,    "Name of this script, optional but recommended for better debugging",   "")
00082     pl_class_end
00083 
00084 
00085     //[-------------------------------------------------------]
00086     //[ Public functions                                      ]
00087     //[-------------------------------------------------------]
00088     public:
00089         /**
00090         *  @brief
00091         *    Destructor
00092         */
00093         PLCORE_API virtual ~Script();
00094 
00095         /**
00096         *  @brief
00097         *    Returns the name of the script language the script is using
00098         *
00099         *  @return
00100         *    The name of the script language the script is using (for example "Lua" or "JavaScript")
00101         */
00102         PLCORE_API String GetScriptLanguage() const;
00103 
00104         /**
00105         *  @brief
00106         *    Returns a list of file formats this script supports
00107         *
00108         *  @param[out] lstFormats
00109         *    List of file formats this script supports (the given list is not cleared before new entries are added)
00110         */
00111         PLCORE_API void GetFormats(Array<String> &lstFormats) const;
00112 
00113         /**
00114         *  @brief
00115         *    Adds a script binding to connect the given RTTI class instance with this script
00116         *
00117         *  @param[in] cObject
00118         *    RTTI class instance, must stay valid as long as this script lives
00119         *  @param[in] sNamespace
00120         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00121         *
00122         *  @note
00123         *    - The added RTTI class instance methods will be available to the script as simple global functions
00124         */
00125         PLCORE_API void AddBinding(Object &cObject, const String &sNamespace = "");
00126 
00127         /**
00128         *  @brief
00129         *    Add all script bindings to this script
00130         *
00131         *  @remarks
00132         *    Iterates over all available script binding instances and adds them to this script.
00133         *
00134         *  @note
00135         *    - The added RTTI class instance methods will be available to the script as simple global functions
00136         */
00137         PLCORE_API void AddBindings();
00138 
00139 
00140     //[-------------------------------------------------------]
00141     //[ Public virtual Script functions                       ]
00142     //[-------------------------------------------------------]
00143     public:
00144         //[-------------------------------------------------------]
00145         //[ Global functions                                      ]
00146         //[-------------------------------------------------------]
00147         /**
00148         *  @brief
00149         *    Returns whether or not the given name belongs to a global function
00150         *
00151         *  @param[in] sName
00152         *    Name of the global function
00153         *  @param[in] sNamespace
00154         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00155         *
00156         *  @return
00157         *    'true' if the given name belongs to a global function, else 'false'
00158         *
00159         *  @remarks
00160         *    When calling a global script function, the script backend usually writes an error into the
00161         *    log when the given global script function wasn't found. So, when using optional global script
00162         *    functions, it's a good idea to check whether there's such a global script function by using "IsGlobalFunction()".
00163         */
00164         virtual bool IsGlobalFunction(const String &sName, const String &sNamespace = "") = 0;
00165 
00166         /**
00167         *  @brief
00168         *    Adds a global function to the script
00169         *
00170         *  @param[in] sFunction
00171         *    Function name used inside the script to call the global function
00172         *  @param[in] cDynFunc
00173         *    Dynamic function
00174         *  @param[in] sNamespace
00175         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00176         *
00177         *  @return
00178         *    'true' if all went fine, else 'false' (maybe a script is already set?)
00179         *
00180         *  @note
00181         *    - If there's already a set script ("SetSourceCode()") this method will return an error
00182         */
00183         virtual bool AddGlobalFunction(const String &sFunction, const DynFunc &cDynFunc, const String &sNamespace = "") = 0;
00184 
00185         /**
00186         *  @brief
00187         *    Removes all global functions from the script
00188         *
00189         *  @return
00190         *    'true' if all went fine, else 'false' (maybe a script is already set?)
00191         *
00192         *  @note
00193         *    - If there's already a set script ("SetSourceCode()") this method will return an error
00194         */
00195         virtual bool RemoveAllGlobalFunctions() = 0;
00196 
00197         //[-------------------------------------------------------]
00198         //[ Script source code                                    ]
00199         //[-------------------------------------------------------]
00200         /**
00201         *  @brief
00202         *    Returns the script source code
00203         *
00204         *  @return
00205         *    The script source code
00206         */
00207         virtual String GetSourceCode() const = 0;
00208 
00209         /**
00210         *  @brief
00211         *    Sets the script source code
00212         *
00213         *  @param[in] sSourceCode
00214         *    Script source code, usually blank ASCII code, empty string to set to script at all
00215         *
00216         *  @return
00217         *    'true' if all went fine, else 'false'
00218         */
00219         virtual bool SetSourceCode(const String &sSourceCode) = 0;
00220 
00221         /**
00222         *  @brief
00223         *    Returns a list of filenames associated with this script
00224         *
00225         *  @param[out] lstFilenames
00226         *    Receives a list of filenames associated with this script (list is not cleared before adding new entries)
00227         *
00228         *  @remarks
00229         *    For example Lua allows to use the keyword "require" to add the content of another script. This method
00230         *    returns a list of the filenames of the files which are included within this script. One can use this
00231         *    information to e.g. reload the script when the one of the associated files was changed, beside reloading
00232         *    when just the original script file was changed.
00233         */
00234         virtual void GetAssociatedFilenames(Array<String> &lstFilenames) = 0;
00235 
00236         /**
00237         *  @brief
00238         *    Executes a given script source code string
00239         *
00240         *  @param[in] sSourceCode
00241         *    Script source code to execute, usually blank ASCII code
00242         *
00243         *  @return
00244         *    'true' if all went fine, else 'false'
00245         *
00246         *  @note
00247         *    - Only works if there's already a valid script currently used
00248         *    - Lua example statement: "Speed=42"
00249         */
00250         virtual bool Execute(const String &sSourceCode) = 0;
00251 
00252         //[-------------------------------------------------------]
00253         //[ Global variables                                      ]
00254         //[-------------------------------------------------------]
00255         /**
00256         *  @brief
00257         *    Adds the names of found global variables to a given list
00258         *
00259         *  @param[out] lstGlobalVariables
00260         *    List to be filled with the names (without namespace) of the found global variables, the given list is not cleared before new entries are added
00261         *  @param[in]  sNamespace
00262         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00263         */
00264         virtual void GetGlobalVariables(Array<String> &lstGlobalVariables, const String &sNamespace = "") = 0;
00265 
00266         /**
00267         *  @brief
00268         *    Returns whether or not the given name belongs to a global variable
00269         *
00270         *  @param[in] sName
00271         *    Name of the global variable
00272         *  @param[in] sNamespace
00273         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00274         *
00275         *  @return
00276         *    'true' if the given name belongs to a global variable, else 'false'
00277         */
00278         virtual bool IsGlobalVariable(const String &sName, const String &sNamespace = "") = 0;
00279 
00280         /**
00281         *  @brief
00282         *    Returns the type ID a global variable
00283         *
00284         *  @param[in] sName
00285         *    Name of the global variable
00286         *  @param[in] sNamespace
00287         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00288         *
00289         *  @return
00290         *    The type ID of the global variable (e.g. "PLCore::TypeFloat" for "float") or "PLCore::TypeInvalid" on error
00291         */
00292         virtual ETypeID GetGlobalVariableTypeID(const String &sName, const String &sNamespace = "") = 0;
00293 
00294         /**
00295         *  @brief
00296         *    Returns the current value of a global variable
00297         *
00298         *  @param[in] sName
00299         *    Name of the global variable
00300         *  @param[in] sNamespace
00301         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00302         *
00303         *  @return
00304         *    The current value of the global variable
00305         */
00306         virtual String GetGlobalVariable(const String &sName, const String &sNamespace = "") = 0;
00307 
00308         /**
00309         *  @brief
00310         *    Sets the current value of a global variable
00311         *
00312         *  @param[in] sName
00313         *    Name of the global variable
00314         *  @param[in] cValue
00315         *    New value of the global variable
00316         *  @param[in] sNamespace
00317         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00318         *
00319         *  @note
00320         *    - If there's no global variable with the given name, a new global variable is added to the script
00321         *    - Please note that it depends on the used script language/API which data types are really available,
00322         *      this means that "GetGlobalVariableTypeID()" may return another data type as the one you specified
00323         */
00324         virtual void SetGlobalVariable(const String &sName, const DynVar &cValue, const String &sNamespace = "") = 0;
00325 
00326         //[-------------------------------------------------------]
00327         //[ Global function call, used by "FuncScriptPtr"         ]
00328         //[-------------------------------------------------------]
00329         /**
00330         *  @brief
00331         *    Starts a function call
00332         *
00333         *  @param[in] sFunctionName
00334         *    Name of the function to call
00335         *  @param[in] sFunctionSignature
00336         *    Signature of the function to call (e.g. "void(int,float)")
00337         *  @param[in] sNamespace
00338         *    Optional namespace (e.g. "MyNamespace", "MyNamespace.MyOtherNamespace" and so on)
00339         *
00340         *  @return
00341         *    'true' if all went fine, else 'false'
00342         *
00343         *  @note
00344         *    - It's not recommended to use this method directly, use "FuncScriptPtr" instead
00345         *
00346         *  @see
00347         *    - Have a look at "IsGlobalFunction()" for additional information
00348         */
00349         virtual bool BeginCall(const String &sFunctionName, const String &sFunctionSignature, const String &sNamespace = "") = 0;
00350 
00351         /**
00352         *  @brief
00353         *    Pushes an argument required for the current function call
00354         *
00355         *  @param[in] nValue
00356         *    Argument value
00357         *
00358         *  @note
00359         *    - It's not recommended to use this method directly, use "FuncScriptPtr" instead
00360         */
00361         virtual void PushArgument(bool bValue) = 0;
00362         virtual void PushArgument(float fValue) = 0;
00363         virtual void PushArgument(double fValue) = 0;
00364         virtual void PushArgument(int8 nValue) = 0;
00365         virtual void PushArgument(int16 nValue) = 0;
00366         virtual void PushArgument(int32 nValue) = 0;
00367         virtual void PushArgument(int64 nValue) = 0;
00368         virtual void PushArgument(uint8 nValue) = 0;
00369         virtual void PushArgument(uint16 nValue) = 0;
00370         virtual void PushArgument(uint32 nValue) = 0;
00371         virtual void PushArgument(uint64 nValue) = 0;
00372         virtual void PushArgument(const String &sString) = 0;
00373         virtual void PushArgument(Object *pObject) = 0;
00374         virtual void PushArgument(Object &cObject) = 0;
00375 
00376         /**
00377         *  @brief
00378         *    Ends a function call
00379         *
00380         *  @return
00381         *    'true' if all went fine, else 'false'
00382         *
00383         *  @note
00384         *    - It's not recommended to use this method directly, use "FuncScriptPtr" instead
00385         *    - This actually performs the prepared function call
00386         */
00387         virtual bool EndCall() = 0;
00388 
00389         /**
00390         *  @brief
00391         *    Returns the result of a function call
00392         *
00393         *  @param[in] nValue
00394         *    Unused value... just there so the compiler can figure out the proper method
00395         *
00396         *  @return
00397         *    The result of a function call
00398         *
00399         *  @note
00400         *    - It's not recommended to use this method directly, use "FuncScriptPtr" instead
00401         */
00402         virtual bool GetReturn(bool nValue) = 0;
00403         virtual float GetReturn(float nValue) = 0;
00404         virtual double GetReturn(double nValue) = 0;
00405         virtual int8 GetReturn(int8 nValue) = 0;
00406         virtual int16 GetReturn(int16 nValue) = 0;
00407         virtual int32 GetReturn(int32 nValue) = 0;
00408         virtual int64 GetReturn(int64 nValue) = 0;
00409         virtual uint8 GetReturn(uint8 nValue) = 0;
00410         virtual uint16 GetReturn(uint16 nValue) = 0;
00411         virtual uint32 GetReturn(uint32 nValue) = 0;
00412         virtual uint64 GetReturn(uint64 nValue) = 0;
00413         virtual String GetReturn(String nValue) = 0;
00414         virtual Object *GetReturn(Object *nValue) = 0;
00415         virtual Object &GetReturn(Object &nValue) = 0;
00416 
00417 
00418     //[-------------------------------------------------------]
00419     //[ Protected functions                                   ]
00420     //[-------------------------------------------------------]
00421     protected:
00422         /**
00423         *  @brief
00424         *    Constructor
00425         */
00426         PLCORE_API Script();
00427 
00428         /**
00429         *  @brief
00430         *    Write a string into the log
00431         *
00432         *  @param[in] nLogLevel
00433         *    Log level
00434         *  @param[in] sText
00435         *    Text which should be written into the log
00436         *
00437         *  @return
00438         *    'true' if all went fine, else 'false'
00439         *
00440         *  @remarks
00441         *    The text is written to the log only if the current
00442         *    log level is greater or equal to the specified value.
00443         *    This method is an extension of "Log::Output()"
00444         *    which also adds the name of the script to the given
00445         *    text.
00446         */
00447         PLCORE_API bool LogOutput(uint8 nLogLevel, const String &sText);
00448 
00449 
00450     //[-------------------------------------------------------]
00451     //[ Private functions                                     ]
00452     //[-------------------------------------------------------]
00453     private:
00454         /**
00455         *  @brief
00456         *    Copy constructor
00457         *
00458         *  @param[in] cSource
00459         *    Source to copy from
00460         */
00461         Script(const Script &cSource);
00462 
00463         /**
00464         *  @brief
00465         *    Copy operator
00466         *
00467         *  @param[in] cSource
00468         *    Source to copy from
00469         *
00470         *  @return
00471         *    Reference to this instance
00472         */
00473         Script &operator =(const Script &cSource);
00474 
00475 
00476 };
00477 
00478 
00479 //[-------------------------------------------------------]
00480 //[ Namespace                                             ]
00481 //[-------------------------------------------------------]
00482 } // PLCore
00483 
00484 
00485 #endif // __PLCORE_SCRIPT_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:59
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported