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_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
245 const IExpression_list* arguments = 0, Sint32* errors = 0) const;
246
251 template <class T>
252 T* create_instance( const IExpression_list* arguments = 0, Sint32* errors = 0) const
253 {
254 IScene_element* ptr_iscene_element = create_instance( arguments, errors);
255 if( !ptr_iscene_element)
256 return 0;
257 T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
258 ptr_iscene_element->release();
259 return ptr_T;
260 }
261
263
265
268
271
274
277
279 const std::string& get_name() const;
280
282
283private:
284
285 base::Handle<ITransaction> m_transaction;
287 base::Handle<IMdl_factory> m_mdl_factory;
288 Element_type m_type;
289 std::string m_name;
290};
291 // end group mi_neuray_mdl_elements
293
295 ITransaction* transaction, const char* name, IMdl_factory* mdl_factory)
296{
297 mi_neuray_assert( transaction);
298 mi_neuray_assert( name);
299
300 m_transaction = make_handle_dup( transaction);
301 m_name = name;
302 m_mdl_factory = make_handle_dup( mdl_factory);
303 m_access = transaction->access<IFunction_definition>( name);
304 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
305}
306
308{
309 return m_access.is_valid_interface();
310}
311
312
314{
315 if( !is_valid())
316 return false;
317
318 return m_access->is_valid( context);
319}
320
322{
323 return m_type;
324}
325
327{
328 if( !is_valid())
329 return 0;
330
331 return m_access->get_mdl_name();
332}
333
334inline const char* Definition_wrapper::get_module() const
335{
336 if( !is_valid())
337 return 0;
338
339 return m_access->get_module();
340}
341
343{
344 if( !is_valid())
345 return false;
346
347 return m_access->is_exported();
348}
349
351{
352 if( !is_valid())
353 return false;
354
355 return m_access->is_declarative();
356}
357
359{
360 if( !is_valid())
361 return false;
362
363 return m_access->is_material();
364}
365
367{
368 if( !is_valid())
369 return 0;
370
371 return m_access->get_parameter_count();
372}
373
374inline const char* Definition_wrapper::get_parameter_name( Size index) const
375{
376 if( !is_valid())
377 return 0;
378
379 return m_access->get_parameter_name( index);
380}
381
382inline Size Definition_wrapper::get_parameter_index( const char* name) const
383{
384 if( !is_valid())
385 return 0;
386
387 return m_access->get_parameter_index( name);
388}
389
391{
392 if( !is_valid())
393 return 0;
394
395 return m_access->get_parameter_types();
396}
397
399{
400 if( !is_valid())
401 return 0;
402
403 return m_access->get_return_type();
404}
405
406inline const char* Definition_wrapper::get_thumbnail() const
407{
408 if( !is_valid())
409 return 0;
410
411 return m_access->get_thumbnail();
412}
413
415{
416 if( !is_valid())
417 return 0;
418
419 return m_access->get_defaults();
420}
421
422template <class T>
424{
425 if( !is_valid())
426 return 0;
427
428 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
429 base::Handle<const IExpression> default_( defaults->get_expression( index));
430 if( !default_)
431 return -2;
434 if( !default_constant)
435 return -4;
436 base::Handle<const IValue> default_value( default_constant->get_value());
437 Sint32 result = get_value( default_value.get(), value);
438 return result == 0 ? 0 : -5;
439}
440
441template <class T>
442Sint32 Definition_wrapper::get_default( const char* name, T& value) const
443{
444 if( !is_valid())
445 return 0;
446
447 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
448 base::Handle<const IExpression> default_( defaults->get_expression( name));
449 if( !default_)
450 return -2;
453 if( !default_constant)
454 return -4;
455 base::Handle<const IValue> default_value( default_constant->get_value());
456 Sint32 result = get_value( default_value.get(), value);
457 return result == 0 ? 0 : -5;
458}
459
461{
462 if( !is_valid())
463 return 0;
464
465 return m_access->get_annotations();
466}
467
469{
470 if( !is_valid())
471 return 0;
472
473 return m_access->get_parameter_annotations();
474}
475
477{
478 if( !is_valid())
479 return 0;
480
481 return m_access->get_return_annotations();
482}
483
485{
486 if( !is_valid())
487 return 0;
488
489 return m_access->get_enable_if_conditions();
490}
491
493{
494 if( !is_valid())
495 return 0;
496
497 return m_access->get_enable_if_users( index);
498}
499
501{
502 if( !is_valid())
503 return static_cast<Size>( ~0);
504
505 return m_access->get_enable_if_user( index, u_index);
506}
507
509 const IExpression_list* arguments, Sint32* errors) const
510{
511 if( !is_valid())
512 return 0;
513
514 if( arguments)
515 return m_access->create_function_call( arguments, errors);
516
517 IFunction_definition::Semantics semantic = m_access->get_semantic();
522 || semantic == IFunction_definition::DS_CAST
524 if( errors)
525 *errors = -4;
526 return 0;
527 }
528
529 base::Handle<const IType_list> parameter_types( m_access->get_parameter_types());
530 base::Handle<const IExpression_list> defaults( m_access->get_defaults());
531 base::Handle<const IAnnotation_list> parameter_annotations(
532 m_access->get_parameter_annotations());
533 base::Handle<IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
535 m_mdl_factory->create_expression_factory( m_transaction.get()));
536 base::Handle<IExpression_list> local_arguments( ef->create_expression_list());
537
538 Size count = m_access->get_parameter_count();
539 for( Size i = 0; i < count; ++i) {
540 const char* name = m_access->get_parameter_name( i);
541 base::Handle<const IExpression> default_( defaults->get_expression( name));
542 if( !default_) {
543 base::Handle<const IType> type( parameter_types->get_type( i));
545 parameter_annotations->get_annotation_block( name));
546 base::Handle<IValue> value( vf->create( type.get(), block.get()));
547 base::Handle<IExpression> expr( ef->create_constant( value.get()));
548 local_arguments->add_expression( name, expr.get());
549 }
550 }
551 return m_access->create_function_call( local_arguments.get(), errors);
552}
553
555{
556 m_transaction->retain();
557 return m_transaction.get();
558}
559
561{
562 m_mdl_factory->retain();
563 return m_mdl_factory.get();
564}
565
567{
568 m_access->retain();
569 return m_access.get();
570}
571
573{
574 return m_type;
575}
576
577inline const std::string& Definition_wrapper::get_name() const
578{
579 return m_name;
580}
581
582} // namespace neuraylib
583
584} // namespace mi
585
586#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:334
Sint32 get_default(Size index, T &value) const
Returns the default of a non-array parameter.
Definition: definition_wrapper.h:423
const IExpression_list * get_enable_if_conditions() const
Returns the enable_if conditions of all parameters.
Definition: definition_wrapper.h:484
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:294
const IExpression_list * get_defaults() const
Returns the defaults of all parameters.
Definition: definition_wrapper.h:414
const char * get_mdl_definition() const
Returns the MDL name of the material or function definition.
Definition: definition_wrapper.h:326
const IAnnotation_list * get_parameter_annotations() const
Returns the annotations of all parameters.
Definition: definition_wrapper.h:468
const IType * get_return_type() const
Returns the return type.
Definition: definition_wrapper.h:398
const IType_list * get_parameter_types() const
Returns the types of all parameters.
Definition: definition_wrapper.h:390
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:500
bool is_material() const
Indicates whether the definition represents a material.
Definition: definition_wrapper.h:358
bool is_exported() const
Indicates whether the material or function definition is exported by its module.
Definition: definition_wrapper.h:342
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:313
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:252
IMdl_factory * get_mdl_factory() const
Get the MDL factory.
Definition: definition_wrapper.h:560
ITransaction * get_transaction() const
Get the transaction.
Definition: definition_wrapper.h:554
Element_type get_type() const
Indicates whether the definition wrapper acts on a material definition or on a function definition.
Definition: definition_wrapper.h:321
bool is_declarative() const
Indicates whether the material or function definition is declarative.
Definition: definition_wrapper.h:350
const char * get_parameter_name(Size index) const
Returns the name of the parameter at index.
Definition: definition_wrapper.h:374
Element_type get_element_type() const
Get the element type.
Definition: definition_wrapper.h:572
const IAnnotation_block * get_return_annotations() const
Returns the annotations of the return type.
Definition: definition_wrapper.h:476
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:492
const std::string & get_name() const
Get the DB name of the MDL function or material definition.
Definition: definition_wrapper.h:577
const IFunction_definition * get_scene_element() const
Get the MDL function or material definition.
Definition: definition_wrapper.h:566
bool is_valid() const
Indicates whether the definition wrapper is in a valid state.
Definition: definition_wrapper.h:307
const IAnnotation_block * get_annotations() const
Returns the annotations for a material or function definition.
Definition: definition_wrapper.h:460
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:508
Size get_parameter_index(const char *name) const
Returns the index position of a parameter.
Definition: definition_wrapper.h:382
const char * get_thumbnail() const
Returns the resolved file name of the thumbnail image for this MDL definition.
Definition: definition_wrapper.h:406
Size get_parameter_count() const
Returns the number of parameters.
Definition: definition_wrapper.h:366
An annotation block is an array of annotations.
Definition: iexpression.h:575
An ordered collection of annotation blocks identified by name or index.
Definition: iexpression.h:603
A constant expression.
Definition: iexpression.h:96
An ordered collection of expressions identified by name or index.
Definition: iexpression.h:317
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:393
@ 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:391
@ 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:395
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:646
The interface to MDL types.
Definition: itype.h:151
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.