neuray Services API Programmer's Manual

ievent.h File Reference

Description

neuray services event system

Code Example

ievent.h

‎//*****************************************************************************
// Copyright 1986, 2011 NVIDIA Corporation. All rights reserved.
//*****************************************************************************
//*****************************************************************************
#ifndef MI_NSERVICES_IEVENT_H
#define MI_NSERVICES_IEVENT_H

#include <mi/base/uuid.h>
#include <mi/base/interface_declare.h>
#include <mi/base/handle.h>
#include <mi/neuraylib/istring.h>
#include <mi/neuraylib/inumber.h>
#include <mi/neuraylib/iuuid.h>
#include <mi/neuraylib/icanvas.h>
#include <mi/nservices/ifactory.h>
#include <mi/nservices/error.h>

namespace mi {
class IMap;
namespace neuraylib {
class IPlugin_api;
class ITransaction;
} // namespace neuraylib

namespace nservices {

class IEvent_handler;
class IFactory;
class IConverter;

// Event handling

typedef base::Uuid_t<0x165B083A,0x9175,0xAA14,0x77,0x18,0x35,0x44,0x07,0x88,0x2A,0x0B>
    PRE_COMMAND_EXECUTION;

typedef base::Uuid_t<0x165B084A,0xC4D3,0xB626,0xD7,0x95,0xEE,0xA9,0x65,0x5A,0xF6,0xFC>
               POST_COMMAND_EXECUTION;

typedef base::Uuid_t<0x165FCF31,0xC8D7,0x01A0,0x5D,0x20,0x89,0xE2,0xD3,0xE7,0x46,0x4C>
               CHANGE_SCOPE;

class IEvent : public
    mi::base::Interface_declare<0xec7470a6,0x7a56,0x4bbf,0x97,0x3a,0x1a,0xd6,0xf0,0xf7,0xdb,0x8c>
{
public:
    virtual base::Uuid 
               get_event_type() const = 0;

    virtual Size 
               push_event_data(base::IInterface *data) = 0;

    virtual base::IInterface *get_event_data(Size idx) const = 0;

    template <class T> T* get_event_data(Size idx) const {
        base::IInterface *ptr_iinterface = get_event_data(idx);
        if (ptr_iinterface) {
            T *ptr_T = ptr_iinterface->get_interface<T>();
            ptr_iinterface->release();
            return ptr_T;
        }
        return NULL;
    };
};

class IEvent_context : public
    mi::base::Interface_declare<0x58090348,0x4fcb,0x4bed,0x99,0x30,0x73,0x24,0xb7,0x97,0xee,0x70>
{
public:
    virtual Sint32 
               register_handler(
        base::Uuid event_type,
        const char *event_handler_name,
        const char *name,
        Sint32 priority) = 0;

    virtual Sint32 
               remove_handler(
        base::Uuid event_type,
        const char *name) = 0;

    virtual Sint32 
               send_event(IEvent *event) = 0;

    virtual IFactory *get_factory() const = 0;

    virtual IMap *get_attachments() const = 0;
};

class IEvent_handler_context : public
    mi::base::Interface_declare<0xb163a582,0xd74f,0x4232,0x81,0x99,0x0a,0x5d,0x22,0xab,0xb9,0x96>
{
public:
    virtual neuraylib::IPlugin_api *get_plugin_api() const = 0;

    virtual neuraylib::ITransaction *get_user_state_transaction() = 0;

    virtual neuraylib::ITransaction *get_execution_transaction() = 0;

    virtual IConverter *get_converter() const = 0;

    virtual const char *get_name() const = 0;

    virtual IEvent_context *get_event_context() const = 0;
};

class IEvent_handler : public
    mi::base::Interface_declare<0xe8ec1f65,0x982b,0x4d77,0x8e,0x8d,0x55,0xc8,0x88,0x3d,0xdb,0xef>
{
public:
    virtual const char *get_name() const = 0;

    virtual Sint32 
               handle(
        IEvent_handler_context *context,
        IEvent *event) = 0;
};

class Event_helper
{
public:
    static IEvent* create_event(IEvent_context *event_context, const base::Uuid &event_id);

    static mi::Sint32 
               send_event(IEvent_context *event_context, 
                                 const base::Uuid &event_id, 
                                 Uint32 event_argc = 0,
                                 base::IInterface *event_argv[] = NULL);
};
 // end group mi_nservices_events

} // namespace nservices
} // namespace mi

inline
mi::nservices::IEvent* mi::nservices::Event_helper::create_event(
                                mi::nservices::IEvent_context *event_context,
                                const mi::base::Uuid &event_id)
{
    if ( NULL == event_context )
        return NULL;

    mi::base::Handle< mi::nservices::IFactory > h_factory(
        event_context->get_factory() );
    if ( false == h_factory.is_valid_interface() )
        return NULL;

    mi::base::Handle< mi::IUuid > e_uuid(
                                h_factory->create< mi::IUuid >("Uuid"));
    if ( false == e_uuid.is_valid_interface() ) 
        return NULL;
    e_uuid->set_uuid(event_id);
    const mi::base::IInterface *argv[1] = { e_uuid.get() };
    return h_factory->create< mi::nservices::IEvent >("Event",1,argv);
}

inline 
mi::Sint32 
               mi::nservices::Event_helper::send_event(
                                 mi::nservices::IEvent_context *event_context, 
                                 const mi::base::Uuid &event_id, 
                                 mi::Uint32 event_argc,
                                 mi::base::IInterface *event_argv[])
{
    if ( NULL == event_context )
            return mi::nservices::NRS_ERROR_INVALID_PARAMETERS;

    mi::base::Handle< mi::nservices::IEvent > event(
        mi::nservices::Event_helper::create_event(event_context, event_id));
    if (event.is_valid_interface()) {
        mi::Uint32 i = 0;
        for(; i < event_argc; i++){
            event->push_event_data(event_argv[i]);
        }
        return event_context->send_event(event.get());
    }
    return mi::nservices::NRS_ERROR_FAILED;
}

#endif // MI_NSERVICES_IEVENT_H

Namespaces

namespace mi
namespace mi::neuraylib
namespace 
Namespace for the neuray Services API. More...

Classes

class 
A helper class to facilitate event creation and sending. More...
class 
Used to specify and send an event. More...
class 
The event context. More...
class 
An Event handler. These are implemented by the user. More...
class 
The event handler context. This is passed to event handlers when they are triggered,. More...

Typedefs

typedef base::Uuid_t< 0x165FCF31, 0xC8D7, 0x01A0, 0x5D, 0x20, 0x89, 0xE2, 0xD3, 0xE7, 0x46, 0x4C>  
Triggered when the command context changes scope. More...
typedef base::Uuid_t< 0x165B084A, 0xC4D3, 0xB626, 0xD7, 0x95, 0xEE, 0xA9, 0x65, 0x5A, 0xF6, 0xFC>  
Triggered after each command is executed. More...
typedef base::Uuid_t< 0x165B083A, 0x9175, 0xAA14, 0x77, 0x18, 0x35, 0x44, 0x07, 0x88, 0x2A, 0x0B>  
Triggered before each command is executed. More...