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  bool matched = false;
148  for (auto&& entry : myPatternEntries)
149  {
150  if (entry->myAttribute != name || entry->myComponent != component)
151  continue;
152 
153  if (matchEntry(*entry.get(), value, inclusive))
154  {
155  if (!myHasExclusions)
156  return true;
157  matched = !entry->myIsExclusion;
158  }
159  }
160 
161  return matched;
162  }
163 
164  inline bool matchEntry(
165  const Entry& entry,
166  fpreal value,
167  bool inclusive) const
168  {
169  return compareNumeric(entry, value, inclusive);
170  }
171 
172  inline bool matchEntry(
173  const Entry& entry,
174  exint value,
175  bool inclusive) const
176  {
177  return compareNumeric(entry, value, inclusive);
178  }
179 
180  inline bool matchEntry(
181  const Entry& entry,
182  const UT_StringHolder& value,
183  bool inclusive) const
184  {
185  if (entry.myOperator == eOpEQ)
186  {
187  if (entry.myPattern.isValid())
188  return entry.myPattern.contains(value, inclusive);
189  return false;
190  }
191 
192  if (entry.myOperator == eOpNE)
193  {
194  if (entry.myPattern.isValid())
195  return !entry.myPattern.contains(value, inclusive);
196  return false;
197  }
198 
199  return false;
200  }
201 
202  template <typename T>
203  inline bool compareNumeric(
204  const Entry& entry,
205  const T& value,
206  bool inclusive) const
207  {
208  switch (entry.myOperator)
209  {
210  case eOpEQ:
211  {
212  if (entry.myHasFloat)
213  return SYSalmostEqual(entry.myFloat, value);
214  if (entry.myHasInteger)
215  return entry.myInteger == value;
216  if (entry.myPattern.isValid())
217  return entry.myPattern.contains(value, inclusive);
218  return false;
219  }
220 
221  case eOpNE:
222  {
223  if (entry.myHasFloat)
224  return !SYSalmostEqual(entry.myFloat, value);
225  if (entry.myHasInteger)
226  return entry.myInteger != value;
227  if (entry.myPattern.isValid())
228  return !entry.myPattern.contains(value, inclusive);
229  return false;
230  }
231 
232  case eOpGT:
233  {
234  if (entry.myHasFloat)
235  return entry.myFloat < value;
236  if (entry.myHasInteger)
237  return entry.myInteger < value;
238  return false;
239  }
240 
241  case eOpGE:
242  {
243  if (entry.myHasFloat)
244  return entry.myFloat <= value;
245  if (entry.myHasInteger)
246  return entry.myInteger <= value;
247  return false;
248  }
249 
250  case eOpLT:
251  {
252  if (entry.myHasFloat)
253  return entry.myFloat > value;
254  if (entry.myHasInteger)
255  return entry.myInteger > value;
256  return false;
257  }
258 
259  case eOpLE:
260  {
261  if (entry.myHasFloat)
262  return entry.myFloat >= value;
263  if (entry.myHasInteger)
264  return entry.myInteger >= value;
265  return false;
266  }
267 
268  default:
269  return false;
270  }
271  }
272 
273 private:
274  UT_Array<EntryPtr> myPatternEntries;
275  bool myHasExclusions;
276 };
277 
278 #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
LeafData & operator=(const LeafData &)=delete
bool match(const UT_StringHolder &name, const UT_StringHolder &value, int component, bool inclusive) const
Definition: core.h:1131
type
Definition: core.h:1059