PixelLightAPI  .
ScriptManager.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: ScriptManager.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_SCRIPTMANAGER_H__
00024 #define __PLCORE_SCRIPTMANAGER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Core/Singleton.h"
00032 #include "PLCore/Container/Array.h"
00033 #include "PLCore/Container/HashMap.h"
00034 #include "PLCore/Base/Event/EventHandler.h"
00035 
00036 
00037 //[-------------------------------------------------------]
00038 //[ Namespace                                             ]
00039 //[-------------------------------------------------------]
00040 namespace PLCore {
00041 
00042 
00043 //[-------------------------------------------------------]
00044 //[ Forward declarations                                  ]
00045 //[-------------------------------------------------------]
00046 class Class;
00047 class Script;
00048 class ScriptBinding;
00049 
00050 
00051 //[-------------------------------------------------------]
00052 //[ Classes                                               ]
00053 //[-------------------------------------------------------]
00054 /**
00055 *  @brief
00056 *    Script manager
00057 */
00058 class ScriptManager : public Singleton<ScriptManager> {
00059 
00060 
00061     //[-------------------------------------------------------]
00062     //[ Friends                                               ]
00063     //[-------------------------------------------------------]
00064     friend class Singleton<ScriptManager>;
00065 
00066 
00067     //[-------------------------------------------------------]
00068     //[ Public static PLCore::Singleton functions             ]
00069     //[-------------------------------------------------------]
00070     // This solution enhances the compatibility with legacy compilers like GCC 4.2.1 used on Mac OS X 10.6
00071     // -> The C++11 feature "extern template" (C++11, see e.g. http://www2.research.att.com/~bs/C++0xFAQ.html#extern-templates) can only be used on modern compilers like GCC 4.6
00072     // -> We can't break legacy compiler support, especially when only the singletons are responsible for the break
00073     // -> See PLCore::Singleton for more details about singletons
00074     public:
00075         static PLCORE_API ScriptManager *GetInstance();
00076         static PLCORE_API bool HasInstance();
00077 
00078 
00079     //[-------------------------------------------------------]
00080     //[ Public functions                                      ]
00081     //[-------------------------------------------------------]
00082     public:
00083         /**
00084         *  @brief
00085         *    Returns a list of the names of the supported script languages
00086         *
00087         *  @return
00088         *    List of the names of the supported script languages
00089         */
00090         inline const Array<String> &GetScriptLanguages();
00091 
00092         /**
00093         *  @brief
00094         *    Returns the name of a script language by it's filename extension
00095         *
00096         *  @param[in] sExtension
00097         *    Script language filename extension to check (e.g. "js")
00098         *
00099         *  @return
00100         *    Script language, empty string on error (possibly not supported filename extension)
00101         */
00102         inline String GetScriptLanguageByExtension(const String &sExtension);
00103 
00104         /**
00105         *  @brief
00106         *    Checks whether or not a given script language is supported
00107         *
00108         *  @param[in] sScriptLanguage
00109         *    Script language to check
00110         *
00111         *  @return
00112         *    'true' if the given script language is supported, else 'false'
00113         */
00114         inline bool IsSupported(const String &sScriptLanguage);
00115 
00116         /**
00117         *  @brief
00118         *    Returns the filename extension of a given script language
00119         *
00120         *  @param[in] sScriptLanguage
00121         *    Script language to return the filename extension from
00122         *
00123         *  @return
00124         *    The filename extension of the given script language, empty string on error (possibly not supported script language)
00125         *
00126         *  @note
00127         *    - If the script language has more than one filename extension, the first filename extension will be returned
00128         */
00129         PLCORE_API String GetScriptLanguageExtension(const String &sScriptLanguage);
00130 
00131         /**
00132         *  @brief
00133         *    Returns a list of all script binding instances
00134         *
00135         *  @return
00136         *    List of all script binding instances
00137         */
00138         inline const Array<ScriptBinding*> &GetScriptBindings();
00139 
00140         /**
00141         *  @brief
00142         *    Creates a script instance
00143         *
00144         *  @param[in] sScriptLanguage
00145         *    Script language to use
00146         *  @param[in] bAddBindings
00147         *    If 'true', add all available script bindings automatically (see "Script::AddBindings()")
00148         *
00149         *  @return
00150         *    The created script instance, null pointer on error
00151         */
00152         PLCORE_API Script *Create(const String &sScriptLanguage, bool bAddBindings = true);
00153 
00154         /**
00155         *  @brief
00156         *    Creates a script instance by using a given filename
00157         *
00158         *  @param[in] sFilename
00159         *    Script filename
00160         *  @param[in] bAddBindings
00161         *    If 'true', add all available script bindings automatically (see "Script::AddBindings()")
00162         *  @param[in] nStringFormat
00163         *    String encoding format to use when dealing with string functions (not supported by all file implementations)
00164         *
00165         *  @return
00166         *    The created script instance, null pointer on error (Unknown filename extension? File not found? Error within the script?)
00167         *
00168         *  @remarks
00169         *    While the "Create()"-method only creates an empty script instance, the "CreateFromFile()"-method will
00170         *    - Create an empty script instance by using the filename extension as indication of the script language to use
00171         *    - Load in the script source code by using the given script filename
00172         *    - Assign the loaded script source code to the created, previously empty, script
00173         *    The result is a ready to be used script by just using this one method. The drawback is, that you can't use
00174         *    custom global functions when using this method because the custom global functions have to be defined before
00175         *    the script source code is assigned to the script (resulting in script compilation).
00176         *
00177         *  @note
00178         *    - Convenience method
00179         *    - It's not recommended to use Unicode by because internally wchar_t is used and this data type has not
00180         *      the same size on every platform (use ASCII or UTF8 instead)
00181         */
00182         PLCORE_API Script *CreateFromFile(const String &sFilename, bool bAddBindings = true, String::EFormat nStringFormat = String::ASCII);
00183 
00184 
00185     //[-------------------------------------------------------]
00186     //[ Protected functions                                   ]
00187     //[-------------------------------------------------------]
00188     protected:
00189         /**
00190         *  @brief
00191         *    Constructor
00192         */
00193         PLCORE_API ScriptManager();
00194 
00195         /**
00196         *  @brief
00197         *    Destructor
00198         */
00199         PLCORE_API virtual ~ScriptManager();
00200 
00201 
00202     //[-------------------------------------------------------]
00203     //[ Private functions                                     ]
00204     //[-------------------------------------------------------]
00205     private:
00206         /**
00207         *  @brief
00208         *    Register a class
00209         *
00210         *  @param[in] pClass
00211         *    Class to register, must be valid!
00212         *
00213         *  @note
00214         *    - If the class is not derived from 'PLCore::Script' it is ignored
00215         *    - The given class must be a new one, so this function did not check whether the
00216         *      class is already registered
00217         */
00218         inline void OnClassLoaded(const Class *pClass);
00219 
00220         /**
00221         *  @brief
00222         *    Unregister a class
00223         *
00224         *  @param[in] pClass
00225         *    Class to unregister, must be valid!
00226         */
00227         void OnClassUnloaded(const Class *pClass);
00228 
00229         /**
00230         *  @brief
00231         *    Registers queued classes
00232         */
00233         PLCORE_API void RegisterClasses();
00234 
00235 
00236     //[-------------------------------------------------------]
00237     //[ Private slots                                         ]
00238     //[-------------------------------------------------------]
00239     private:
00240         EventHandler<const Class*> SlotClassLoaded;
00241         EventHandler<const Class*> SlotClassUnloaded;
00242 
00243 
00244     //[-------------------------------------------------------]
00245     //[ Private data                                          ]
00246     //[-------------------------------------------------------]
00247     private:
00248         Array<const Class*>             m_lstNewClasses;                    /**< New classes to register as soon as required */
00249         // Script languages
00250         Array<String>                   m_lstScriptLanguages;               /**< List of script languages */
00251         HashMap<String, const Class*>   m_mapScriptLanguages;               /**< Map of script languages (key = class name) */
00252         HashMap<String, String>         m_mapScriptLanguagesByExtension;    /**< Map of script languages by extension (key = extension) */
00253         // Script bindings
00254         Array<ScriptBinding*>           m_lstScriptBindings;                /**< List of script bindings */
00255 
00256 
00257 };
00258 
00259 
00260 //[-------------------------------------------------------]
00261 //[ Namespace                                             ]
00262 //[-------------------------------------------------------]
00263 } // PLCore
00264 
00265 
00266 //[-------------------------------------------------------]
00267 //[ Implementation                                        ]
00268 //[-------------------------------------------------------]
00269 #include "PLCore/Script/ScriptManager.inl"
00270 
00271 
00272 #endif // __PLCORE_SCRIPTMANAGER_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