HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FSATable.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_FSATable.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_FSATable__
13 #define __UT_FSATable__
14 
15 #include "UT_API.h"
16 #include "UT_FSASymbolTable.h"
17 
18 template <typename T, int DEFAULT_TOKEN = -1>
20 {
21 public:
22  /// Empty table constructor.
24  {
25  }
26 
27  /// Create a new token map. Variadic arguments are provided as pairs -
28  /// for example:
29  /// UT_FSATableT map(1, "one", 2, "two");
30  template <typename... Args>
31  UT_FSATableT(T token, const char *symbol, Args... args)
32  {
33  insertTokens(token, symbol, args...);
34  myFSA.rebuild();
35  }
36 
37  /// @{
38  /// Find the integer id corresponding to the symbol.
39  /// Returns DEFAULT_TOKEN if not found
40  inline T findSymbol(const char *symbol) const
41  {
42  T token;
43  if (symbol && myFSA.findSymbol(symbol, &token))
44  return token;
45  return (T)DEFAULT_TOKEN;
46  }
47  inline T findSymbol(const UT_StringView &symbol) const
48  {
49  T token;
50  if (symbol && myFSA.findSymbol(symbol, &token))
51  return token;
52  return (T)DEFAULT_TOKEN;
53  }
54  inline T findSymbol(const UT_String &symbol) const
55  { return findSymbol(symbol.c_str()); }
56  inline T findSymbol(const UT_StringRef &symbol) const
57  { return findSymbol(UT_StringView(symbol)); }
58  /// @}
59 
60  /// Return the name associated with the integer id. If there are duplicate
61  /// strings which map to the same id, the first string will be returned.
62  /// Returns NULL if not valid
63  const char *getToken(T token) const
64  {
65  int idx = (int)token;
66  if (idx >= 0 && idx < myTokens.entries() && myTokens(idx))
67  return myTokens(idx);
68  return NULL;
69  }
70 
71  /// Returns the memory usage of this object
73  { return myFSA.getMemoryUsage(true) +
74  myTokens.getMemoryUsage(true); }
75 
76  /// Extract all strings
77  void extractStrings(UT_StringArray &list) const
78  { myFSA.extractStrings(list); }
79 
80  /// @{
81  /// Return whether a symbol exists
82  bool contains(const char *symbol) const
83  { return myFSA.contains(symbol); }
84  int count(const char *symbol) const
85  { return myFSA.count(symbol); }
86  /// @}
87 
88 
89 private:
90  void
91  insertTokens()
92  {
93  // Base case for the variadic template.
94  }
95 
96  template <typename... Args>
97  void
98  insertTokens(T token, const char *symbol, Args... args)
99  {
100  insertToken(token, symbol);
101  insertTokens(args...);
102  }
103 
104  void
105  insertToken(T token, const char *symbol)
106  {
107  // From the previous varargs implementation, nullptr is ignored as it
108  // terminates the list of arguments.
109  if (!symbol)
110  return;
111 
112  symbol = myFSA.addSymbol(symbol, token);
113 
114  int idx = static_cast<int>(token);
115  if (idx >= 0)
116  {
117  if (idx < myTokens.entries())
118  {
119  if (!myTokens(idx))
120  myTokens(idx) = symbol;
121  }
122  else
123  myTokens.insert(symbol, idx);
124  }
125  }
126 
127  UT_FSASymbolTable<T> myFSA;
128  UT_Array<const char *> myTokens;
129 };
130 
132 
133 #endif
134 
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
bool contains(const char *symbol) const
Definition: UT_FSATable.h:82
int findSymbol(const char *symbol, ITEM_T *datap) const
UT_FSATableT()
Empty table constructor.
Definition: UT_FSATable.h:23
void extractStrings(UT_StringArray &list) const
Extract all strings.
Definition: UT_FSATable.h:77
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:657
int count(const char *symbol) const
Definition: UT_FSATable.h:84
T findSymbol(const UT_String &symbol) const
Definition: UT_FSATable.h:54
const char * c_str() const
Definition: UT_String.h:508
const char * getToken(T token) const
Definition: UT_FSATable.h:63
void extractStrings(UT_StringArray &list) const
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:39
bool count(const char *symbol) const
long long int64
Definition: SYS_Types.h:116
T findSymbol(const UT_StringRef &symbol) const
Definition: UT_FSATable.h:56
T findSymbol(const UT_StringView &symbol) const
Definition: UT_FSATable.h:47
int64 getMemoryUsage(bool inclusive) const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:648
bool contains(const char *symbol) const
UT_FSATableT(T token, const char *symbol, Args...args)
Definition: UT_FSATable.h:31
UT_FSATableT< int,-1 > UT_FSATable
Definition: UT_FSATable.h:131
**If you just want to fire and args
Definition: thread.h:609
int64 getMemUsage() const
Returns the memory usage of this object.
Definition: UT_FSATable.h:72
T findSymbol(const char *symbol) const
Definition: UT_FSATable.h:40
exint insert(exint index)
Definition: UT_ArrayImpl.h:721
const char * addSymbol(const char *symbol, const ITEM_T &data)