HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
extComputationContext.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_EXT_COMPUTATION_CONTEXT_H
8 #define PXR_IMAGING_HD_EXT_COMPUTATION_CONTEXT_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/base/vt/value.h"
13 
15 
16 ///
17 /// Interface class that defines the execution environment for the client
18 /// to run a computation.
19 ///
21 public:
22  HdExtComputationContext() = default;
23  virtual ~HdExtComputationContext();
24 
25  ///
26  /// Obtains the value of an named input to the computation.
27  ///
28  /// The code will issue a coding error and return a empty value
29  /// if the input is missing.
30  ///
31  virtual const VtValue &GetInputValue(const TfToken &name) const = 0;
32 
33  ///
34  /// Obtains the value of an named input to the computation.
35  ///
36  /// The code will issue a coding error and return a default constructed
37  /// value if the input is missing or is of the wrong type.
38  ///
39  template <typename T>
40  T GetTypedInputValue(const TfToken &name) const;
41 
42  ///
43  /// Obtains the value of an named input to the computation.
44  ///
45  /// If the input isn't present, a nullptr will be returned.
46  ///
47  virtual const VtValue *GetOptionalInputValuePtr(
48  const TfToken &name) const = 0;
49 
50  ///
51  /// Obtains the value of an named input to the computation.
52  ///
53  /// If the input isn't present, a nullptr will be returned. If the input
54  /// is of the wrong type, a coding error will be issued and a nullptr will
55  /// be returned.
56  ///
57  template <typename T>
58  const T *GetOptionalTypedInputValuePtr(const TfToken &name) const;
59 
60  ///
61  /// Set the value of the specified output.
62  ///
63  virtual void SetOutputValue(const TfToken &name, const VtValue &output) = 0;
64 
65  ///
66  /// Set the value of the specified output.
67  ///
68  template <typename T>
69  void SetTypedOutputValue(const TfToken &name, const T &output);
70 
71  ///
72  /// Called to indicate an error occurred while executing a computation
73  /// so that its output are invalid.
74  ///
75  virtual void RaiseComputationError() = 0;
76 
77 private:
80  = delete;
81 };
82 
83 template <typename T>
84 T
86 {
87  const VtValue &v = GetInputValue(name);
88  if (!v.IsHolding<T>()) {
90  "HdExtComputationContext::GetTypedInputValue<T> called with type T"
91  "not matching the type of the input value for '%s'.",
92  name.GetText());
93  return {};
94  }
95 
96  return v.UncheckedGet<T>();
97 }
98 
99 template <typename T>
100 const T *
102 {
103  const VtValue * const v = GetOptionalInputValuePtr(name);
104  if (!v) {
105  return nullptr;
106  }
107 
108  if (!v->IsHolding<T>()) {
110  "HdExtComputationContext::GetOptionalTypedInputValue<T> called with "
111  "type T not matching the type of the input value for '%s'.",
112  name.GetText());
113  return nullptr;
114  }
115 
116  return &(v->UncheckedGet<T>());
117 }
118 
119 template <typename T>
120 void
122 SetTypedOutputValue(const TfToken &name, const T &output)
123 {
124  SetOutputValue(name, VtValue(output));
125 }
126 
128 
129 #endif // PXR_IMAGING_HD_EXT_COMPUTATION_CONTEXT_H
T const & UncheckedGet() const &
Definition: value.h:1104
const GLdouble * v
Definition: glcorearb.h:837
#define TF_CODING_ERROR
virtual ~HdExtComputationContext()
Definition: token.h:70
T GetTypedInputValue(const TfToken &name) const
virtual const VtValue * GetOptionalInputValuePtr(const TfToken &name) const =0
virtual void SetOutputValue(const TfToken &name, const VtValue &output)=0
void SetTypedOutputValue(const TfToken &name, const T &output)
GLuint const GLchar * name
Definition: glcorearb.h:786
char const * GetText() const
Definition: token.h:179
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
virtual void RaiseComputationError()=0
const T * GetOptionalTypedInputValuePtr(const TfToken &name) const
bool IsHolding() const
Definition: value.h:1064
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdExtComputationContext()=default
virtual const VtValue & GetInputValue(const TfToken &name) const =0
Definition: value.h:146