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;
42  PDG_FilterPattern& operator=(const PDG_FilterPattern&) = delete;
43 
44  void reset(const UT_StringHolder& pattern);
45 
46  bool match(
47  const PDG_WorkItem* work_item,
48  bool inclusive) const;
49  bool match(
50  const UT_StringHolder& name,
51  const UT_StringHolder& value,
52  int component,
53  bool inclusive) const
54  {
55  return matchPattern(
56  name, value, component, inclusive);
57  }
58 
59  bool match(
60  const UT_StringHolder& name,
61  exint value,
62  int component,
63  bool inclusive) const
64  {
65  return matchPattern(
66  name, value, component, inclusive);
67  }
68 
69  bool match(
70  const UT_StringHolder& name,
71  fpreal value,
72  int component,
73  bool inclusive) const
74  {
75  return matchPattern(
76  name, value, component, inclusive);
77  }
78 
79 private:
80  enum Operation
81  {
82  // @id=pattern or @id==pattern
83  eOpEQ,
84 
85  // @id!=pattern
86  eOpNE,
87 
88  // @id>pattern
89  eOpGT,
90 
91  // @id>=pattern
92  eOpGE,
93 
94  // @id<pattern
95  eOpLT,
96 
97  // @id<=pattern
98  eOpLE,
99  };
100 
101  struct Entry
102  {
103  Entry()
104  : myPattern(',')
105  , myComponent(0) {}
106 
107  UT_StringHolder myAttribute;
108  int myComponent;
109 
110  PDG_ValuePattern myPattern;
111  fpreal myFloat;
112  exint myInteger;
113 
114  Operation myOperator;
115 
116  bool myHasFloat;
117  bool myHasInteger;
118 
119  bool myIsExclusion;
120  };
121 
122  using EntryPtr = UT_UniquePtr<Entry>;
123 
124 private:
125  bool parse(const UT_StringHolder& pattern);
126 
127  bool matchIntrinsic(
128  bool& result,
129  const PDG_WorkItem* work_item,
130  const EntryPtr& entry,
132  bool inclusive) const;
133 
134  template <typename T>
135  inline bool matchPattern(
136  const UT_StringHolder& name,
137  const T& value,
138  int component,
139  bool inclusive) const
140  {
141  bool matched = false;
142  for (auto&& entry : myPatternEntries)
143  {
144  if (entry->myAttribute != name || entry->myComponent != component)
145  continue;
146 
147  if (matchEntry(*entry.get(), value, inclusive))
148  {
149  if (!myHasExclusions)
150  return true;
151  matched = !entry->myIsExclusion;
152  }
153  }
154 
155  return matched;
156  }
157 
158  inline bool matchEntry(
159  const Entry& entry,
160  fpreal value,
161  bool inclusive) const
162  {
163  return compareNumeric(entry, value, inclusive);
164  }
165 
166  inline bool matchEntry(
167  const Entry& entry,
168  exint value,
169  bool inclusive) const
170  {
171  return compareNumeric(entry, value, inclusive);
172  }
173 
174  inline bool matchEntry(
175  const Entry& entry,
176  const UT_StringHolder& value,
177  bool inclusive) const
178  {
179  if (entry.myOperator == eOpEQ)
180  {
181  if (entry.myPattern.isValid())
182  return entry.myPattern.contains(value, inclusive);
183  return false;
184  }
185 
186  if (entry.myOperator == eOpNE)
187  {
188  if (entry.myPattern.isValid())
189  return !entry.myPattern.contains(value, inclusive);
190  return false;
191  }
192 
193  return false;
194  }
195 
196  template <typename T>
197  inline bool compareNumeric(
198  const Entry& entry,
199  const T& value,
200  bool inclusive) const
201  {
202  switch (entry.myOperator)
203  {
204  case eOpEQ:
205  {
206  if (entry.myHasFloat)
207  return SYSalmostEqual(entry.myFloat, value);
208  if (entry.myHasInteger)
209  return entry.myInteger == value;
210  if (entry.myPattern.isValid())
211  return entry.myPattern.contains(value, inclusive);
212  return false;
213  }
214 
215  case eOpNE:
216  {
217  if (entry.myHasFloat)
218  return !SYSalmostEqual(entry.myFloat, value);
219  if (entry.myHasInteger)
220  return entry.myInteger != value;
221  if (entry.myPattern.isValid())
222  return !entry.myPattern.contains(value, inclusive);
223  return false;
224  }
225 
226  case eOpGT:
227  {
228  if (entry.myHasFloat)
229  return entry.myFloat < value;
230  if (entry.myHasInteger)
231  return entry.myInteger < value;
232  return false;
233  }
234 
235  case eOpGE:
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 eOpLT:
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 eOpLE:
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  default:
263  return false;
264  }
265  }
266 
267 private:
268  UT_Array<EntryPtr> myPatternEntries;
269  bool myHasExclusions;
270 };
271 
272 #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:613
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
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
fpreal64 fpreal
Definition: SYS_Types.h:277
bool match(const UT_StringHolder &name, const UT_StringHolder &value, int component, bool inclusive) const
Definition: core.h:1131
type
Definition: core.h:1059