Changeset 3500

Show
Ignore:
Timestamp:
03/15/10 06:22:08 (5 months ago)
Author:
trichards
Message:

Implemented Core Scripting enhancements to allow script_type<>() constructor to take a I_ScriptType as an argument.
This allows additional methods to be added to a partially defined script type prior to it being activated. See #204.

Location:
Core/branches/0075_TR_SCRIPTING/Scripting
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • Core/branches/0075_TR_SCRIPTING/Scripting/I_ScriptModule.hpp

    r1074 r3500  
    22// Zen Core Framework 
    33// 
    4 // Copyright (C) 2001 - 2009 Tony Richards 
     4// Copyright (C) 2001 - 2010 Tony Richards 
    55// Copyright (C) 2008 - 2009 Matthew Alan Gray 
    66// 
     
    3131#include <Zen/Core/Memory/managed_ptr.hpp> 
    3232#include <Zen/Core/Memory/managed_weak_ptr.hpp> 
     33#include <Zen/Core/Memory/managed_self_ref.hpp> 
     34 
    3335#include <Zen/Core/Event/Event.hpp> 
    3436 
     
    5052/// the modules nonetheless. 
    5153class SCRIPTING_DLL_LINK I_ScriptModule 
     54:   public Memory::managed_self_ref<I_ScriptModule> 
    5255{ 
    5356    /// @name Types 
     
    6770    /// @{ 
    6871public: 
    69     /// Create a type or class 
     72    /// Create a type or class. 
    7073    /// @param _typeName name of the type as exposed to the scripting language.  Some languages 
    7174    ///         prepend the module name (such as Python). 
     
    8386    virtual void activate() = 0; 
    8487 
    85     /// Create a new script object and binds it to a C++ object 
     88    /// Create a new script object and binds it to a C++ object. 
    8689    /// @param _name Name of the object 
    8790    /// @param _pType Type of the object 
    8891    /// @param _pObject C++ version of the object 
    89     /// @todo  
    9092    virtual void createObject(pScriptType_type _pType, I_ObjectReference* _pObject) = 0; 
    9193 
    92     /// Create a new global script object and binds it to a C++ object 
     94    /// Create a new global script object and binds it to a C++ object. 
    9395    /// @param _name Name of the object 
    9496    /// @param _pType Type of the object 
  • Core/branches/0075_TR_SCRIPTING/Scripting/I_ScriptType.hpp

    r3256 r3500  
    4444class I_ObjectReference; 
    4545class I_ScriptMethod; 
     46class I_ScriptModule; 
    4647 
    4748class SCRIPTING_DLL_LINK I_ScriptType 
     
    5354    typedef Zen::Memory::managed_weak_ptr<I_ScriptType>                 wpScriptType_type; 
    5455    typedef Zen::Event::Event<wpScriptType_type>                        scriptTypeEvent_type; 
     56    typedef Zen::Memory::managed_ptr<I_ScriptModule>                    pScriptModule_type; 
    5557 
    5658    typedef I_ObjectReference*                                          pObjectReference_type; 
     
    7072    typedef int(*int_function_no_args_type)(pObjectReference_type); 
    7173    typedef int(*int_function_args_type)(pObjectReference_type, std::vector<boost::any>); 
    72 /// @} 
     74    /// @} 
    7375 
    7476    /// @name I_ScriptType interface 
    7577    /// @{ 
    7678public: 
     79    /// Get the module to which this type belongs. 
     80    virtual pScriptModule_type getScriptModule() = 0; 
     81 
     82    /// Get the name of this type. 
     83    virtual const std::string& getTypeName() = 0; 
     84 
     85    /// Get the documentation / short description of this type. 
     86    virtual const std::string& getDocumentation() = 0; 
     87 
    7788    /// Add a method to this type that takes no arguments and returns void 
    7889    virtual void addMethod(const std::string& _name, const std::string& _docString, void_function_no_args_type _function) = 0; 
  • Core/branches/0075_TR_SCRIPTING/Scripting/script_type.hpp

    r3407 r3500  
    5757    typedef std::map<std::string, I_ScriptMethod*>          Methods_type; 
    5858    typedef Zen::Memory::managed_ptr<I_ScriptType>          pScriptType_type; 
     59    typedef Zen::Memory::managed_ptr<I_ScriptModule>        pScriptModule_type; 
     60    typedef Zen::Memory::managed_weak_ptr<I_ScriptModule>   wpScriptModule_type; 
    5961    typedef std::map<std::string, I_ScriptableType*>        GlobalObjects_type; 
    6062    /// @} 
     
    8385    /// Create a global instance of this script type. 
    8486    void createGlobalObject(const std::string& _objectName, I_ScriptableType* _pScriptableObject); 
     87 
     88    /// Get the script module for this type. 
     89    /// This will not always return a valid script module depending 
     90    /// upon the state of this script_type. 
     91    /// Look at the documentation of the constructors of this class for more information. 
     92    pScriptModule_type getScriptModule(); 
    8593    /// @} 
    8694 
    8795    /// @name 'Structors. 
    88     ///     These methods are only called by script_module. 
     96    /// 
    8997    /// @{ 
     98public: 
     99    /// Construct a script type that adds to an existing script type whose 
     100    /// module has not yet been activated.  After adding methods to this script_type, 
     101    /// invoke the script_type::activate() method to register the methods. 
     102    /// getScriptModule() will return a valid script module if this constructor 
     103    /// is used. 
     104    script_type(pScriptType_type pScriptType_type); 
    90105private: 
    91106    friend class script_module; 
    92107    /// Create a script type. 
     108    /// This constructor is invoked by script_module. 
     109    /// getScriptModule() will return a valid script module if this constructor 
     110    /// is used only after _module::activate() has been invoked. 
    93111    script_type(script_module& _module, const std::string& _typeName, const std::string& _documentation); 
    94112    /// @} 
     
    98116private: 
    99117    /// Script module that contains this script type. 
    100     script_module&      m_module; 
     118    /// This is the template version.  Either m_pModule or m_pScriptModule 
     119    /// are valid, but generally not both. 
     120    /// @see m_pScriptModule 
     121    script_module*      m_pModule; 
     122 
     123    /// Script module that contains this script type. 
     124    /// This is the interface version. 
     125    /// @see m_pModule 
     126    wpScriptModule_type m_pScriptModule; 
    101127 
    102128    /// Name of this script_type. 
  • Core/branches/0075_TR_SCRIPTING/Scripting/script_type_impl.hpp

    r3407 r3500  
    4040inline 
    4141script_type<ScriptableClass_type>::script_type(script_module& _module, const std::string& _typeName, const std::string& _documentation) 
    42 :   m_module(_module) 
     42:   m_pModule(&_module) 
     43,   m_pScriptModule() 
    4344,   m_typeName(_typeName) 
    4445,   m_documentation(_documentation) 
     46{ 
     47} 
     48 
     49//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     50template<typename ScriptableClass_type> 
     51inline 
     52script_type<ScriptableClass_type>::script_type(pScriptType_type _pScriptType) 
     53:   m_pModule(NULL) 
     54,   m_pScriptModule(_pScriptType->getScriptModule()) 
     55,   m_typeName(_pScriptType->getTypeName()) 
     56,   m_documentation(_pScriptType->getDocumentation()) 
     57,   m_pScriptType(_pScriptType) 
    4558{ 
    4659} 
     
    90103script_type<ScriptableClass_type>::activate() 
    91104{ 
    92     m_pScriptType = m_module.getScriptModule()->createScriptType(m_typeName, m_documentation, 0); 
     105    // Create m_pScriptType if it hasn't already been created. 
     106    if (!m_pScriptType.isValid()) 
     107    { 
     108        m_pScriptType = getScriptModule()->createScriptType(m_typeName, m_documentation, 0); 
     109    } 
    93110 
    94111    // Iterate through the script_method and register them. 
    95112    for(Methods_type::iterator iter = m_methods.begin(); iter != m_methods.end(); iter++) 
    96113    { 
    97         // TODO I_ScriptMethod needs to support documentation 
     114        // TODO I_ScriptMethod needs to support documentation.  For now the second 
     115        // parameter is simply an empty string. 
    98116        m_pScriptType->addMethod(iter->first, "", iter->second); 
    99117    } 
     
    110128    for(GlobalObjects_type::iterator iter = m_globalObjects.begin(); iter != m_globalObjects.end(); iter++) 
    111129    { 
    112         m_module.getScriptModule()->createGlobalObject(iter->first, m_pScriptType, iter->second->getScriptObject()); 
     130        getScriptModule()->createGlobalObject(iter->first, m_pScriptType, iter->second->getScriptObject()); 
    113131    } 
    114132 
     
    121139script_type<ScriptableClass_type>::createGlobalObject(const std::string& _objectName, I_ScriptableType* _pScriptableObject) 
    122140{ 
    123     // Defer this until after the module has been activated. 
     141    // Defer the creation of the object until after the module has been activated. 
     142    // For now, keep a collection of global objects, and when the module 
     143    // is activated, create them using createGlobals() 
    124144    m_globalObjects[_objectName] = _pScriptableObject; 
     145} 
     146 
     147//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     148template<typename ScriptableClass_type> 
     149inline 
     150Zen::Memory::managed_ptr<I_ScriptModule> 
     151script_type<ScriptableClass_type>::getScriptModule() 
     152{ 
     153    if(m_pModule) 
     154    { 
     155        return m_pModule->getScriptModule(); 
     156    } 
     157    else 
     158    { 
     159        return m_pScriptModule.lock(); 
     160    } 
    125161} 
    126162