MDL SDK API nvidia_logo_transpbg.gif Up
definition_wrapper.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2025 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
6
7#ifndef MI_NEURAYLIB_DEFINITION_WRAPPER_H
8#define MI_NEURAYLIB_DEFINITION_WRAPPER_H
9
10#include <mi/base/handle.h>
11#include <mi/neuraylib/assert.h>
18#include <mi/neuraylib/itype.h>
19#include <mi/neuraylib/ivalue.h>
20
21#include <string>
22
23namespace mi {
24
25namespace neuraylib {
26
44{
45public:
46
48
49
57 Definition_wrapper( ITransaction* transaction, const char* name, IMdl_factory* mdl_factory);
58
64 bool is_valid() const;
65
74 bool is_valid_definition( IMdl_execution_context* context) const;
75
81 Element_type get_type() const;
82
84 const char* get_mdl_definition() const;
85
87 const char* get_module() const;
88
90 bool is_exported() const;
91
93 bool is_declarative() const;
94
96 bool is_material() const;
97
100
105 const char* get_parameter_name( Size index) const;
106
111 Size get_parameter_index( const char* name) const;
112
114 const IType_list* get_parameter_types() const;
115
117 const IType* get_return_type() const;
118
126 const char* get_thumbnail() const;
127
128
130
132
138 const IExpression_list* get_defaults() const;
139
157 template<class T>
158 Sint32 get_default( Size index, T& value) const;
159
173 template <class T>
174 Sint32 get_default( const char* name, T& value) const;
175
177
179
181 const IAnnotation_block* get_annotations() const;
182
190
193
201
208 Size get_enable_if_users( Size index) const;
209
217 Size get_enable_if_user( Size index, Size u_index) const;
218
220
222
246 const IExpression_list* arguments = nullptr, Sint32* errors = nullptr) const;
247
252 template <class T>
253 T* create_instance( const IExpression_list* arguments = nullptr, Sint32* errors = nullptr) const
254 {
255 IScene_element* ptr_iscene_element = create_instance( arguments, errors);
256 if( !ptr_iscene_element)
257 return 0;
258 T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
259 ptr_iscene_element->release();
260 return ptr_T;
261 }
262
264
266
269
272
275
278
280 const std::string& get_name() const;
281
283
284private:
285
286 base::Handle<ITransaction> m_transaction;
288 base::Handle<IMdl_factory> m_mdl_factory;
289 Element_type m_type;
290 std::string m_name;
291};
292 // end group mi_neuray_mdl_elements
294
296 ITransaction* transaction, const char* name, IMdl_factory* mdl_factory)
297{
298 mi_neuray_assert( transaction);
299 mi_neuray_assert( name);
300
301 m_transaction = make_handle_dup( transaction);
302 m_name = name;
303 m_mdl_factory = make_handle_dup( mdl_factory);
304 m_access = transaction->access<IFunction_definition>( name);
305 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
306}
307
309{
310 return m_access.is_valid_interface();
311}
312
313
315{
316 if( !is_valid())
317 return false;
318
319 return m_access->is_valid( context);
320}
321
323{
324 return m_type;
325}
326
328{
329 if( !is_valid())
330 return nullptr;
331
332 return m_access->get_mdl_name();
333}
334
335inline const char* Definition_wrapper::get_module() const
336{
337 if( !is_valid())
338 return nullptr;
339
340 return m_access->get_module();
341}
342
344{
345 if( !is_valid())
346 return false;
347
348 return m_access->is_exported();
349}
350
352{
353 if( !is_valid())
354 return false;
355
356 return m_access->is_declarative();
357}
358
360{
361 if( !is_valid())
362 return false;
363
364 return m_access->is_material();
365}
366
368{
369 if( !is_valid())
370 return 0;
371
372 return m_access->get_parameter_count();
373}
374
375inline const char* Definition_wrapper::get_parameter_name( Size index) const
376{
377 if( !is_valid())
378 return nullptr;
379
380 return m_access->get_parameter_name( index);
381}
382
383inline Size Definition_wrapper::get_parameter_index( const char* name) const
384{
385 if( !is_valid())
386 return 0;
387
388 return m_access->get_parameter_index( name);
389}
390
392{
393 if( !is_valid())
394 return nullptr;
395
396 return m_access->get_parameter_types();
397}
398
400{
401 if( !is_valid())
402 return nullptr;
403
404 return m_access->get_return_type();
405}
406
407inline const char* Definition_wrapper::get_thumbnail() const
408{
409 if( !is_valid())
410 return nullptr;
411
412 return m_access->get_thumbnail();
413}
414
416{
417 if( !is_valid())
418 return nullptr;
419
420 return m_access->get_defaults();
421}
422
423template <class T>
425{
426 if( !is_valid())
427 return 0;
428
429 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
430 base::Handle<const IExpression> default_( defaults->get_expression( index));
431 if( !default_)
432 return -2;
435 if( !default_constant)
436 return -4;
437 base::Handle<const IValue> default_value( default_constant->get_value());
438 Sint32 result = get_value( default_value.get(), value);
439 return result == 0 ? 0 : -5;
440}
441
442template <class T>
443Sint32 Definition_wrapper::get_default( const char* name, T& value) const
444{
445 if( !is_valid())
446 return 0;
447
448 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
449 base::Handle<const IExpression> default_( defaults->get_expression( name));
450 if( !default_)
451 return -2;
454 if( !default_constant)
455 return -4;
456 base::Handle<const IValue> default_value( default_constant->get_value());
457 Sint32 result = get_value( default_value.get(), value);
458 return result == 0 ? 0 : -5;
459}
460
462{
463 if( !is_valid())
464 return nullptr;
465
466 return m_access->get_annotations();
467}
468
470{
471 if( !is_valid())
472 return nullptr;
473
474 return m_access->get_parameter_annotations();
475}
476
478{
479 if( !is_valid())
480 return nullptr;
481
482 return m_access->get_return_annotations();
483}
484
486{
487 if( !is_valid())
488 return nullptr;
489
490 return m_access->get_enable_if_conditions();
491}
492
494{
495 if( !is_valid())
496 return 0;
497
498 return m_access->get_enable_if_users( index);
499}
500
502{
503 if( !is_valid())
504 return static_cast<Size>( ~0);
505
506 return m_access->get_enable_if_user( index, u_index);
507}
508
510 const IExpression_list* arguments, Sint32* errors) const
511{
512 if( !is_valid())
513 return nullptr;
514
515 if( arguments)
516 return m_access->create_function_call( arguments, errors);
517
518 IFunction_definition::Semantics semantic = m_access->get_semantic();
523 || semantic == IFunction_definition::DS_CAST
525 if( errors)
526 *errors = -4;
527 return nullptr;
528 }
529
530 base::Handle<const IType_list> parameter_types( m_access->get_parameter_types());
531 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
532 base::Handle<const IAnnotation_list> parameter_annotations(
533 m_access->get_parameter_annotations());
534 base::Handle<IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
536 m_mdl_factory->create_expression_factory( m_transaction.get()));
537 base::Handle<IExpression_list> local_arguments( ef->create_expression_list());
538
539 Size count = m_access->get_parameter_count();
540 for( Size i = 0; i < count; ++i) {
541 const char* name = m_access->get_parameter_name( i);
542 base::Handle<const IExpression> default_( defaults->get_expression( name));
543 if( !default_) {
544 base::Handle<const IType> type( parameter_types->get_type( i));
546 parameter_annotations->get_annotation_block( name));
547 base::Handle<IValue> value( vf->create( type.get(), block.get()));
548 base::Handle<IExpression> expr( ef->create_constant( value.get()));
549 local_arguments->add_expression( name, expr.get());
550 }
551 }
552 return m_access->create_function_call( local_arguments.get(), errors);
553}
554
556{
557 m_transaction->retain();
558 return m_transaction.get();
559}
560
562{
563 m_mdl_factory->retain();
564 return m_mdl_factory.get();
565}
566
568{
569 m_access->retain();
570 return m_access.get();
571}
572
574{
575 return m_type;
576}
577
578inline const std::string& Definition_wrapper::get_name() const
579{
580 return m_name;
581}
582
583} // namespace neuraylib
584
585} // namespace mi
586
587#endif // MI_NEURAYLIB_DEFINITION_WRAPPER_H
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:112
A wrapper around the interface for MDL function definitions.
Definition: definition_wrapper.h:44
const char * get_module() const
Returns the DB name of the corresponding module.
Definition: definition_wrapper.h:335
Sint32 get_default(Size index, T &value) const
Returns the default of a non-array parameter.
Definition: definition_wrapper.h:424
const IExpression_list * get_enable_if_conditions() const
Returns the enable_if conditions of all parameters.
Definition: definition_wrapper.h:485
Definition_wrapper(ITransaction *transaction, const char *name, IMdl_factory *mdl_factory)
Constructs an MDL definition wrapper for a fixed material or function definition.
Definition: definition_wrapper.h:295
const IExpression_list * get_defaults() const
Returns the defaults of all parameters.
Definition: definition_wrapper.h:415
const char * get_mdl_definition() const
Returns the MDL name of the material or function definition.
Definition: definition_wrapper.h:327
const IAnnotation_list * get_parameter_annotations() const
Returns the annotations of all parameters.
Definition: definition_wrapper.h:469
const IType * get_return_type() const
Returns the return type.
Definition: definition_wrapper.h:399
const IType_list * get_parameter_types() const
Returns the types of all parameters.
Definition: definition_wrapper.h:391
Size get_enable_if_user(Size index, Size u_index) const
Returns the index of a parameter whose enable_if condition might depend on the argument of the given ...
Definition: definition_wrapper.h:501
bool is_material() const
Indicates whether the definition represents a material.
Definition: definition_wrapper.h:359
bool is_exported() const
Indicates whether the material or function definition is exported by its module.
Definition: definition_wrapper.h:343
IFunction_call * create_instance(const IExpression_list *arguments=nullptr, Sint32 *errors=nullptr) const
Creates an instance of the material or function definition.
Definition: definition_wrapper.h:509
bool is_valid_definition(IMdl_execution_context *context) const
Indicates whether the material or function definition referenced by this definition wrapper matches a...
Definition: definition_wrapper.h:314
T * create_instance(const IExpression_list *arguments=nullptr, Sint32 *errors=nullptr) const
Creates an instance of the material or function definition.
Definition: definition_wrapper.h:253
IMdl_factory * get_mdl_factory() const
Get the MDL factory.
Definition: definition_wrapper.h:561
ITransaction * get_transaction() const
Get the transaction.
Definition: definition_wrapper.h:555
Element_type get_type() const
Indicates whether the definition wrapper acts on a material definition or on a function definition.
Definition: definition_wrapper.h:322
bool is_declarative() const
Indicates whether the material or function definition is declarative.
Definition: definition_wrapper.h:351
const char * get_parameter_name(Size index) const
Returns the name of the parameter at index.
Definition: definition_wrapper.h:375
Element_type get_element_type() const
Get the element type.
Definition: definition_wrapper.h:573
const IAnnotation_block * get_return_annotations() const
Returns the annotations of the return type.
Definition: definition_wrapper.h:477
Size get_enable_if_users(Size index) const
Returns the number of other parameters whose enable_if condition might depend on the argument of the ...
Definition: definition_wrapper.h:493
const std::string & get_name() const
Get the DB name of the MDL function or material definition.
Definition: definition_wrapper.h:578
const IFunction_definition * get_scene_element() const
Get the MDL function or material definition.
Definition: definition_wrapper.h:567
bool is_valid() const
Indicates whether the definition wrapper is in a valid state.
Definition: definition_wrapper.h:308
const IAnnotation_block * get_annotations() const
Returns the annotations for a material or function definition.
Definition: definition_wrapper.h:461
Size get_parameter_index(const char *name) const
Returns the index position of a parameter.
Definition: definition_wrapper.h:383
const char * get_thumbnail() const
Returns the resolved file name of the thumbnail image for this MDL definition.
Definition: definition_wrapper.h:407
Size get_parameter_count() const
Returns the number of parameters.
Definition: definition_wrapper.h:367
An annotation block is an array of annotations.
Definition: iexpression.h:580
An ordered collection of annotation blocks identified by name or index.
Definition: iexpression.h:610
A constant expression.
Definition: iexpression.h:97
An ordered collection of expressions identified by name or index.
Definition: iexpression.h:319
This interface represents a function call.
Definition: ifunction_call.h:52
This interface represents a function definition.
Definition: ifunction_definition.h:44
Semantics
All known semantics of functions definitions.
Definition: ifunction_definition.h:54
@ DS_INTRINSIC_DAG_ARRAY_LENGTH
The array length operator. See Array length operator.
Definition: ifunction_definition.h:401
@ DS_TERNARY
The ternary operator (conditional). See Ternary operator.
Definition: ifunction_definition.h:126
@ DS_CAST
The cast operator. See Cast operator.
Definition: ifunction_definition.h:81
@ DS_INTRINSIC_DAG_ARRAY_CONSTRUCTOR
The array constructor. See Array constructor.
Definition: ifunction_definition.h:399
@ DS_ARRAY_INDEX
The array index operator. See Array index operator.
Definition: ifunction_definition.h:89
@ DS_INTRINSIC_DAG_DECL_CAST
The decl_cast operator. See Decl_cast operator.
Definition: ifunction_definition.h:403
The execution context can be used to query status information like error and warning messages concern...
Definition: imdl_execution_context.h:131
Factory for various MDL interfaces and functions.
Definition: imdl_factory.h:53
Common base interface for all scene elements.
Definition: iscene_element.h:75
A transaction provides a consistent view on the database.
Definition: itransaction.h:82
virtual const base::IInterface * access(const char *name)=0
Retrieves an element from the database.
An ordered collection of types identified by name or index.
Definition: itype.h:650
The interface to MDL types.
Definition: itype.h:154
virtual const IInterface * get_interface(const Uuid &interface_id) const =0
Acquires a const interface from another.
Handle<Interface> make_handle_dup(Interface *iptr)
Converts passed-in interface pointer to a handle, without taking interface over.
Definition: handle.h:434
Handle<New_interface> get_interface() const
Returns a new handle for a possibly different interface type, similar to a dynamic cast,...
Definition: handle.h:348
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:289
virtual Uint32 release() const =0
Decrements the reference count.
int Sint32
32-bit signed integer.
Definition: types.h:46
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
#define mi_neuray_assert(expr)
If expr evaluates to true this macro shall have no effect.
Definition: assert.h:67
mi::Sint32 get_value(const mi::neuraylib::IValue *value, T &v)
Simplifies reading the value of mi::neuraylib::IValue into the corresponding classes from the base an...
Definition: ivalue.h:1255
Element_type
Distinguishes scene elements.
Definition: iscene_element.h:30
Smart-pointer handle class for interfaces, const and non-const version.
Expressions of the MDL type system.
Scene element Function_call.
Scene element Function_definition.
Scene element Material_instance.
API component that gives access to some MDL functionality.
Database transactions.
Types of the MDL type system.
Values of the MDL type system.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Assertions and compile-time assertions.