neuray API Programmer's Manual

definition_wrapper.h File Reference

Description

Utility class for MDL material and function definitions.

Code Example

definition_wrapper.h

‎/***************************************************************************************************
 * Copyright 2024 NVIDIA Corporation. All rights reserved.
 **************************************************************************************************/

#ifndef MI_NEURAYLIB_DEFINITION_WRAPPER_H
#define MI_NEURAYLIB_DEFINITION_WRAPPER_H

#include <mi/base/handle.h>
#include <mi/neuraylib/assert.h>
#include <mi/neuraylib/iexpression.h>
#include <mi/neuraylib/ifunction_call.h>
#include <mi/neuraylib/ifunction_definition.h>
#include <mi/neuraylib/imaterial_instance.h>
#include <mi/neuraylib/imdl_factory.h>
#include <mi/neuraylib/itransaction.h>
#include <mi/neuraylib/itype.h>
#include <mi/neuraylib/ivalue.h>

#include <string>

namespace mi {

namespace neuraylib {













class Definition_wrapper
{
public:



    Definition_wrapper( ITransaction* transaction, const char* name, IMdl_factory* mdl_factory);

    bool is_valid() const;

    bool is_valid_definition( IMdl_execution_context* context) const;

    Element_type 
               get_type() const;

    const char* get_mdl_definition() const;

    const char* get_module() const;

    bool is_exported() const;

    bool is_declarative() const;

    bool is_material() const;

    Size 
               get_parameter_count() const;

    const char* get_parameter_name( Size index) const;

    Size 
               get_parameter_index( const char* name) const;

    const IType_list* get_parameter_types() const;

    const IType* get_return_type() const;

    const char* get_thumbnail() const;





    const IExpression_list* get_defaults() const;

    template<class T>
    Sint32 
               get_default( Size index, T& value) const;

    template <class T>
    Sint32 
               get_default( const char* name, T& value) const;




    const IAnnotation_block* get_annotations() const;

    const IAnnotation_list* get_parameter_annotations() const;

    const IAnnotation_block* get_return_annotations() const;

    const IExpression_list* get_enable_if_conditions() const;

    Size 
               get_enable_if_users( Size index) const;

    Size 
               get_enable_if_user( Size index, Size u_index) const;




    IFunction_call* create_instance(
        const IExpression_list* arguments = 0, Sint32* errors = 0) const;

    template <class T>
    T* create_instance( const IExpression_list* arguments = 0, Sint32* errors = 0) const
    {
        IScene_element* ptr_iscene_element = create_instance( arguments, errors);
        if(  !ptr_iscene_element)
            return 0;
        T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
        ptr_iscene_element->release();
        return ptr_T;
    }




    ITransaction* get_transaction() const;

    IMdl_factory* get_mdl_factory() const;

    const IFunction_definition* get_scene_element() const;

    Element_type 
               get_element_type() const;

    const std::string& get_name() const;


private:

    base::Handle< ITransaction> m_transaction;
    base::Handle< const IFunction_definition> m_access;
    base::Handle< IMdl_factory> m_mdl_factory;
    Element_type m_type;
    std::string m_name;
};
 // end group mi_neuray_mdl_elements

inline Definition_wrapper::Definition_wrapper(
    ITransaction* transaction, const char* name, IMdl_factory* mdl_factory)
{
    mi_neuray_assert( transaction);
    mi_neuray_assert( name);

    m_transaction = make_handle_dup( transaction);
    m_name = name;
    m_mdl_factory = make_handle_dup( mdl_factory);
    m_access = transaction->access<IFunction_definition>( name);
    m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
}

inline bool Definition_wrapper::is_valid() const
{
    return m_access.is_valid_interface();
}


inline bool Definition_wrapper::is_valid_definition( IMdl_execution_context* context) const
{
    if( !is_valid())
        return false;

    return m_access->is_valid( context);
}

inline Element_type 
               Definition_wrapper::get_type() const
{
    return m_type;
}

inline const char* Definition_wrapper::get_mdl_definition() const
{
    if( !is_valid())
        return 0;

    return m_access->get_mdl_name();
}

inline const char* Definition_wrapper::get_module() const
{
    if( !is_valid())
        return 0;

    return m_access->get_module();
}

inline bool Definition_wrapper::is_exported() const
{
    if( !is_valid())
        return false;

    return m_access->is_exported();
}

inline bool Definition_wrapper::is_declarative() const
{
    if( !is_valid())
        return false;

    return m_access->is_declarative();
}

inline bool Definition_wrapper::is_material() const
{
    if( !is_valid())
        return false;

    return m_access->is_material();
}

inline Size 
               Definition_wrapper::get_parameter_count() const
{
    if( !is_valid())
        return 0;

    return m_access->get_parameter_count();
}

inline const char* Definition_wrapper::get_parameter_name( Size index) const
{
    if( !is_valid())
        return 0;

    return m_access->get_parameter_name( index);
}

inline Size 
               Definition_wrapper::get_parameter_index( const char* name) const
{
    if( !is_valid())
        return 0;

    return m_access->get_parameter_index( name);
}

inline const IType_list* Definition_wrapper::get_parameter_types() const
{
    if( !is_valid())
        return 0;

    return m_access->get_parameter_types();
}

inline const IType* Definition_wrapper::get_return_type() const
{
    if( !is_valid())
        return 0;

    return m_access->get_return_type();
}

inline const char* Definition_wrapper::get_thumbnail() const
{
    if( !is_valid())
        return 0;

    return m_access->get_thumbnail();
}

inline const IExpression_list* Definition_wrapper::get_defaults() const
{
    if( !is_valid())
        return 0;

    return m_access->get_defaults();
}

template <class T>
Sint32 
               Definition_wrapper::get_default( Size index, T& value) const
{
    if( !is_valid())
        return 0;

    base::Handle< const IExpression_list> defaults( m_access->get_defaults());
    base::Handle< const IExpression> default_( defaults->get_expression( index));
    if( !default_)
        return -2;
    base::Handle< const IExpression_constant> default_constant(
        default_->get_interface<IExpression_constant>());
    if( !default_constant)
        return -4;
    base::Handle< const IValue> default_value( default_constant->get_value());
    Sint32 result = get_value( default_value.get(), value);
    return result == 0 ? 0 : -5;
}

template <class T>
Sint32 
               Definition_wrapper::get_default( const char* name, T& value) const
{
    if( !is_valid())
        return 0;

    base::Handle< const IExpression_list> defaults( m_access->get_defaults());
    base::Handle< const IExpression> default_( defaults->get_expression( name));
    if( !default_)
        return -2;
    base::Handle< const IExpression_constant> default_constant(
        default_->get_interface<IExpression_constant>());
    if( !default_constant)
        return -4;
    base::Handle< const IValue> default_value( default_constant->get_value());
    Sint32 result = get_value( default_value.get(), value);
    return result == 0 ? 0 : -5;
}

inline const IAnnotation_block* Definition_wrapper::get_annotations() const
{
    if( !is_valid())
        return 0;

    return m_access->get_annotations();
}

inline const IAnnotation_list* Definition_wrapper::get_parameter_annotations() const
{
    if( !is_valid())
        return 0;

    return m_access->get_parameter_annotations();
}

inline const IAnnotation_block* Definition_wrapper::get_return_annotations() const
{
    if( !is_valid())
        return 0;

    return m_access->get_return_annotations();
}

inline const IExpression_list* Definition_wrapper::get_enable_if_conditions() const
{
    if( !is_valid())
        return 0;

    return m_access->get_enable_if_conditions();
}

inline Size 
               Definition_wrapper::get_enable_if_users( Size index) const
{
    if( !is_valid())
        return 0;

    return m_access->get_enable_if_users( index);
}

inline Size 
               Definition_wrapper::get_enable_if_user( Size index, Size u_index) const
{
    if( !is_valid())
        return static_cast<Size>( ~0);

    return m_access->get_enable_if_user( index, u_index);
}

inline IFunction_call* Definition_wrapper::create_instance(
    const IExpression_list* arguments, Sint32* errors) const
{
    if( !is_valid())
        return 0;

    if( arguments)
        return m_access->create_function_call( arguments, errors);

    IFunction_definition::Semantics semantic = m_access->get_semantic();
    if(    semantic == IFunction_definition::DS_INTRINSIC_DAG_ARRAY_CONSTRUCTOR
        || semantic == IFunction_definition::DS_INTRINSIC_DAG_ARRAY_LENGTH
        || semantic == IFunction_definition::DS_ARRAY_INDEX
        || semantic == IFunction_definition::DS_TERNARY
        || semantic == IFunction_definition::DS_CAST
        || semantic == IFunction_definition::DS_INTRINSIC_DAG_DECL_CAST) {
        if( errors)
            *errors = -4;
        return 0;
    }

    base::Handle< const IType_list> parameter_types( m_access->get_parameter_types());
    base::Handle< const IExpression_list> defaults( m_access->get_defaults());
    base::Handle< const IAnnotation_list> parameter_annotations(
        m_access->get_parameter_annotations());
    base::Handle< IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
    base::Handle< IExpression_factory> ef(
        m_mdl_factory->create_expression_factory( m_transaction.get()));
    base::Handle< IExpression_list> local_arguments( ef->create_expression_list());

    Size count = m_access->get_parameter_count();
    for( Size i = 0; i < count; ++i) {
            const char* name = m_access->get_parameter_name( i);
        base::Handle< const IExpression> default_( defaults->get_expression( name));
        if( !default_) {
            base::Handle< const IType> type( parameter_types->get_type( i));
            base::Handle< const IAnnotation_block> block(
                parameter_annotations->get_annotation_block( name));
            base::Handle< IValue> value( vf->create( type.get(), block.get()));
            base::Handle< IExpression> expr( ef->create_constant( value.get()));
            local_arguments->add_expression( name, expr.get());
        }
    }
    return m_access->create_function_call( local_arguments.get(), errors);
}

inline ITransaction* Definition_wrapper::get_transaction() const
{
    m_transaction->retain();
    return m_transaction.get();
}

inline IMdl_factory* Definition_wrapper::get_mdl_factory() const
{
    m_mdl_factory->retain();
    return m_mdl_factory.get();
}

inline const IFunction_definition* Definition_wrapper::get_scene_element() const
{
    m_access->retain();
    return m_access.get();
}

inline Element_type 
               Definition_wrapper::get_element_type() const
{
    return m_type;
}

inline const std::string& Definition_wrapper::get_name() const
{
    return m_name;
}

} // namespace neuraylib

} // namespace mi

#endif // MI_NEURAYLIB_DEFINITION_WRAPPER_H

Namespaces

namespace 
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH. More...
namespace 
Namespace for the neuray API. More...

Classes

class 
A wrapper around the interface for MDL function definitions. More...