-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathParameterizedObject.h
153 lines (125 loc) · 5.16 KB
/
ParameterizedObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2022-2024 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "AnariAny.h"
// anari
#include "anari/anari_cpp/Traits.h"
// stl
#include <cstring>
#include <memory>
#include <utility>
#include <vector>
namespace helium {
struct ParameterizedObject
{
ParameterizedObject() = default;
virtual ~ParameterizedObject() = default;
// Return true if there was a parameter set with the corresponding 'name'
bool hasParam(const std::string &name) const;
// Return true if there was a parameter set with the corresponding 'name' and
// if it matches the corresponding type
bool hasParam(const std::string &name, ANARIDataType type) const;
// Set the value of the parameter 'name', or add it if it doesn't exist yet
//
// Returns 'true' if the value for that parameter actually changed
bool setParam(const std::string &name, ANARIDataType type, const void *v);
// Set the value of the parameter 'name', or add it if it doesn't exist yet
//
// Returns 'true' if the value for that parameter actually changed
template <typename T>
bool setParam(const std::string &name, const T &v);
// Get the value of the parameter associated with 'name', or return
// 'valueIfNotFound' if the parameter isn't set. This is strongly typed by
// using the anari::ANARITypeFor<T> to get the ANARIDataType of the provided
// C++ type 'T'. Implementations should specialize `anari::ANARITypeFor<>` for
// things like vector math types and matricies. This function is not able to
// access ANARIObject or ANARIString parameters, see special methods for
// getting parameters of those types.
template <typename T>
T getParam(const std::string &name, T valIfNotFound) const;
// Get the value of the parameter associated with 'name' and write it to
// location 'v', returning whether the was actually read. Just like the
// templated version above, this requires that 'type' exactly match what the
// application set. This function also cannot get objects or strings.
bool getParam(const std::string &name, ANARIDataType type, void *v) const;
// Get the pointer to an object parameter (returns null if not present). While
// ParameterizedObject will track object lifetime appropriately, accessing
// an object parameter does _not_ influence lifetime considerations -- devices
// should consider using `helium::IntrusivePtr<>` to guarantee correct
// lifetime handling.
template <typename T>
T *getParamObject(const std::string &name) const;
// Get a string parameter value
std::string getParamString(
const std::string &name, const std::string &valIfNotFound) const;
// Get/Set the container holding the value of a parameter (default constructed
// AnariAny if not present). Getting this container will create a copy of the
// parameter value, which for objects will incur the correct ref count changes
// accordingly (handled by AnariAny).
AnariAny getParamDirect(const std::string &name) const;
void setParamDirect(const std::string &name, const AnariAny &v);
// Remove the value of the parameter associated with 'name'.
//
// Returns 'true' if anything actually happened
bool removeParam(const std::string &name);
// Remove all set parameters
//
// Returns 'true' if anything actually happened
bool removeAllParams();
protected:
using Param = std::pair<std::string, AnariAny>;
using ParameterList = std::vector<Param>;
ParameterList::iterator params_begin();
ParameterList::iterator params_end();
private:
// Data members //
const Param *findParam(const std::string &name) const;
Param *findParam(const std::string &name);
ParameterList m_params;
};
// Inlined ParameterizedObject definitions ////////////////////////////////////
template <typename T>
inline bool ParameterizedObject::setParam(const std::string &name, const T &v)
{
constexpr ANARIDataType type = anari::ANARITypeFor<T>::value;
return setParam(name, type, &v);
}
template <>
inline bool ParameterizedObject::setParam(
const std::string &name, const std::string &v)
{
return setParam(name, ANARI_STRING, v.c_str());
}
template <>
inline bool ParameterizedObject::setParam(
const std::string &name, const bool &v)
{
uint8_t b = v;
return setParam(name, ANARI_BOOL, &b);
}
template <typename T>
inline T ParameterizedObject::getParam(
const std::string &name, T valIfNotFound) const
{
constexpr ANARIDataType type = anari::ANARITypeFor<T>::value;
static_assert(!anari::isObject(type),
"use ParameterizedObect::getParamObject() for getting objects");
static_assert(type != ANARI_STRING && !std::is_same_v<T, std::string>,
"use ParameterizedObject::getParamString() for getting strings");
auto *p = findParam(name);
return p && p->second.is(type) ? p->second.get<T>() : valIfNotFound;
}
template <>
inline bool ParameterizedObject::getParam(
const std::string &name, bool valIfNotFound) const
{
auto *p = findParam(name);
return p && p->second.is(ANARI_BOOL) ? p->second.get<bool>() : valIfNotFound;
}
template <typename T>
inline T *ParameterizedObject::getParamObject(const std::string &name) const
{
auto *p = findParam(name);
return p ? p->second.getObject<T>() : nullptr;
}
} // namespace helium