HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TagManager.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: UT_TagManager.h ( UT Library, C++)
7  *
8  * COMMENTS: Manage lists of tags. Tags are simply name strings. A list of
9  * tags is a list of names. The names may contain alphanumeric characters
10  * along with the characters:
11  * '_' (underscore)
12  * '.' (period)
13  * '-' (minus) (we also support ^)
14  * ':' (colon)
15  * Names may NOT begin with '-' (minus). Invalid characters will be
16  * forced to an '_' (underscore) character.
17  *
18  * Pattern expression can also be created which can be matched against tag
19  * lists (see below for more documentation).
20  *
21  * A global list of used tags are kept and may be retrieved. The list is
22  * not cleared (TODO: it would be nice to have a garbage collection
23  * process)
24  */
25 
26 #ifndef __UT_TagManager__
27 #define __UT_TagManager__
28 
29 #include "UT_API.h"
30 #include "UT_BitArray.h"
31 #include "UT_ConcurrentHashMap.h"
32 #include "UT_ConcurrentVector.h"
33 #include "UT_HashFunctor.h"
34 #include "UT_IntArray.h"
35 #include "UT_IntrusivePtr.h"
36 #include "UT_Memory.h"
37 #include "UT_NonCopyable.h"
38 #include "UT_SmallObject.h"
39 #include "UT_SymbolTable.h"
40 
41 class UT_String;
42 class UT_StringArray;
43 class UT_TagManager;
44 class UT_TagList;
45 class UT_TagExpression;
46 
47 // #define UT_TAG_USE_SMALL_OBJECTS
48 
49 /// A comma separated list of tags (i.e. "fill,spot,red")
51  : public UT_IntrusiveRefCounter<UT_TagList>
52 #if defined(UT_TAG_USE_SMALL_OBJECTS)
53  , public UT_SmallObject<UT_TagList,
54  UT_SMALLOBJECT_CLEANPAGES_ON,
55  512,
56  UT_SMALLOBJECT_THREADSAFE_ON>
57 #endif
58 {
59 public:
60  UT_TagList(UT_TagManager &manager, const UT_IntArray &bits, int max);
61  UT_TagList(UT_TagManager &manager, int single_name_index);
62  ~UT_TagList();
63 
65 
66  void getListString(UT_WorkBuffer &buffer) const;
67  bool match(const UT_TagExpression &pattern) const;
68  uint hash() const;
69  bool compare(const UT_TagList &list) const;
70 
71  bool operator==(const UT_TagList &list) const
72  { return compare(list); }
73  bool operator!=(const UT_TagList &list) const
74  { return !compare(list); }
75 
76  /// @{
77  /// Get the tag manager
78  const UT_TagManager &getManager() const { return myManager; }
79  UT_TagManager &getManager() { return myManager; }
80  /// @}
81 
83  { return sizeof(*this) + myBits.getMemoryUsage(); }
84 
85 private:
86  bool isEmpty() const
87  { return mySingle < 0 && myBits.size() == 0; }
88  bool hasTag(int id) const
89  {
90  return id == mySingle ||
91  (id < myBits.size() && myBits.getBit(id));
92  }
93  void validateList(int namecount);
94  void setSingleBit(int bit);
95 
96 private:
97  UT_TagManager &myManager;
98  UT_BitArray myBits;
99  int mySingle;
100  friend class UT_TagManager;
101  friend class UT_TagExpression;
102 
103 };
104 
105 /// An expression to match tags (i.e. "* & ^fill")
107  : public UT_IntrusiveRefCounter<UT_TagExpression>
108 #if defined(UT_TAG_USE_SMALL_OBJECTS)
109  , public UT_SmallObject<UT_TagExpression,
110  UT_SMALLOBJECT_CLEANPAGES_ON,
111  512,
112  UT_SMALLOBJECT_THREADSAFE_ON>
113 #endif
114 {
115 public:
117  ~UT_TagExpression();
118 
120 
121  /// @{
122  /// Allocate a new expression with the appropriate edits
123 
124  /// (expr) -> A|(expr). However, we can also simplify some components of
125  /// expression. For example, (A|-A) -> * (Complementary Law)
126  UT_TagExpression *addTags(const UT_TagList &list) const;
127 
128  /// (expr) -> -A & (expr). Since | has a greater precedence, we need to
129  /// expand using distributive property. Some components of the expression
130  /// can be simplified (i.e. -A & A -> -*).
131  UT_TagExpression *rmTags(const UT_TagList &list) const;
132 
133  /// @}
134 
135  /// @{
136  /// Get the tag manager
137  const UT_TagManager &getManager() const { return myManager; }
138  UT_TagManager &getManager() { return myManager; }
139  /// @}
140 
141  bool compare(const UT_TagExpression &expr) const;
142  bool operator==(const UT_TagExpression &expr) const
143  { return compare(expr); }
144  bool operator!=(const UT_TagExpression &expr) const
145  { return !compare(expr); }
146  uint hash() const;
147 
148  void getExpressionString(UT_WorkBuffer &buffer) const;
149  bool match(const UT_TagList &list) const;
150  bool isTautology() const;
151 
152  /// Determine whether any known names match. The matching names will
153  /// be returned in 'matching' and names that don't match are put in
154  /// 'failing'. 'outmatch' will be set to true when unknown names will
155  /// produce a match.
156  void matchAllNames(UT_Array<const char *> &matching,
157  UT_Array<const char *> &failing,
158  bool &outmatch);
159 
161 
163  { return sizeof(*this) + myCode.getMemoryUsage(); }
164 private:
165  // Break up a complex expression into multiple sub-expressions which can be
166  // or'ed together.
167  void extractOrComponents(SubExpressionList &elist) const;
168 
169  // Combine multiple sub-expressions into a single expression by or'ing them
170  // together.
171  void combineOrComponents(const SubExpressionList &elist);
172 
173  static bool simplifyExpressions(SubExpressionList &elist,
174  int bit,
175  UT_TagExpression *expr,
176  UT_BitArray &need_final_or);
177  static void addExpressionOr(UT_TagExpression *expr,
178  int bit,
179  const UT_BitArray &need_final_or);
180 
181  void clear();
182  void validateExpr(int namecount);
183  void addOpCode(int opcode, int operand = 0);
184  bool addOpCode(UT_IntArray &stack)
185  {
186  int n;
187  n = stack.entries();
188  if (n < 2)
189  return false;
190  addOpCode(stack(n-2), stack(n-1));
191  stack.entries(n-2);
192  return true;
193  }
194 
195 
196 private:
197  UT_TagManager &myManager;
198  UT_IntArray myCode;
199  friend class UT_TagManager;
200 };
201 
204 
205 /// A tag manager keeps track of lists of tags and expressions. It stores them
206 /// very compactly for efficient processing.
208 {
209 public:
210  class WeakKey
211  {
212  public:
213  // Copy in as a weak pointer when passed a const char *
214  WeakKey(const char *key)
215  : myKey(UTmakeUnsafeRef(key))
216  {
217  }
218  // Given a UT_StringHolder, we can just store the holder
220  : myKey(key)
221  {
222  }
223  // The copy c-tor is used when inserting into the map, so we need to
224  // ensure the key is hardened.
225  WeakKey(const WeakKey &k)
226  : myKey(k.myKey.c_str()) // UT_StringHolder will duplicate
227  {
228  }
229  WeakKey &operator=(const WeakKey &key)
230  {
231  // We need to harden the string in the source key
232  myKey = UT_StringHolder(key.c_str());
233  return *this;
234  }
235  const char *c_str() const { return myKey.c_str(); }
236  uint hash() const { return myKey.hash(); }
237  bool operator==(const WeakKey &k) const
238  {
239  return myKey == k.myKey;
240  }
241  private:
242  UT_StringHolder myKey;
243  };
244  using tag_ListType = UT_ConcurrentVector<const char *>;
247 
248  /// getTagList() creates a tag-list object. This is a list of names which
249  /// are considered as tags for matching (see below).
250  /// The names allow alphanumeric letters and characters from "_.:"
251  UT_TagListPtr createList(const char *list, UT_String &errs)
252  {
253  return UT_TagListPtr(createTagList(list, errs));
254  }
256  {
257  return UT_TagListPtr(createTagListForName(name_index));
258  }
259 
260  /// createTagExpression() is used to create a pattern to match against tag
261  /// lists.
262  ///
263  /// The tag expression can be a very simple expression consisting of
264  /// - @b name @n Matches tag lists which have the name
265  /// - @b -name @n Matches tag lists which do NOT contain the name
266  /// - @b + @n Matches tag lists which have ANY entries
267  /// - @b - @n Matches tag lists which have NO entries
268  /// - @b * @n Matches all tag lists (Equivalent to "+|-")
269  /// - @b -* @n Matches no tag lists (Equivalend to "+&-")
270  ///
271  /// Expressions may be joined with & or | (AND or OR). For example
272  /// - <tt>-foo</tt> @n Match all tags except those with "foo"
273  /// - <tt>* & -foo</tt> @n Equivalent to "-foo"
274  /// - <tt>-foo & -bar</tt> @n
275  /// Match all tags except those with "foo" or "bar"
276  /// - <tt>foo & -bar</tt> @n
277  /// Match all tags that have "foo" but don't have "bar"
278  ///
279  /// Expressions are processed left to right with AND at a higher
280  /// precedence in the order of operations than OR. Thus:
281  /// @code
282  /// a & b & c | d & e | f | g & h
283  /// @endcode
284  /// is equivalent to:
285  /// @code
286  /// (a & b & c) | (d & e) | f | (g & h)
287  /// @endcode
288  ///
289  /// @note You are responsible for deleting this
290  /// Smart pointer access to tag expressions
292  UT_String &errs)
293  {
294  return UT_TagExpressionPtr(
295  createTagExpression(expr, errs));
296  }
297 
299  {
300  TAG_OP_ADD, // Add name to expression
301  TAG_OP_RM, // Remove name from expression
302  TAG_OP_SET, // Set the expression to the name
303  };
304 
305  /// Apply an edit to an expression. This operation may have bugs, but will
306  /// always work on a simplified expression.
307  UT_TagExpressionPtr editExpression(const UT_TagExpressionPtr &expr,
308  const char *name,
309  UT_TagEditOperation op,
310  UT_String &errors);
311 
312  /// Get a list of all the tags used by any list or pattern we've
313  /// constructed
314  void getAllNames(UT_StringArray &tags);
315 
316  /// @private accessors
317  const tag_ListType &nameList() const { return myNameList; }
318 
319 private:
320  UT_TagList *createTagListForName(int name_index);
321  UT_TagList *createTagList(const char *list,
322  UT_String &errs);
323  UT_TagExpression *createTagExpression(const char *expression,
324  UT_String &errs);
325 
326  // Returns the index of the the tag
327  int findOrCreateTag(const char *tag);
328 
329  tag_MapType myNameTable;
330  tag_ListType myNameList;
331 
332  friend class UT_TagList;
333  friend class UT_TagExpression;
334 };
335 
336 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
An expression to match tags (i.e. "* & ^fill")
bool operator!=(const UT_TagList &list) const
Definition: UT_TagManager.h:73
WeakKey(const UT_StringHolder &key)
UT_IntrusivePtr< UT_TagExpression > UT_TagExpressionPtr
const char * c_str() const
#define UT_API
Definition: UT_API.h:14
bool operator!=(const UT_TagExpression &expr) const
const UT_TagManager & getManager() const
UT_ConcurrentVector< const char * > tag_ListType
bool operator==(const UT_TagExpression &expr) const
A reference counter base class for use with UT_IntrusivePtr.
UT_TagListPtr createList(const char *list, UT_String &errs)
bool operator==(const UT_TagList &list) const
Definition: UT_TagManager.h:71
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
GLdouble n
Definition: glcorearb.h:2008
Definition: core.h:760
UT_TagManager & getManager()
WeakKey(const char *key)
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
A comma separated list of tags (i.e. "fill,spot,red")
Definition: UT_TagManager.h:50
Wrapper around hboost::intrusive_ptr.
long long int64
Definition: SYS_Types.h:116
UT_ConcurrentHashMap< WeakKey, int, UT_HashFunctor< WeakKey > > tag_MapType
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
WeakKey(const WeakKey &k)
UT_IntrusivePtr< UT_TagList > UT_TagListPtr
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:648
#define UT_ConcurrentHashMap
UT_TagListPtr createListForName(int name_index)
WeakKey & operator=(const WeakKey &key)
const UT_TagManager & getManager() const
Definition: UT_TagManager.h:78
int64 getMemUsage() const
int64 getMemUsage() const
Definition: UT_TagManager.h:82
UT_TagManager & getManager()
Definition: UT_TagManager.h:79
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool operator==(const WeakKey &k) const
unsigned int uint
Definition: SYS_Types.h:45
UT_TagExpressionPtr createExpression(const char *expr, UT_String &errs)