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 (1)
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  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  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  /// @}
85 
86  /// Return the name associated with the integer id. If there are duplicate
87  /// strings which map to the same id, the first string will be returned.
88  /// Returns NULL if not valid
89  const char *getToken(T token) const
90  {
91  int idx = (int)token;
92  if (idx >= 0 && idx < myTokens.entries() && myTokens(idx))
93  return myTokens(idx);
94  return NULL;
95  }
96 
97  /// Returns the memory usage of this object
99  { return myFSA.getMemoryUsage(true) +
100  myTokens.getMemoryUsage(true); }
101 
102  /// Extract all strings
103  void extractStrings(UT_StringArray &list) const
104  { myFSA.extractStrings(list); }
105 
106  /// @{
107  /// Return whether a symbol exists
108  bool contains(const char *symbol) const
109  { return myFSA.contains(symbol); }
110  bool count(const char *symbol) const
111  { return myFSA.count(symbol); }
112  /// @}
113 
114 
115 private:
116  UT_FSASymbolTable<T> myFSA;
117  UT_Array<const char *> myTokens;
118 };
119 
121 
122 #endif
123 
bool contains(const char *symbol) const
Definition: UT_FSATable.h:108
#define SYS_STATIC_ASSERT(expr)
int findSymbol(const char *symbol, ITEM_T *datap) const
UT_FSATableT()
Empty table constructor.
Definition: UT_FSATable.h:25
void extractStrings(UT_StringArray &list) const
Extract all strings.
Definition: UT_FSATable.h:103
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:462
const char * getToken(T token) const
Definition: UT_FSATable.h:89
void extractStrings(UT_StringArray &list) const
long long int64
Definition: SYS_Types.h:107
A utility class to do read-only operations on a subset of an existing string.
Definition: UT_StringView.h:31
bool count(const char *symbol) const
Definition: UT_FSATable.h:110
bool count(const char *symbol) const
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:453
bool contains(const char *symbol) const
typedef int
Definition: png.h:1175
UT_FSATableT< int,-1 > UT_FSATable
Definition: UT_FSATable.h:120
int64 getMemUsage() const
Returns the memory usage of this object.
Definition: UT_FSATable.h:98
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:122
const char * addSymbol(const char *symbol, const ITEM_T &data)