Jamoma API  0.6.0.a19
TTSymbolBase.h
1 /*
2  * Jamoma Class for representing Symbols
3  * Copyright © 2008, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #ifndef __TT_SYMBOLBASE_H__
10 #define __TT_SYMBOLBASE_H__
11 
12 #include "TTBase.h"
13 #include "TTString.h"
14 
15 /****************************************************************************************************/
16 // Class Specification
17 
18 /**
19  The TTSymbolBase class is used to represent a string and efficiently pass and compare that string.
20 
21  Note that generally speaking you should not directly create a TTSymbolBase!
22  Instead, TTSymbolBase objects should only by created by the TTSymbolBaseTable that you are using.
23  Because of this we *always* pass TTSymbolBases as references (#TTSymbolBaseRef) into the symbol table rather than as pointers or copies.
24 
25  Also, if you don't need the fast lookup capabilities of the symbol table (such as for message or attribute lookup)
26  then consider passing a #TTString instead.
27 */
28 class TTSymbolBase {
29 private:
30  TTPtrSizedInt mSymbolTableId; ///< a unique identifier for the symbol table that owns this symbol
31  TTUInt32 mSymbolId; ///< a unique identifier for the given string
32  TTString mString; ///< the actual string represented by this symbol
33 
34 protected:
35 
36  /** used by the constructors to create the new symbol */
37  void init(const TTString& newString, TTPtrSizedInt newSymbolTableId, TTInt32 newSymbolId);
38 
39 
40 public:
41 
42  /** The constructor is intended only for use by the TTSymbolBaseTable object when creating new symbols
43  in the table. Perhaps this could be made private and then the class made a friend... */
44  TTSymbolBase(const TTString& newString, TTPtrSizedInt newSymbolTableId, TTInt32 newSymbolId);
45 
46 
47  virtual ~TTSymbolBase();
48 
49 
50  /** Copy Constructor */
51  TTSymbolBase(const TTSymbolBase& oldSymbol);
52 
53 
54  /** Return a pointer to the internal string as a C-string. */
55  const char* getCString() const
56  {
57  return mString.c_str();
58  }
59 
60 
61  const TTString& string() const
62  {
63  return mString;
64  }
65 
66 
67  /** Return this symbol's unique id. */
69  {
70  return mSymbolId;
71  }
72 
73  /** Return this symboltable's unique id. */
75  {
76  return mSymbolTableId;
77  }
78 
79  /** Compare two symbols for equality. */
80  inline friend bool operator == (const TTSymbolBase& symbol1, const TTSymbolBase& symbol2)
81  {
82  if (symbol1.mSymbolTableId == symbol2.mSymbolTableId) {
83  // both symbols are in the same symbol table
84  // this should typically be the case and we optimize for this
85  return (symbol1.mSymbolId == symbol2.mSymbolId);
86  }
87  else {
88  // hopefully this won't happen, but it could if there are libs statically linked and communicate with each other
89  return (symbol1.mString == symbol2.mString);
90  }
91  }
92 
93 
94  /** Compare two symbols for inequality. */
95  inline friend bool operator != (const TTSymbolBase& symbol1, const TTSymbolBase& symbol2)
96  {
97  return !(symbol1 == symbol2);
98  }
99 
100 
101  /** Cast a symbol to a C-string. */
102  operator const char*() const
103  {
104  return mString.c_str();
105  }
106 
107 
108  /** Cast a symbol to a TTStringRef. */
109  operator const TTString&() const
110  {
111  return mString;
112  }
113 
114 };
115 
116 
117 /** A reference to a symbol. This is the way symbols are typically communicated throughout the environment. */
119 
120 
121 #endif // __TT_SYMBOLBASE_H__
TTUInt32 getSymbolId() const
Return this symbol's unique id.
Definition: TTSymbolBase.h:68
const char * c_str() const
Return a pointer to the internal C-string.
Definition: TTString.h:83
friend bool operator==(const TTSymbolBase &symbol1, const TTSymbolBase &symbol2)
Compare two symbols for equality.
Definition: TTSymbolBase.h:80
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
const char * getCString() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbolBase.h:55
TTSymbolBase(const TTString &newString, TTPtrSizedInt newSymbolTableId, TTInt32 newSymbolId)
The constructor is intended only for use by the TTSymbolBaseTable object when creating new symbols in...
Jamoma's lowest-level base class and related infrastructure.
void init(const TTString &newString, TTPtrSizedInt newSymbolTableId, TTInt32 newSymbolId)
used by the constructors to create the new symbol
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
friend bool operator!=(const TTSymbolBase &symbol1, const TTSymbolBase &symbol2)
Compare two symbols for inequality.
Definition: TTSymbolBase.h:95
The TTString class is used to represent a string.
Definition: TTString.h:34
TTUInt32 getSymbolTableId() const
Return this symboltable's unique id.
Definition: TTSymbolBase.h:74