MDL SDK API nvidia_logo_transpbg.gif Up
set_get.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
6
7#ifndef MI_NEURAYLIB_SET_GET_H
8#define MI_NEURAYLIB_SET_GET_H
9
10#include <mi/base/config.h>
11#include <mi/base/handle.h>
12#include <mi/neuraylib/iarray.h>
14#include <mi/neuraylib/ibbox.h>
15#include <mi/neuraylib/icolor.h>
17#include <mi/neuraylib/ienum.h>
20#include <mi/neuraylib/iref.h>
23#include <mi/neuraylib/iuuid.h>
26
27#include <string>
28#include <vector>
29
30namespace mi {
31
56template<class T>
57mi::Sint32 set_value( mi::IData* data, const T& value)
58{
60 if( v) {
61 v->set_value( value);
62 return 0;
63 }
65 if( e) {
66 mi::Sint32 result = e->set_value( static_cast<mi::Sint32>( value));
67 return result == 0 ? 0 : -2;
68 }
69 return -1;
70}
71
75inline mi::Sint32 set_value( mi::IData* data, const char* value)
76{
78 if( s) {
79 s->set_c_str( value);
80 return 0;
81 }
83 if( r) {
84 mi::Sint32 result = r->set_reference( value);
85 return result == 0 ? 0 : -2;
86 }
88 if( e) {
89 mi::Sint32 result = e->set_value_by_name( value);
90 return result == 0 ? 0 : -2;
91 }
92 return -1;
93}
94
98inline mi::Sint32 set_value( mi::IData* data, const std::string& value)
99{
100 return set_value( data, value.c_str());
101}
102
106inline mi::Sint32 set_value( mi::IData* data, const mi::base::Uuid& value)
107{
109 if( u) {
110 u->set_uuid( value);
111 return 0;
112 }
113 return -1;
114}
115
126template <class T, Size DIM>
128{
129 typedef typename mi::Vector_type_traits<T,DIM>::Interface_type Vector_interface_type;
130 mi::base::Handle<Vector_interface_type> v( data->get_interface<Vector_interface_type>());
131 if( v) {
132 v->set_value( value);
133 return 0;
134 }
135 return -1;
136}
137
148template <class T, Size ROW, Size COL>
150{
151 typedef typename mi::Matrix_type_traits<T,ROW,COL>::Interface_type Matrix_interface_type;
152 mi::base::Handle<Matrix_interface_type> m( data->get_interface<Matrix_interface_type>());
153 if( m) {
154 m->set_value( value);
155 return 0;
156 }
157 return -1;
158}
159
163inline mi::Sint32 set_value( mi::IData* data, const mi::Color& value)
164{
166 if( c) {
167 c->set_value( value);
168 return 0;
169 }
171 if( c3) {
172 c3->set_value( value);
173 return 0;
174 }
175 return -1;
176}
177
181inline mi::Sint32 set_value( mi::IData* data, const mi::Spectrum& value)
182{
184 if( s) {
185 s->set_value( value);
186 return 0;
187 }
188 return -1;
189}
190
194inline mi::Sint32 set_value( mi::IData* data, const mi::Bbox3& value)
195{
197 if( b) {
198 b->set_value( value);
199 return 0;
200 }
201 return -1;
202}
203
218template<class T>
219mi::Sint32 set_value( mi::IData* data, mi::Size index, const T& value)
220{
222 if( c) {
223 mi::base::Handle<mi::IData> d( c->get_value<mi::IData>( index));
224 if( !d)
225 return -3;
226 return set_value( d.get(), value);
227 }
228 return -1;
229}
230
245template<class T>
246mi::Sint32 set_value( mi::IData* data, const char* key, const T& value)
247{
249 if( c) {
250 mi::base::Handle<mi::IData> d( c->get_value<mi::IData>( key));
251 if( !d)
252 return -3;
253 return set_value( d.get(), value);
254 }
255 return -1;
256}
257
274template<class T>
275mi::Sint32 set_value( mi::IData* data, const T* values, mi::Size n)
276{
278 if( a) {
279
281 if( da)
282 da->set_length( n);
283 else if( a->get_length() != n)
284 return -5;
285
286 for( mi::Size i = 0; i < n; ++i) {
287 mi::base::Handle<mi::IData> d( a->get_element<mi::IData>( i));
288 if( !d)
289 return -1;
290 mi::Sint32 result = set_value( d.get(), values[i]);
291 if( result != 0)
292 return result;
293 }
294 return 0;
295 }
296
297 return -1;
298}
299
315template<class T>
316mi::Sint32 set_value( mi::IData* data, const std::vector<T>& values)
317{
318 return set_value( data, values.data(), static_cast<mi::Size>( values.size()));
319}
320
340template<class T>
341mi::Sint32 get_value( const mi::IData* data, T& value)
342{
344 if( v) {
345 v->get_value( value);
346 return 0;
347 }
348// disable C4800: 'mi::Sint32' : forcing value to bool 'true' or 'false' (performance warning)
349// disable C4800: 'mi::Uint32' : forcing value to bool 'true' or 'false' (performance warning)
350#ifdef MI_COMPILER_MSC
351#pragma warning( push )
352#pragma warning( disable : 4800 )
353#endif
355 if( e) {
356 value = static_cast<T>( e->get_value());
357 return 0;
358 }
359#ifdef MI_COMPILER_MSC
360#pragma warning( pop )
361#endif
362 return -1;
363}
364
368inline mi::Sint32 get_value( const mi::IData* data, const char*& value)
369{
371 if( i) {
372 value = i->get_c_str();
373 return 0;
374 }
376 if( r) {
377 value = r->get_reference_name();
378 return 0;
379 }
381 if( e) {
382 value = e->get_value_by_name();
383 return 0;
384 }
385 return -1;
386}
387
391inline mi::Sint32 get_value( const mi::IData* data, mi::base::Uuid& value)
392{
394 if( u) {
395 value = u->get_uuid();
396 return 0;
397 }
398 return -1;
399}
400
411template <class T, Size DIM>
413{
414 typedef typename mi::Vector_type_traits<T,DIM>::Interface_type Vector_interface_type;
415 mi::base::Handle<const Vector_interface_type> v( data->get_interface<Vector_interface_type>());
416 if( v) {
417 value = v->get_value();
418 return 0;
419 }
420 return -1;
421}
422
433template <class T, Size ROW, Size COL>
435{
436 typedef typename mi::Matrix_type_traits<T,ROW,COL>::Interface_type Matrix_interface_type;
437 mi::base::Handle<const Matrix_interface_type> m( data->get_interface<Matrix_interface_type>());
438 if( m) {
439 value = m->get_value();
440 return 0;
441 }
442 return -1;
443}
444
448inline mi::Sint32 get_value( const mi::IData* data, mi::Color& value)
449{
451 if( c) {
452 value = c->get_value();
453 return 0;
454 }
456 if( c3) {
457 value = c3->get_value();
458 return 0;
459 }
460 return -1;
461}
462
466inline mi::Sint32 get_value( const mi::IData* data, mi::Spectrum& value)
467{
469 if( s) {
470 value = s->get_value();
471 return 0;
472 }
473 return -1;
474}
475
479inline mi::Sint32 get_value( const mi::IData* data, mi::Bbox3& value)
480{
482 if( b) {
483 value = b->get_value();
484 return 0;
485 }
486 return -1;
487}
488
502template<class T>
503mi::Sint32 get_value( const mi::IData* data, mi::Size index, T& value)
504{
506 if( c) {
507 mi::base::Handle<const mi::IData> d( c->get_value<mi::IData>( index));
508 if( !d)
509 return -3;
510 return get_value( d.get(), value);
511 }
512 return -1;
513}
514
528template<class T>
529mi::Sint32 get_value( const mi::IData* data, const char* key, T& value)
530{
532 if( c) {
533 mi::base::Handle<const mi::IData> d( c->get_value<mi::IData>( key));
534 if( !d)
535 return -3;
536 return get_value( d.get(), value);
537 }
538 return -1;
539}
540
553template<class T>
554mi::Sint32 get_value( const mi::IData* data, T* values, mi::Size n)
555{
557 if( a) {
558
559 if( a->get_length() != n)
560 return -5;
561
562 for( mi::Size i = 0; i < n; ++i) {
563 mi::base::Handle<const mi::IData> d( a->get_element<mi::IData>( i));
564 if( !d)
565 return -1;
566 mi::Sint32 result = get_value( d.get(), values[i]);
567 if( result != 0)
568 return result;
569 }
570 return 0;
571 }
572
573 return -1;
574}
575
587template<class T>
588mi::Sint32 get_value( const mi::IData* data, std::vector<T>& values)
589{
591 if( a)
592 values.resize( a->get_length());
593
594 return get_value( data, values.data(), static_cast<mi::Size>( values.size()));
595}
596
606inline mi::Sint32 get_value( const mi::IData* data, std::string& value)
607{
608 const char* c_str = 0;
609 const mi::Sint32 res = get_value( data, c_str);
610 if( c_str)
611 value.assign( c_str);
612 return res;
613}
614 // end group mi_neuray_types
616
633template<class T>
635 mi::neuraylib::IAttribute_set* attribute_set, const char* name, const T& value)
636{
637 mi::base::Handle<mi::IData> data( attribute_set->edit_attribute<mi::IData>( name));
638 if( !data)
639 return -4;
640 return set_value( data.get(), value);
641}
642
657template<class T>
659 mi::neuraylib::IAttribute_set* attribute_set, const char* name, mi::Size index, const T& value)
660{
661 mi::base::Handle<mi::IData> data( attribute_set->edit_attribute<mi::IData>( name));
662 if( !data)
663 return -4;
664 return set_value( data.get(), index, value);
665}
666
681template<class T>
683 mi::neuraylib::IAttribute_set* attribute_set, const char* name, const char* key, const T& value)
684{
685 mi::base::Handle<mi::IData> data( attribute_set->edit_attribute<mi::IData>( name));
686 if( !data)
687 return -4;
688 return set_value( data.get(), key, value);
689}
690
712template<class T>
714 mi::neuraylib::IAttribute_set* attribute_set, const char* name, const T* values, mi::Size n)
715{
716 mi::base::Handle<mi::IData> data( attribute_set->edit_attribute<mi::IData>( name));
717 if( !data)
718 return -4;
719 return set_value( data.get(), values, n);
720}
721
742template<class T>
744 mi::neuraylib::IAttribute_set* attribute_set, const char* name, const std::vector<T>& values)
745{
746 mi::base::Handle<mi::IData> data( attribute_set->edit_attribute<mi::IData>( name));
747 if( !data)
748 return -4;
749 return set_value( data.get(), values);
750}
751
763template<class T>
765 const mi::neuraylib::IAttribute_set* attribute_set, const char* name, T& value)
766{
768 if( !data)
769 return -4;
770 return get_value( data.get(), value);
771}
772
786template<class T>
788 const mi::neuraylib::IAttribute_set* attribute_set, const char* name, mi::Size index, T& value)
789{
791 if( !data)
792 return -4;
793 return get_value( data.get(), index, value);
794}
795
809template<class T>
811 const mi::neuraylib::IAttribute_set* attribute_set, const char* name, const char* key, T& value)
812{
814 if( !data)
815 return -4;
816 return get_value( data.get(), key, value);
817}
818
838template<class T>
840 const mi::neuraylib::IAttribute_set* attribute_set, const char* name, T* values, mi::Size n)
841{
843 if( !data)
844 return -4;
845 return get_value( data.get(), values, n);
846}
847
866template<class T>
868 const mi::neuraylib::IAttribute_set* attribute_set, const char* name, std::vector<T>& values)
869{
871 if( !data)
872 return -4;
873 return get_value( data.get(), values);
874}
875 // end group mi_neuray_types
877
878} // namespace mi
879
880#endif // MI_NEURAYLIB_SET_GET_H
This interface represents static arrays, i.e., arrays with a fixed number of elements.
Definition: iarray.h:37
This interface represents bounding boxes.
Definition: ibbox.h:28
This interface represents RGB colors.
Definition: icolor.h:52
This interface represents RGBA colors.
Definition: icolor.h:28
This interface represents collections.
Definition: idata.h:350
This interface is the base interface of all types.
Definition: idata.h:297
This interface represents dynamic arrays, i.e., arrays with a variable number of elements.
Definition: idynamic_array.h:36
This interface represents enums.
Definition: ienum.h:39
This interface represents simple numeric types.
Definition: inumber.h:25
A reference is an object that acts as a pointer to other database elements.
Definition: iref.h:25
This interface represents spectrums.
Definition: ispectrum.h:28
A simple string class.
Definition: istring.h:22
This interface represents UUIDs.
Definition: iuuid.h:24
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Axis-aligned N-dimensional bounding box class template of fixed dimension.
Definition: bbox.h:74
Standard RGBA color class with floating point elements and operations.
Definition: color.h:81
NxM-dimensional matrix class template of fixed dimensions.
Definition: matrix.h:367
Spectrum with floating point elements and operations.
Definition: spectrum.h:53
Fixed-size math vector class template with generic operations.
Definition: vector.h:286
The attribute set comprises all attributes attached to a database element.
Definition: iattribute_set.h:332
virtual IData * edit_attribute(const char *name)=0
Returns a mutable pointer to the attribute name.
virtual const IData * access_attribute(const char *name) const =0
Returns a const pointer to the attribute name.
Configuration of the Base API.
virtual const IInterface * get_interface(const Uuid &interface_id) const =0
Acquires a const interface from another.
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
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
mi::Sint32 set_value(mi::IData *data, const T &value)
Simplifies setting the value of mi::IData from the corresponding classes from the base and math API.
Definition: set_get.h:57
mi::Sint32 get_value(const mi::IData *data, T &value)
Simplifies reading the value of mi::IData into the corresponding classes from the base and math API.
Definition: set_get.h:341
Smart-pointer handle class for interfaces, const and non-const version.
Static array type.
Base class for database elements with attributes.
Bounding box type.
Color type.
Dynamic array type.
Numeric types.
Matrix types.
Numeric types.
Type that holds a reference to a database element.
Spectrum type.
String type.
UUID type.
Vector types.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
A 128 bit representation of a universally unique identifier (UUID or GUID).
Definition: uuid.h:26
Type traits.