HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
APEX_Include.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: APEX_Include.h (APEX Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __APEX_INCLUDE_H__
12 #define __APEX_INCLUDE_H__
13 
14 #include "APEX_API.h"
15 #include "APEX_Names.h"
16 #include "APEX_COW.h"
17 
18 #include <UN/UN_Include.h>
19 
20 #include <UT/UT_Array.h>
21 #include <UT/UT_ArrayMap.h>
22 #include <UT/UT_Assert.h>
23 #include <UT/UT_Debug.h>
24 #include <UT/UT_Format.h>
25 #include <UT/UT_Map.h>
26 #include <UT/UT_Matrix3.h> // IWYU pragma: export
27 #include <UT/UT_Matrix4.h> // IWYU pragma: export
28 #include <UT/UT_Options.h>
29 #include <UT/UT_Ramp.h> // IWYU pragma: export
30 #include <UT/UT_Set.h>
31 #include <UT/UT_SharedPtr.h>
32 #include <UT/UT_StringArray.h>
33 #include <UT/UT_StringHolder.h>
34 #include <UT/UT_Tracing.h>
35 #include <UT/UT_Tuple.h>
36 #include <UT/UT_Vector2.h> // IWYU pragma: export
37 #include <UT/UT_Vector3.h> // IWYU pragma: export
38 #include <UT/UT_Vector4.h> // IWYU pragma: export
39 #include <SYS/SYS_Types.h>
40 
41 #include <algorithm>
42 #include <any>
43 #include <functional>
44 #include <string>
45 #include <tuple>
46 #include <type_traits>
47 #include <typeinfo>
48 #include <utility>
49 #include <vector>
50 
51 #include <stddef.h>
52 
53 // Forward declarations from <PRM/PRM_Parm.h>
54 class PRM_DataItem;
56 
57 namespace apex
58 {
59 
60 using Bool = bool;
61 using Int = exint;
62 using Float = double;
70 class FloatRamp : public UT_Ramp
71 {
72 public:
74  {
75  this->addNode(0, UT_FRGBA(0.0, 0.0, 0.0, 0.0));
76  this->addNode(1, UT_FRGBA(1.0, 1.0, 1.0, 1.0));
77  };
78  FloatRamp(const UT_Ramp &ramp) : UT_Ramp{ramp} {}
79 };
80 class ColorRamp : public UT_Ramp
81 {
82 public:
84  {
85  this->addNode(0, UT_FRGBA(0.0, 0.0, 0.0, 0.0));
86  this->addNode(1, UT_FRGBA(1.0, 1.0, 1.0, 1.0));
87  };
88  ColorRamp(const UT_Ramp &ramp) : UT_Ramp{ramp} {}
89 };
90 
103 
104 // Quaternion is not an exposed type. It's only used within callbacks
105 // internally to ensure we consistently use the same base type. Externally,
106 // Vector4 is used in the callback signatures.
108 
110 
111 template <typename T>
113 {
114 public:
115  VariadicArg() : myNames(), myData() {};
116  VariadicArg(std::initializer_list<std::pair<const char *, T *>> inputs) : myNames(), myData()
117  {
118  myNames.setCapacity(inputs.size()); // optional metadata
119  myData.setCapacity(inputs.size());
120  for (auto &[name, p] : inputs)
121  {
122  myNames.emplace_back(name);
123  myData.append(p);
124  }
125  }
126 
127  using value_type = T;
128 
129  void append(const UT_StringHolder &name, T *item)
130  {
131  myNames.emplace_back(name);
132  myData.append(item);
133  }
134 
135  exint size() const
136  {
137  if (myData.size() != myNames.size())
138  {
139  UTdebugPrint(this, myData.size(), myNames.size());
140  }
141  UT_ASSERT(myData.size() == myNames.size());
142  return myData.size();
143  }
144 
145  const UT_Array<UT_StringHolder> &names() const { return myNames; }
146 
148  {
149  return myData[idx];
150  }
151 
152  const T *operator[](exint idx) const
153  {
154  return myData[idx];
155  }
156 
158  {
159  return myNames.find(name);
160  }
161 
162  void toArray(UT_Array<T> &arr) const
163  {
164  arr.setSize(myData.size());
165  for (exint i = 0, n = myData.size(); i < n; ++i)
166  arr[i] = *operator[](i);
167  }
168 
171 
172  iterator begin() { return myData.begin(); }
173  iterator end() { return myData.end(); }
174  const_iterator begin() const { return myData.begin(); }
175  const_iterator end() const { return myData.end(); }
176 
177  /// A minimal array like adaptor which dereferences the VariadicArgs data
178  /// before returning it. This can be useful to write generic code which
179  /// deals either with VariadicArgs or ApexArrays.
180  /// Note: Not compatible with ranged based for loops
181  template<typename IterT>
183  {
184  public:
185  using value_type = T;
186 
187  exint size() const
188  {
189  return std::distance(myBegin, myEnd);
190  }
191 
193  {
194  return *myBegin[index];
195  }
196 
197  const T &operator[](exint index) const
198  {
199  return *myBegin[index];
200  }
201 
202  private:
203  friend class VariadicArg<T>;
204  IndirectRange() = delete;
205  IndirectRange(IterT begin, IterT end) : myBegin{begin}, myEnd{end} {}
206  IterT myBegin, myEnd;
207  };
208 
210  {
211  return IndirectRange<iterator>(begin(), end());
212  }
213 
215  {
217  }
218 
219 private:
221  UT_Array<T *> myData;
222 };
223 
225 {
226  NOOP, /// Do nothing; ignored after compilation.
227 
228  NORMAL, /// Normal callback which is unconditionally executed
229 
230  /// @{ flow control
231 
232  IF_BEGIN, /// Callback is used as an IF_BEGIN: expects the signature to be exactly a Bool called
233  /// condition + scope/spare
234  IF_END, /// Callback marks the end of an if block: expects the signture to be exactly
235  /// scope/spare.
236 
237  LOOP_BEGIN, /// Marks the start of an arbitrary loop, expects RunData to be an iterator
238  LOOP_END /// Marks the end of an arbitrary loop, expects RunData to be an iterator and shares
239  /// that rundata with the corresponding LOOP_BEGIN.
240 
241  /// @}
242 };
243 
244 template <typename... T, size_t... I>
245 auto
246 makeref(UT_Tuple<T...> &t, std::index_sequence<I...>)
247 {
248  return UTlhsTuple(*std::get<I>(t)...);
249 }
250 
251 template <typename... T>
252 auto
254 {
255  return makeref<T...>(t, std::make_index_sequence<sizeof...(T)>{});
256 }
257 
259 
260 static inline UT_StringArray
261 parseTag(const UT_StringRef &tag_key)
262 {
264  std::string tag_name(tag_key);
265  std::string theprefix;
266  std::string delimiter = ".";
267 
268  auto pos = tag_name.find(delimiter);
269  while (pos != std::string::npos)
270  {
271  theprefix = tag_name.substr(0, pos);
272  if (pos != std::string::npos)
273  tag_name.erase(0, pos + delimiter.length());
274  result.append(theprefix);
275  pos = tag_name.find(delimiter);
276  }
277  result.append(tag_name);
278  return result;
279 }
280 
281 static const char *
282 stripQualifiers(const UT_StringLit &portname)
283 {
284  if (portname.length() > 1 && portname.c_str()[0] == '*')
285  return portname.c_str() + 1;
286  return portname.c_str();
287 }
288 
289 #define APEX_ID(IdType) \
290  class IdType : public UN_DataIndex \
291  { \
292  public: \
293  explicit IdType(UN_DataIndex data_index = UN_DataIndex()) : UN_DataIndex(data_index) \
294  { \
295  } \
296  };
297 
298 #define APEX_INDEX(IndexType) \
299  class IndexType : public UN_DataIndex \
300  { \
301  public: \
302  explicit IndexType(UN_DataIndex data_index = UN_DataIndex()) : UN_DataIndex(data_index) \
303  { \
304  } \
305  };
306 
307 APEX_ID(APEX_StickyNoteID)
308 APEX_INDEX(APEX_StickyNoteIndex)
309 
310 template <typename T>
311 struct is_apex_array : std::false_type
312 {
313 };
314 
315 template <typename T>
316 struct is_apex_array<ApexArray<T>> : std::true_type
317 {
318 };
319 
321 #define APEX_INVALID_GRAPHDATAID -1
322 
323 } // end namespace apex
324 
325 
326 // Apex Scope ID. It is currently defined as an index, because
327 // APEX (subnets) use it solely as a direct index into its arrays.
328 APEX_INDEX(APEX_ScopeID)
329 
330 
331 // APEX data IDs based on UN concepts:
332 using APEX_NodeID = UN_NodeID;
333 using APEX_SubnetID = UN_SubnetID;
335 using APEX_PortID = UN_PortID;
336 using APEX_WireID = UN_WireID;
337 
339 
342 
343 
344 #define APEX_ID_CLEARER(IdType) \
345  template <> \
346  struct DefaultClearer<IdType> \
347  { \
348  static void clear(IdType &v) \
349  { \
350  v = IdType(); \
351  } \
352  static bool isClear(const IdType &v) \
353  { \
354  return !v.isValid(); \
355  } \
356  static void clearConstruct(IdType *p) \
357  { \
358  clear(*p); \
359  } \
360  static const bool clearNeedsDestruction = false; \
361  };
362 
363 #define APEX_INDEX_CLEARER(IndexType) \
364  template <> \
365  struct DefaultClearer<IndexType> \
366  { \
367  static void clear(IndexType &v) \
368  { \
369  v = IndexType(); \
370  } \
371  static bool isClear(const IndexType &v) \
372  { \
373  return !v.isValid(); \
374  } \
375  static void clearConstruct(IndexType *p) \
376  { \
377  clear(*p); \
378  } \
379  static const bool clearNeedsDestruction = false; \
380  };
381 
382 #define APEX_NUMBER_FORMATTER(IdOrIndexType) \
383  inline size_t format(char *buffer, size_t buffer_size, const IdOrIndexType &v) \
384  { \
385  UT::Format::Writer w(buffer, buffer_size); \
386  UT::Format::Formatter f; \
387  return f.format(w, #IdOrIndexType "({})", {v.exintValue()}); \
388  }
389 
390 APEX_NUMBER_FORMATTER(APEX_ScopeID)
391 APEX_NUMBER_FORMATTER(apex::APEX_StickyNoteID)
392 APEX_NUMBER_FORMATTER(apex::APEX_StickyNoteIndex)
393 
394 // For UT::ArraySet.
395 namespace UT
396 {
397 template <typename T>
399 
400 APEX_ID_CLEARER(apex::APEX_StickyNoteID)
401 
402 APEX_INDEX_CLEARER(APEX_ScopeID)
403 APEX_INDEX_CLEARER(apex::APEX_StickyNoteIndex)
404 
405 }; // namespace UT
406 
407 #endif
int64 APEX_GraphDataID
Definition: APEX_Include.h:320
ColorRamp(const UT_Ramp &ramp)
Definition: APEX_Include.h:88
#define APEX_ID(IdType)
Definition: APEX_Include.h:289
#define UTdebugPrint(...)
Definition: UT_Debug.h:146
Normal callback which is unconditionally executed.
int addNode(fpreal pos)
Returns the index of the inserted node.
#define APEX_NUMBER_FORMATTER(IdOrIndexType)
Definition: APEX_Include.h:382
PRM_DataItemHandle DataItem
Definition: APEX_Include.h:109
SYS_FORCE_INLINE constexpr exint length() const
exint size() const
Definition: APEX_Include.h:135
auto makerefs(UT_Tuple< T...> &t)
Definition: APEX_Include.h:253
int64 exint
Definition: SYS_Types.h:125
UN_SubnetID APEX_SubnetID
Definition: APEX_Include.h:333
void setCapacity(exint new_capacity)
iterator begin()
Definition: APEX_Include.h:172
std::tuple< Types...> UT_Tuple
Definition: UT_Tuple.h:53
**But if you need a result
Definition: thread.h:622
const IndirectRange< const_iterator > indirectRange() const
Definition: APEX_Include.h:214
3D Vector class.
typename UT_Array< T * >::iterator iterator
Definition: APEX_Include.h:169
4D Vector class.
Definition: UT_Vector4.h:174
2D Vector class.
Definition: UT_Vector2.h:159
exint find(const S &s, exint start=0) const
UN_WireID APEX_WireID
Definition: APEX_Include.h:336
const T & operator[](exint index) const
Definition: APEX_Include.h:197
#define APEX_INDEX(IndexType)
Definition: APEX_Include.h:298
Marks the start of an arbitrary loop, expects RunData to be an iterator.
exint size() const
Definition: UT_Array.h:653
void setSize(exint newsize)
Definition: UT_Array.h:673
APEX_CallbackType
Definition: APEX_Include.h:224
OutGridT const XformOp bool bool
std::decay_t< decltype(make_index_sequence_impl< N >())> make_index_sequence
Definition: Types.h:234
void toArray(UT_Array< T > &arr) const
Definition: APEX_Include.h:162
UT_SharedPtr< const PRM_DataItem > PRM_DataItemHandle
Definition: APEX_Include.h:55
UN_PortKind
Differentiates between input and output ports.
Definition: UN_Include.h:646
GLdouble n
Definition: glcorearb.h:2008
void append(const UT_StringHolder &name, T *item)
Definition: APEX_Include.h:129
IndirectRange< iterator > indirectRange()
Definition: APEX_Include.h:209
exint emplace_back(S &&...s)
Definition: UT_ArrayImpl.h:769
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
double Float
Definition: APEX_Include.h:62
auto makeref(UT_Tuple< T...> &t, std::index_sequence< I...>)
Definition: APEX_Include.h:246
#define UTlhsTuple
Definition: UT_Tuple.h:61
typename UT_Array< T * >::const_iterator const_iterator
Definition: APEX_Include.h:170
GLuint GLuint end
Definition: glcorearb.h:475
VariadicArg(std::initializer_list< std::pair< const char *, T * >> inputs)
Definition: APEX_Include.h:116
UN_PortID APEX_PortID
Definition: APEX_Include.h:335
iterator begin()
Definition: UT_Array.h:1013
long long int64
Definition: SYS_Types.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
SYS_FORCE_INLINE constexpr const char * c_str() const
exint append()
Definition: UT_Array.h:142
GLdouble t
Definition: glad.h:2397
#define APEX_ID_CLEARER(IdType)
Definition: APEX_Include.h:344
Quaternion class.
Definition: GEO_Detail.h:49
exint Int
Definition: APEX_Include.h:61
const UT_Array< UT_StringHolder > & names() const
Definition: APEX_Include.h:145
const_iterator end() const
Definition: APEX_Include.h:175
UT_RGBAT< fpreal32 > UT_FRGBA
Definition: UT_Pixel.h:113
Utility class for containing a color ramp.
Definition: UT_Ramp.h:96
GLuint index
Definition: glcorearb.h:786
bool Bool
Definition: APEX_Include.h:60
Type-safe formatting, modeled on the Python str.format function.
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
T * operator[](exint idx)
Definition: APEX_Include.h:147
SIM_API const UT_StringHolder distance
exint nameToIndex(const UT_StringRef &name) const
Definition: APEX_Include.h:157
UN_NodeID APEX_NodeID
Definition: APEX_Include.h:332
const_iterator begin() const
Definition: APEX_Include.h:174
const T * operator[](exint idx) const
Definition: APEX_Include.h:152
iterator end()
End iterator.
Definition: UT_Array.h:1018
FloatRamp(const UT_Ramp &ramp)
Definition: APEX_Include.h:78
Do nothing; ignored after compilation.
APEX_SubnetID APEX_GraphID
Definition: APEX_Include.h:334
#define APEX_INDEX_CLEARER(IndexType)
Definition: APEX_Include.h:363