MDL SDK API nvidia_logo_transpbg.gif Up
definition_wrapper.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 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_material() const;
94
97
102 const char* get_parameter_name( Size index) const;
103
108 Size get_parameter_index( const char* name) const;
109
111 const IType_list* get_parameter_types() const;
112
114 const IType* get_return_type() const;
115
123 const char* get_thumbnail() const;
124
125
127
129
135 const IExpression_list* get_defaults() const;
136
154 template<class T>
155 Sint32 get_default( Size index, T& value) const;
156
170 template <class T>
171 Sint32 get_default( const char* name, T& value) const;
172
174
176
178 const IAnnotation_block* get_annotations() const;
179
187
190
198
205 Size get_enable_if_users( Size index) const;
206
214 Size get_enable_if_user( Size index, Size u_index) const;
215
217
219
244 const IExpression_list* arguments = 0, Sint32* errors = 0) const;
245
250 template <class T>
251 T* create_instance( const IExpression_list* arguments = 0, Sint32* errors = 0) const
252 {
253 IScene_element* ptr_iscene_element = create_instance( arguments, errors);
254 if( !ptr_iscene_element)
255 return 0;
256 T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
257 ptr_iscene_element->release();
258 return ptr_T;
259 }
260
262
264
267
270
273
276
278 const std::string& get_name() const;
279
281
282private:
283
284 base::Handle<ITransaction> m_transaction;
286 base::Handle<IMdl_factory> m_mdl_factory;
287 Element_type m_type;
288 std::string m_name;
289};
290 // end group mi_neuray_mdl_elements
292
294 ITransaction* transaction, const char* name, IMdl_factory* mdl_factory)
295{
296 mi_neuray_assert( transaction);
297 mi_neuray_assert( name);
298
299 m_transaction = make_handle_dup( transaction);
300 m_name = name;
301 m_mdl_factory = make_handle_dup( mdl_factory);
302 m_access = transaction->access<IFunction_definition>( name);
303 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
304}
305
307{
308 return m_access.is_valid_interface();
309}
310
311
313{
314 if( !is_valid())
315 return false;
316
317 return m_access->is_valid( context);
318}
319
321{
322 return m_type;
323}
324
326{
327 if( !is_valid())
328 return 0;
329
330 return m_access->get_mdl_name();
331}
332
333inline const char* Definition_wrapper::get_module() const
334{
335 if( !is_valid())
336 return 0;
337
338 return m_access->get_module();
339}
340
342{
343 if( !is_valid())
344 return false;
345
346 return m_access->is_exported();
347}
348
350{
351 if( !is_valid())
352 return false;
353
354 return m_access->is_material();
355}
356
358{
359 if( !is_valid())
360 return 0;
361
362 return m_access->get_parameter_count();
363}
364
365inline const char* Definition_wrapper::get_parameter_name( Size index) const
366{
367 if( !is_valid())
368 return 0;
369
370 return m_access->get_parameter_name( index);
371}
372
373inline Size Definition_wrapper::get_parameter_index( const char* name) const
374{
375 if( !is_valid())
376 return 0;
377
378 return m_access->get_parameter_index( name);
379}
380
382{
383 if( !is_valid())
384 return 0;
385
386 return m_access->get_parameter_types();
387}
388
390{
391 if( !is_valid())
392 return 0;
393
394 return m_access->get_return_type();
395}
396
397inline const char* Definition_wrapper::get_thumbnail() const
398{
399 if( !is_valid())
400 return 0;
401
402 return m_access->get_thumbnail();
403}
404
406{
407 if( !is_valid())
408 return 0;
409
410 return m_access->get_defaults();
411}
412
413template <class T>
415{
416 if( !is_valid())
417 return 0;
418
419 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
420 base::Handle<const IExpression> default_( defaults->get_expression( index));
421 if( !default_)
422 return -2;
425 if( !default_constant)
426 return -4;
427 base::Handle<const IValue> default_value( default_constant->get_value());
428 Sint32 result = get_value( default_value.get(), value);
429 return result == 0 ? 0 : -5;
430}
431
432template <class T>
433Sint32 Definition_wrapper::get_default( const char* name, T& value) const
434{
435 if( !is_valid())
436 return 0;
437
438 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
439 base::Handle<const IExpression> default_( defaults->get_expression( name));
440 if( !default_)
441 return -2;
444 if( !default_constant)
445 return -4;
446 base::Handle<const IValue> default_value( default_constant->get_value());
447 Sint32 result = get_value( default_value.get(), value);
448 return result == 0 ? 0 : -5;
449}
450
452{
453 if( !is_valid())
454 return 0;
455
456 return m_access->get_annotations();
457}
458
460{
461 if( !is_valid())
462 return 0;
463
464 return m_access->get_parameter_annotations();
465}
466
468{
469 if( !is_valid())
470 return 0;
471
472 return m_access->get_return_annotations();
473}
474
476{
477 if( !is_valid())
478 return 0;
479
480 return m_access->get_enable_if_conditions();
481}
482
484{
485 if( !is_valid())
486 return 0;
487
488 return m_access->get_enable_if_users( index);
489}
490
492{
493 if( !is_valid())
494 return static_cast<Size>( ~0);
495
496 return m_access->get_enable_if_user( index, u_index);
497}
498
500 const IExpression_list* arguments, Sint32* errors) const
501{
502 if( !is_valid())
503 return 0;
504
505 if( arguments)
506 return m_access->create_function_call( arguments, errors);
507
508 IFunction_definition::Semantics semantic = m_access->get_semantic();
513 || semantic == IFunction_definition::DS_CAST) {
514 if( errors)
515 *errors = -4;
516 return 0;
517 }
518
519 base::Handle<const IType_list> parameter_types( m_access->get_parameter_types());
520 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
521 base::Handle<const IAnnotation_list> parameter_annotations(
522 m_access->get_parameter_annotations());
523 base::Handle<IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
525 m_mdl_factory->create_expression_factory( m_transaction.get()));
526 base::Handle<IExpression_list> local_arguments( ef->create_expression_list());
527
528 Size count = m_access->get_parameter_count();
529 for( Size i = 0; i < count; ++i) {
530 const char* name = m_access->get_parameter_name( i);
531 base::Handle<const IExpression> default_( defaults->get_expression( name));
532 if( !default_) {
533 base::Handle<const IType> type( parameter_types->get_type( i));
535 parameter_annotations->get_annotation_block( name));
536 base::Handle<IValue> value( vf->create( type.get(), block.get()));
537 base::Handle<IExpression> expr( ef->create_constant( value.get()));
538 local_arguments->add_expression( name, expr.get());
539 }
540 }
541 return m_access->create_function_call( local_arguments.get(), errors);
542}
543
545{
546 m_transaction->retain();
547 return m_transaction.get();
548}
549
551{
552 m_mdl_factory->retain();
553 return m_mdl_factory.get();
554}
555
557{
558 m_access->retain();
559 return m_access.get();
560}
561
563{
564 return m_type;
565}
566
567inline const std::string& Definition_wrapper::get_name() const
568{
569 return m_name;
570}
571
572} // namespace neuraylib
573
574} // namespace mi
575
576#endif // MI_NEURAYLIB_DEFINITION_WRAPPER_H
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
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:333
Sint32 get_default(Size index, T &value) const
Returns the default of a non-array parameter.
Definition: definition_wrapper.h:414
const IExpression_list * get_enable_if_conditions() const
Returns the enable_if conditions of all parameters.
Definition: definition_wrapper.h:475
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:293
const IExpression_list * get_defaults() const
Returns the defaults of all parameters.
Definition: definition_wrapper.h:405
const char * get_mdl_definition() const
Returns the MDL name of the material or function definition.
Definition: definition_wrapper.h:325
const IAnnotation_list * get_parameter_annotations() const
Returns the annotations of all parameters.
Definition: definition_wrapper.h:459
const IType * get_return_type() const
Returns the return type.
Definition: definition_wrapper.h:389
const IType_list * get_parameter_types() const
Returns the types of all parameters.
Definition: definition_wrapper.h:381
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:491
bool is_material() const
Indicates whether the definition represents a material.
Definition: definition_wrapper.h:349
bool is_exported() const
Indicates whether the material or function definition is exported by its module.
Definition: definition_wrapper.h:341
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:312
T * create_instance(const IExpression_list *arguments=0, Sint32 *errors=0) const
Creates an instance of the material or function definition.
Definition: definition_wrapper.h:251
IMdl_factory * get_mdl_factory() const
Get the MDL factory.
Definition: definition_wrapper.h:550
ITransaction * get_transaction() const
Get the transaction.
Definition: definition_wrapper.h:544
Element_type get_type() const
Indicates whether the definition wrapper acts on a material definition or on a function definition.
Definition: definition_wrapper.h:320
const char * get_parameter_name(Size index) const
Returns the name of the parameter at index.
Definition: definition_wrapper.h:365
Element_type get_element_type() const
Get the element type.
Definition: definition_wrapper.h:562
const IAnnotation_block * get_return_annotations() const
Returns the annotations of the return type.
Definition: definition_wrapper.h:467
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:483
const std::string & get_name() const
Get the DB name of the MDL function or material definition.
Definition: definition_wrapper.h:567
const IFunction_definition * get_scene_element() const
Get the MDL function or material definition.
Definition: definition_wrapper.h:556
bool is_valid() const
Indicates whether the definition wrapper is in a valid state.
Definition: definition_wrapper.h:306
const IAnnotation_block * get_annotations() const
Returns the annotations for a material or function definition.
Definition: definition_wrapper.h:451
IFunction_call * create_instance(const IExpression_list *arguments=0, Sint32 *errors=0) const
Creates an instance of the material or function definition.
Definition: definition_wrapper.h:499
Size get_parameter_index(const char *name) const
Returns the index position of a parameter.
Definition: definition_wrapper.h:373
const char * get_thumbnail() const
Returns the resolved file name of the thumbnail image for this MDL definition.
Definition: definition_wrapper.h:397
Size get_parameter_count() const
Returns the number of parameters.
Definition: definition_wrapper.h:357
An annotation block is an array of annotations.
Definition: iexpression.h:573
An ordered collection of annotation blocks identified by name or index.
Definition: iexpression.h:601
A constant expression.
Definition: iexpression.h:94
An ordered collection of expressions identified by name or index.
Definition: iexpression.h:315
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:391
@ 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:389
@ DS_ARRAY_INDEX
The array index operator. See Array index operator.
Definition: ifunction_definition.h:89
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:83
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:540
The interface to MDL types.
Definition: itype.h:51
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:439
Handle<New_interface> get_interface() const
Returns a new handle for a possibly different interface type, similar to a dynamic cast,...
Definition: handle.h:353
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
virtual Uint32 release() const =0
Decrements the reference count.
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
signed int Sint32
32-bit signed integer.
Definition: types.h:46
#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:1254
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.