annotation_wrapper.h File Reference
Description
Utility class for MDL annotations.
Code Example
annotation_wrapper.h
/***************************************************************************************************
* Copyright 2024 NVIDIA Corporation. All rights reserved.
**************************************************************************************************/
#ifndef MI_NEURAYLIB_ANNOTATION_WRAPPER_H
#define MI_NEURAYLIB_ANNOTATION_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/imaterial_instance.h>
#include <mi/neuraylib/imdl_factory.h>
#include <mi/neuraylib/itransaction.h>
#include <mi/neuraylib/itype.h>
#include <mi/neuraylib/ivalue.h>
namespace mi {
namespace neuraylib {
class Annotation_wrapper
{
public:
Annotation_wrapper(const mi::neuraylib::IAnnotation_block* anno_block);
mi::Size
get_annotation_count() const;
const char* get_annotation_name(mi::Size anno_index) const;
mi::Size
get_annotation_index(
const char* anno_name,
mi::Size offset = 0) const;
mi::Size
get_annotation_param_count(mi::Size anno_index) const;
const char* get_annotation_param_name(mi::Size anno_index, mi::Size param_index) const;
const IType* get_annotation_param_type(mi::Size anno_index, mi::Size param_index) const;
const IValue* get_annotation_param_value(
mi::Size anno_index, mi::Size param_index) const;
const IValue* get_annotation_param_value_by_name(
const char* anno_name, mi::Size param_index ) const;
template <class T>
mi::Sint32
get_annotation_param_value(
mi::Size anno_index, mi::Size param_index, T& value) const;
template <class T>
mi::Sint32
get_annotation_param_value_by_name(
const char* anno_name, mi::Size param_index, T& value ) const;
private:
mi::base::Handle< const mi::neuraylib::IAnnotation_block> m_anno_block;
};
// end group mi_neuray_mdl_elements
inline Annotation_wrapper::Annotation_wrapper(
const mi::neuraylib::IAnnotation_block* anno_block)
{
// anno_block == null is valid and will result in an annotation count of zero
m_anno_block = make_handle_dup(anno_block);
}
inline mi::Size
Annotation_wrapper::get_annotation_count() const
{
if (!m_anno_block)
return 0;
return m_anno_block->get_size();
}
inline const char* Annotation_wrapper::get_annotation_name(mi::Size anno_index) const
{
if (!m_anno_block || m_anno_block->get_size() <= anno_index)
return 0;
mi::base::Handle< const mi::neuraylib::IAnnotation> anno(
m_anno_block->get_annotation(anno_index));
if (!anno)
return 0;
return anno->get_name();
}
inline mi::Size
Annotation_wrapper::get_annotation_index(
const char* anno_name,
mi::Size offset) const
{
if (!anno_name || !m_anno_block)
return static_cast<mi::Size>(-1);
for (mi::Size i = offset, n = get_annotation_count(); i < n; ++i)
if (strcmp(anno_name, get_annotation_name(i)) == 0) //-V575 PVS
return i;
return static_cast<mi::Size>(-1);
}
inline mi::Size
Annotation_wrapper::get_annotation_param_count(mi::Size anno_index) const
{
if (!m_anno_block || m_anno_block->get_size() <= anno_index)
return 0;
mi::base::Handle< const mi::neuraylib::IAnnotation> anno(
m_anno_block->get_annotation(anno_index));
if (!anno)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression_list> expr_list(anno->get_arguments());
if (!expr_list)
return 0;
return expr_list->get_size();
}
inline const char* Annotation_wrapper::get_annotation_param_name(
mi::Size anno_index, mi::Size param_index) const
{
if (!m_anno_block || m_anno_block->get_size() <= anno_index)
return 0;
mi::base::Handle< const mi::neuraylib::IAnnotation> anno(
m_anno_block->get_annotation(anno_index));
if (!anno)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression_list> expr_list(anno->get_arguments());
if (!expr_list)
return 0;
if (expr_list->get_size() <= param_index)
return 0;
return expr_list->get_name(param_index);
}
inline const IType* Annotation_wrapper::get_annotation_param_type(
mi::Size anno_index, mi::Size param_index) const
{
if (!m_anno_block || m_anno_block->get_size() <= anno_index)
return 0;
mi::base::Handle< const mi::neuraylib::IAnnotation> anno(
m_anno_block->get_annotation(anno_index));
if (!anno)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression_list> expr_list(anno->get_arguments());
if (!expr_list)
return 0;
if (expr_list->get_size() <= param_index)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression> expr(expr_list->get_expression(param_index));
if (!expr)
return 0;
return expr->get_type();
}
inline const mi::neuraylib::IValue* Annotation_wrapper::get_annotation_param_value(
mi::Size anno_index, mi::Size param_index) const
{
if (!m_anno_block || m_anno_block->get_size() <= anno_index)
return 0;
mi::base::Handle< const mi::neuraylib::IAnnotation> anno(
m_anno_block->get_annotation(anno_index));
if (!anno)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression_list> expr_list(anno->get_arguments());
if (!expr_list)
return 0;
if (expr_list->get_size() <= param_index)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression> expr(expr_list->get_expression(param_index));
if (!expr)
return 0;
mi::base::Handle< const mi::neuraylib::IExpression_constant> c(
expr->get_interface<const mi::neuraylib::IExpression_constant>());
if (!c)
return 0;
return c->get_value();
}
inline const mi::neuraylib::IValue* Annotation_wrapper::get_annotation_param_value_by_name(
const char* anno_name, mi::Size param_index) const
{
mi::Size anno_index = get_annotation_index(anno_name);
if (anno_index == static_cast<mi::Size>(-1))
return 0;
return get_annotation_param_value(anno_index, param_index);
}
template <class T>
inline mi::Sint32
Annotation_wrapper::get_annotation_param_value(
mi::Size anno_index, mi::Size param_index, T& value) const
{
mi::base::Handle< const mi::neuraylib::IValue> v(get_annotation_param_value(
anno_index, param_index));
if (!v)
return -3;
return mi::neuraylib::get_value(v.get(), value);
}
template <class T>
inline mi::Sint32
Annotation_wrapper::get_annotation_param_value_by_name(
const char* anno_name, mi::Size param_index, T& value) const
{
mi::base::Handle< const mi::neuraylib::IValue> v(get_annotation_param_value_by_name(
anno_name, param_index));
if (!v)
return -3;
return mi::neuraylib::get_value(v.get(), value);
}
} // namespace neuraylib
} // namespace mi
#endif // MI_NEURAYLIB_ANNOTATION_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 interfaces for MDL annotations. More...