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 #include <SYS/SYS_StaticAssert.h>
18 #include <stdarg.h>
19 
20 template <typename T, int DEFAULT_TOKEN = -1>
22 {
23 public:
24  /// Empty table constructor.
26  {
27  }
28 
29  /// Create a new token map. Variadic arguments are provided as pairs -
30  /// for example:
31  /// UT_FSATableT map(1, "one", 2, "two", -1, NULL);
32  /// The argument list must be terminated by (-1, NULL).
33  UT_FSATableT(T token, const char *symbol, ...)
34  {
35  va_list args;
36  va_start(args, symbol);
37 
38  while (true)
39  {
40  if (!symbol)
41  break;
42 
43  symbol = myFSA.addSymbol(symbol, token);
44 
45  int idx = (int)token;
46  if (idx >= 0)
47  {
48  if (idx < myTokens.entries())
49  {
50  if (!myTokens(idx))
51  myTokens(idx) = symbol;
52  }
53  else
54  myTokens.insert(symbol, idx);
55  }
56 
57  SYS_STATIC_ASSERT(sizeof(T) == sizeof(int));
58  token = (T)va_arg(args, int);
59  symbol = va_arg(args, const char *);
60  }
61 
62  va_end(args);
63 
64  myFSA.rebuild();
65  }
66 
67  /// @{
68  /// Find the integer id corresponding to the symbol.
69  /// Returns DEFAULT_TOKEN if not found
70  inline T findSymbol(const char *symbol) const
71  {
72  T token;
73  if (symbol && myFSA.findSymbol(symbol, &token))
74  return token;
75  return (T)DEFAULT_TOKEN;
76  }
77  inline T findSymbol(const UT_StringView &symbol) const
78  {
79  T token;
80  if (symbol && myFSA.findSymbol(symbol, &token))
81  return token;
82  return (T)DEFAULT_TOKEN;
83  }
84  inline T findSymbol(const UT_String &symbol) const
85  { return findSymbol(symbol.c_str()); }
86  inline T findSymbol(const UT_StringRef &symbol) const
87  { return findSymbol(UT_StringView(symbol)); }
88  /// @}
89 
90  /// Return the name associated with the integer id. If there are duplicate
91  /// strings which map to the same id, the first string will be returned.
92  /// Returns NULL if not valid
93  const char *getToken(T token) const
94  {
95  int idx = (int)token;
96  if (idx >= 0 && idx < myTokens.entries() && myTokens(idx))
97  return myTokens(idx);
98  return NULL;
99  }
100 
101  /// Returns the memory usage of this object
103  { return myFSA.getMemoryUsage(true) +
104  myTokens.getMemoryUsage(true); }
105 
106  /// Extract all strings
107  void extractStrings(UT_StringArray &list) const
108  { myFSA.extractStrings(list); }
109 
110  /// @{
111  /// Return whether a symbol exists
112  bool contains(const char *symbol) const
113  { return myFSA.contains(symbol); }
114  int count(const char *symbol) const
115  { return myFSA.count(symbol); }
116  /// @}
117 
118 
119 private:
120  UT_FSASymbolTable<T> myFSA;
121  UT_Array<const char *> myTokens;
122 };
123 
125 
126 #endif
127 
bool contains(const char *symbol) const
Definition: UT_FSATable.h:112
#define SYS_STATIC_ASSERT(expr)
int findSymbol(const char *symbol, ITEM_T *datap) const
UT_FSATableT()
Empty table constructor.
Definition: UT_FSATable.h:25
const Args & args
Definition: printf.h:628
void extractStrings(UT_StringArray &list) const
Extract all strings.
Definition: UT_FSATable.h:107
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:469
int count(const char *symbol) const
Definition: UT_FSATable.h:114
T findSymbol(const UT_String &symbol) const
Definition: UT_FSATable.h:84
const char * c_str() const
Definition: UT_String.h:505
const char * getToken(T token) const
Definition: UT_FSATable.h:93
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:40
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:86
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
T findSymbol(const UT_StringView &symbol) const
Definition: UT_FSATable.h:77
int64 getMemoryUsage(bool inclusive) const
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:460
bool contains(const char *symbol) const
UT_FSATableT< int,-1 > UT_FSATable
Definition: UT_FSATable.h:124
int64 getMemUsage() const
Returns the memory usage of this object.
Definition: UT_FSATable.h:102
UT_FSATableT(T token, const char *symbol,...)
Definition: UT_FSATable.h:33
T findSymbol(const char *symbol) const
Definition: UT_FSATable.h:70
exint insert(exint index)
Definition: UT_ArrayImpl.h:123
const char * addSymbol(const char *symbol, const ITEM_T &data)