HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_FilterPattern.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  * COMMENTS:
7  */
8 
9 #ifndef __PDG_FILTER_PATTERN_H__
10 #define __PDG_FILTER_PATTERN_H__
11 
12 #include "PDG_API.h"
13 #include "PDG_AttributeTypes.h"
14 #include "PDG_BasePattern.h"
15 #include "PDG_ValuePattern.h"
16 
17 #include <UT/UT_ArrayStringSet.h>
18 #include <UT/UT_NonCopyable.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_UniquePtr.h>
21 
22 #include <SYS/SYS_Math.h>
23 
24 class PDG_ValuePattern;
25 class PDG_WorkItem;
26 
27 /**
28  * Matches attribute or intrinsic names to filter ranges, for example:
29  *
30  * `@pdg_id=1,10,100-200`
31  * `@scale.x>0.5 @scale.y<0.5`
32  *
33  * Multiple pattern entries can be specified, separated by a space.
34  */
36 {
37 public:
40 
41  PDG_FilterPattern(const PDG_FilterPattern&) = delete;
43 
44  /// Resets the pattern and reparses it from the specified string
45  bool reset(const UT_StringHolder& pattern);
46 
47  /// Updates the pattern if it doesn't match the current one stored in
48  /// this instance
49  bool tryReset(const UT_StringHolder& pattern);
50 
51 
52  bool match(
53  const PDG_WorkItem* work_item,
54  bool inclusive) const;
55  bool match(
56  const UT_StringHolder& name,
57  const UT_StringHolder& value,
58  int component,
59  bool inclusive) const
60  {
61  return matchPattern(
62  name, value, component, inclusive);
63  }
64 
65  bool match(
66  const UT_StringHolder& name,
67  exint value,
68  int component,
69  bool inclusive) const
70  {
71  return matchPattern(
72  name, value, component, inclusive);
73  }
74 
75  bool match(
76  const UT_StringHolder& name,
77  fpreal value,
78  int component,
79  bool inclusive) const
80  {
81  return matchPattern(
82  name, value, component, inclusive);
83  }
84 
85 private:
86  enum Operation
87  {
88  // @id=pattern or @id==pattern
89  eOpEQ,
90 
91  // @id!=pattern
92  eOpNE,
93 
94  // @id>pattern
95  eOpGT,
96 
97  // @id>=pattern
98  eOpGE,
99 
100  // @id<pattern
101  eOpLT,
102 
103  // @id<=pattern
104  eOpLE,
105  };
106 
107  struct Entry
108  {
109  Entry()
110  : myPattern(',')
111  , myComponent(0) {}
112 
113  UT_StringHolder myAttribute;
114  int myComponent;
115 
116  PDG_ValuePattern myPattern;
117  fpreal myFloat;
118  exint myInteger;
119 
120  Operation myOperator;
121 
122  bool myHasFloat;
123  bool myHasInteger;
124 
125  bool myIsExclusion;
126  };
127 
128  using EntryPtr = UT_UniquePtr<Entry>;
129 
130 private:
131  bool parse(const UT_StringHolder& pattern);
132 
133  bool matchIntrinsic(
134  bool& result,
135  const PDG_WorkItem* work_item,
136  const EntryPtr& entry,
138  bool inclusive) const;
139 
140  template <typename T>
141  inline bool matchPattern(
142  const UT_StringHolder& name,
143  const T& value,
144  int component,
145  bool inclusive) const
146  {
147  if (!myIsValid)
148  return false;
149 
150  bool matched = false;
151  for (auto&& entry : myPatternEntries)
152  {
153  if (entry->myAttribute != name || entry->myComponent != component)
154  continue;
155 
156  if (matchEntry(*entry.get(), value, inclusive))
157  {
158  if (!myHasExclusions)
159  return true;
160  matched = !entry->myIsExclusion;
161  }
162  }
163 
164  return matched;
165  }
166 
167  inline bool matchEntry(
168  const Entry& entry,
169  fpreal value,
170  bool inclusive) const
171  {
172  return compareNumeric(entry, value, inclusive);
173  }
174 
175  inline bool matchEntry(
176  const Entry& entry,
177  exint value,
178  bool inclusive) const
179  {
180  return compareNumeric(entry, value, inclusive);
181  }
182 
183  inline bool matchEntry(
184  const Entry& entry,
185  const UT_StringHolder& value,
186  bool inclusive) const
187  {
188  if (entry.myOperator == eOpEQ)
189  {
190  if (entry.myPattern.isValid())
191  return entry.myPattern.contains(value, inclusive);
192  return false;
193  }
194 
195  if (entry.myOperator == eOpNE)
196  {
197  if (entry.myPattern.isValid())
198  return !entry.myPattern.contains(value, inclusive);
199  return false;
200  }
201 
202  return false;
203  }
204 
205  template <typename T>
206  inline bool compareNumeric(
207  const Entry& entry,
208  const T& value,
209  bool inclusive) const
210  {
211  switch (entry.myOperator)
212  {
213  case eOpEQ:
214  {
215  if (entry.myHasFloat)
216  return SYSalmostEqual(entry.myFloat, value);
217  if (entry.myHasInteger)
218  return entry.myInteger == value;
219  if (entry.myPattern.isValid())
220  return entry.myPattern.contains(value, inclusive);
221  return false;
222  }
223 
224  case eOpNE:
225  {
226  if (entry.myHasFloat)
227  return !SYSalmostEqual(entry.myFloat, value);
228  if (entry.myHasInteger)
229  return entry.myInteger != value;
230  if (entry.myPattern.isValid())
231  return !entry.myPattern.contains(value, inclusive);
232  return false;
233  }
234 
235  case eOpGT:
236  {
237  if (entry.myHasFloat)
238  return entry.myFloat < value;
239  if (entry.myHasInteger)
240  return entry.myInteger < value;
241  return false;
242  }
243 
244  case eOpGE:
245  {
246  if (entry.myHasFloat)
247  return entry.myFloat <= value;
248  if (entry.myHasInteger)
249  return entry.myInteger <= value;
250  return false;
251  }
252 
253  case eOpLT:
254  {
255  if (entry.myHasFloat)
256  return entry.myFloat > value;
257  if (entry.myHasInteger)
258  return entry.myInteger > value;
259  return false;
260  }
261 
262  case eOpLE:
263  {
264  if (entry.myHasFloat)
265  return entry.myFloat >= value;
266  if (entry.myHasInteger)
267  return entry.myInteger >= value;
268  return false;
269  }
270 
271  default:
272  return false;
273  }
274  }
275 
276 private:
277  UT_Array<EntryPtr> myPatternEntries;
278  bool myHasExclusions;
279 };
280 
281 #endif
bool match(const UT_StringHolder &name, exint value, int component, bool inclusive) const
PDG_AttributeIntrinsic
Enumeration of types of attribute that can be queried from work items.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define PDG_API
Definition: PDG_API.h:23
void reset(const UT_StringHolder &pattern)
Resets the pattern.
int64 exint
Definition: SYS_Types.h:125
**But if you need a result
Definition: thread.h:622
bool match(const UT_StringHolder &name, fpreal value, int component, bool inclusive) const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
fpreal64 fpreal
Definition: SYS_Types.h:283
LeafData & operator=(const LeafData &)=delete
bool match(const UT_StringHolder &name, const UT_StringHolder &value, int component, bool inclusive) const