Jamoma API  0.6.0.a19
TTSymbol.h
1 /*
2  * Jamoma Class for accessing Symbols
3  * Copyright © 2012, 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_SYMBOL_H__
10 #define __TT_SYMBOL_H__
11 
12 #include "TTSymbolBase.h"
13 #include "TTSymbolTable.h"
14 
15 
16 /****************************************************************************************************/
17 // Class Specification
18 
19 /**
20  The TTSymbol class is used to represent a string and efficiently pass and compare that string.
21 
22  If you don't need the fast lookup capabilities of the symbol table (such as for message or attribute lookup)
23  then consider passing a #TTString instead to keep the internal symbol table from becoming bloated.
24 */
25 
26 class TTFOUNDATION_EXPORT TTSymbol
27 {
28 protected:
29 
30  TTSymbolBase* mSymbolPointer; ///< pointer to the symbol that we represent
31 
32 public:
33 
34  TTSymbol()
35  {
36  mSymbolPointer = gTTSymbolTable.lookup("");
37  }
38 
39 
40  TTSymbol(const char *cstr)
41  {
42  mSymbolPointer = gTTSymbolTable.lookup(cstr);
43  }
44 
45 
46  TTSymbol(const TTString& aString)
47  {
48  mSymbolPointer = gTTSymbolTable.lookup(aString);
49  }
50 
51 
52  TTSymbol(const int int_to_convert_to_string)
53  {
54  TTString s;
55 
56  s.append(int_to_convert_to_string);
57  mSymbolPointer = gTTSymbolTable.lookup(s);
58  }
59 
60  TTSymbol(TTPtr do_not_use_this_constructor_unless_you_absolutely_know_what_you_are_doing)
61  {
62  mSymbolPointer = (TTSymbolBase*)do_not_use_this_constructor_unless_you_absolutely_know_what_you_are_doing;
63  }
64 
65  // copy constructor
66  TTSymbol(const TTSymbol& otherSymbol)
67  {
68  mSymbolPointer = otherSymbol.mSymbolPointer;
69  }
70 
71 
72  //virtual ~TTSymbol()
73  //{;}
74 
75 
76  /** Return a pointer to the internal string as a C-string. */
77  const char* c_str() const
78  {
79  return mSymbolPointer->getCString();
80  }
81 
82 
83  const TTString& string() const
84  {
85  return mSymbolPointer->string();
86  }
87 
88 
89  /** Return this symbol's unique id. */
91  {
92  return mSymbolPointer->getSymbolId();
93  }
94 
95 
96  /** Compare two symbols for equality. */
97  inline friend bool operator == (const TTSymbol& symbol1, const TTSymbol& symbol2)
98  {
99  return (*symbol1.mSymbolPointer == *symbol2.mSymbolPointer);
100  }
101 
102 
103  /** Compare two symbols for inequality. */
104  inline friend bool operator != (const TTSymbol& symbol1, const TTSymbol& symbol2)
105  {
106  return (*symbol1.mSymbolPointer != *symbol2.mSymbolPointer);
107  }
108 
109 
110  /** Compare a symbols against a c-string for equality. */
111  inline friend bool operator == (const TTSymbol& symbol1, const char* aCString)
112  {
113  return symbol1 == TTSymbol(aCString);
114  }
115 
116 
117  /** Cast a symbol to a C-string. */
118  operator const char*() const
119  {
120  return mSymbolPointer->getCString();
121  }
122 
123 
124  /** Cast a symbol to a TTStringRef. */
125  operator const TTString&() const
126  {
127  return mSymbolPointer->string();
128  }
129 
130 
131  /** Get the value of the raw pointer into the symbol table.
132  Do not use this unless you absolutely know what you are doing.
133  */
135  {
136  return TTPtr(mSymbolPointer);
137  }
138 
139 
140  /** Generate a pseudo-random symbol */
141  static TTSymbol random()
142  {
143  TTString s;
144 
145  s.random();
146  return TTSymbol(s);
147  }
148 
149 };
150 
151 
152 /** This macro is defined as a shortcut for doing a lookup in the symbol table.
153  @ingroup macros
154  */
155 #define TT TTSymbol
156 
157 
158 #endif // __TT_SYMBOL_H__
TTUInt32 getSymbolId() const
Return this symbol's unique id.
Definition: TTSymbolBase.h:68
bool TTFOUNDATION_EXPORT operator!=(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for inequality.
Definition: TTObject.cpp:173
TTSymbolBase * lookup(const char *aString)
Look in the symbol table for this string.
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
static TTSymbol random()
Generate a pseudo-random symbol.
Definition: TTSymbol.h:141
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
const char * getCString() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbolBase.h:55
TTPtr rawpointer() const
Get the value of the raw pointer into the symbol table.
Definition: TTSymbol.h:134
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
void TTFOUNDATION_EXPORT random()
Replace contents with a pseudo-random string.
Definition: TTString.cpp:13
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.
Definition: TTObject.cpp:167
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
TTSymbolBase * mSymbolPointer
pointer to the symbol that we represent
Definition: TTSymbol.h:30
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
void append(const char *str, size_t length=0)
Append / Concatenate.
Definition: TTString.h:196
The TTString class is used to represent a string.
Definition: TTString.h:34
TTUInt32 getSymbolId() const
Return this symbol's unique id.
Definition: TTSymbol.h:90