Jamoma API  0.6.0.a19
TTAddress.h
1 /*
2  * Jamoma Class for accessing Addresses
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_ADDRESS_H__
10 #define __TT_ADDRESS_H__
11 
12 #include "TTAddressBase.h"
13 #include "TTAddressTable.h"
14 
15 class TTRegex;
16 
17 extern TTFOUNDATION_EXPORT TTRegex* ttRegexForInstanceZero; ///< The global regex to parse instance .0
18 
19 /****************************************************************************************************/
20 // Class Specifications
21 
22 /**
23  The TTAddress class is used to represent a string and efficiently pass and compare that string.
24  It is essentially a TTSymbol extended with OSC parsing.
25  An address follows the form:
26  directory:/parent/name.instance:attribute
27  */
28 
29 class TTFOUNDATION_EXPORT TTAddress : public TTSymbol
30 {
31 public:
32 
33  TTAddress()
34  {
35  mSymbolPointer = gTTAddressTable.lookup("");
36  }
37 
38 
39  TTAddress(const char *cstr)
40  {
41  // check if there is a '0'
42  const char* zero = strchr(cstr, C_ZERO);
43  if (zero != 0) {
44 
45  // check if there is a '.' just before
46  if (*(zero-1) == C_INSTANCE) {
47 
48  TTString sparsed;
49  parseInstanceZero(cstr, sparsed);
50 
51  mSymbolPointer = gTTAddressTable.lookup(sparsed);
52 
53  return;
54  }
55  }
56 
57  mSymbolPointer = gTTAddressTable.lookup(cstr);
58  }
59 
60 
61  TTAddress(const int int_to_convert_to_string)
62  {
63  TTString s;
64 
65  s.append(int_to_convert_to_string);
66  mSymbolPointer = gTTAddressTable.lookup(s);
67  }
68 
69 
70  TTAddress(TTPtr do_not_use_this_constructor_unless_you_absolutely_know_what_you_are_doing)
71  {
72  TTSymbolBase* base = (TTSymbolBase*)do_not_use_this_constructor_unless_you_absolutely_know_what_you_are_doing;
73 
74  // we have to check if this symbol base is part of the gTTAddressTable
75  // (to not bind on a symbol base stored into gTTSymbolTable)
76  if (base->getSymbolTableId() == TTPtrSizedInt(&gTTAddressTable))
77  mSymbolPointer = base;
78  else
79  mSymbolPointer = ((TTAddressBase*)gTTAddressTable.lookup(base->getCString()));
80  }
81 
82 
83  /** TTAddress constructor from directory, parent, name, instance and attribute part
84  @param newDirectory directory symbol
85  @param newParent parent address
86  @param newName name symbol
87  @param newInstance instance symbol
88  @param newAttribute attribute symbol */
89  TTAddress(const TTSymbol newDirectory, const TTAddress newParent, const TTSymbol newName, const TTSymbol newInstance, const TTSymbol newAttribute)
90  {
91  mSymbolPointer = ((TTAddressBase*)gTTAddressTable.lookup(""))->edit(newDirectory, newParent.getBasePointer(), newName, newInstance, newAttribute);
92  }
93 
94 
95  //virtual ~TTAddress()
96  //{;}
97 
98 
99  TTAddressBase* getBasePointer() const
100  {
101  return static_cast<TTAddressBase*>(mSymbolPointer);
102  }
103 
104 
105  /** Get the directory part */
107  {
108  return getBasePointer()->getDirectory();
109  }
110 
111  /** Get a pointer to the parent address */
113  {
114  return TTAddress(getBasePointer()->getParent());
115  }
116 
117  /** Get the name part */
119  {
120  return getBasePointer()->getName();
121  }
122 
123  /** Get the instance part */
125  {
126  return getBasePointer()->getInstance();
127  }
128 
129  /** Get the attribute part */
131  {
132  return getBasePointer()->getAttribute();
133  }
134 
135  /** Get the type */
137  {
138  return getBasePointer()->getType();
139  }
140 
141  /** Get the name.instance part */
143  {
144  return getBasePointer()->getNameInstance();
145  }
146 
147  /** Normalize an address for lookup and other directory operations
148  This would return an address without directory and attribute */
150  {
151  return TTAddress(getBasePointer()->normalize());
152  }
153 
154  /** Return a new TTAddress without attribute part */
156  {
157  return TTAddress(getBasePointer()->removeAttribute());
158  }
159 
160  /** Return a new TTAddress with attribute part */
162  {
163  return TTAddress(getBasePointer()->appendAttribute(anAttribute));
164  }
165 
166  /** Return a new TTAddress with the appended part */
168  {
169  return TTAddress(getBasePointer()->appendAddress( (static_cast<TTAddressBase*>(toAppend.mSymbolPointer)) ));
170  }
171 
172  /** Return a new TTAddress with a instance part */
174  {
175  return TTAddress(getBasePointer()->appendInstance(anInstance));
176  }
177 
178  /** A comparison tool
179  @param toCompare An address to compare (it doesn't compare attribute part)
180  @param depthDifference Return the number of level separating the compared addresses (> 0 for lower result, < 0 for upper result)
181  @return An comparison flag */
182  TTAddressComparisonFlag compare(const TTAddress& toCompare, TTInt8& depthDifference)
183  {
184  return getBasePointer()->compare((static_cast<TTAddressBase*>(toCompare.mSymbolPointer)), depthDifference);
185  }
186 
187  /** A parsing tool : split address in two part from a given '/' position
188  @param whereToSplit An int to give the '/' id where to split
189  @param returnedPart1 A pointer to upper part (before the given position) : /part1
190  @param returnedPart2 A pointer to lower part (after the given position) : /part2
191  @return kTTValueNotFound if the given '/' id doesn't exist */
192  TTErr splitAt(TTUInt32 whereToSplit, TTAddress& returnedPart1, TTAddress& returnedPart2)
193  {
194  TTAddressBase* returnedPart1Ptr = NULL;
195  TTAddressBase* returnedPart2Ptr = NULL;
196  TTErr err;
197 
198  err = getBasePointer()->splitAt(whereToSplit, &returnedPart1Ptr, &returnedPart2Ptr);
199  returnedPart1 = TTAddress(returnedPart1Ptr);
200  returnedPart2 = TTAddress(returnedPart2Ptr);
201  return err;
202  }
203 
204  /** A parsing tool : count how many C_SEPARATOR there is in the address
205  @return The number of C_SEPARATOR */
207  {
208  return getBasePointer()->countSeparator();
209  }
210 
211  /** A parsing tool : return a list containing all name.instance part (without any S_SEPARATOR)
212  @param The list of name.instance part to fill */
213  TTErr listNameInstance(TTList& nameInstanceList)
214  {
215  return getBasePointer()->listNameInstance(nameInstanceList);
216  }
217 
218 private:
219  /** Parse ".0"
220  @return An error code if the parsing failed */
221  TTErr parseInstanceZero(const char* cstr, TTString& parsed);
222 
223 };
224 
225 #endif // __TT_ADDRESS_H__
TTAddress appendAddress(const TTAddress &toAppend)
Return a new TTAddress with the appended part.
Definition: TTAddress.h:167
TTSymbol & getInstance()
Get the instance part.
Definition: TTAddress.h:124
TTAddress appendAttribute(TTSymbol anAttribute)
Return a new TTAddress with attribute part.
Definition: TTAddress.h:161
TTSymbol & getDirectory()
Get the directory part.
Definition: TTAddress.h:106
TTSymbolBase * lookup(const char *aString)
Look in the symbol table for this string.
TTAddress getParent()
Get a pointer to the parent address.
Definition: TTAddress.h:112
The TTAddress class is used to represent a string and efficiently pass and compare that string...
Definition: TTAddress.h:29
TTAddress appendInstance(const TTSymbol anInstance)
Return a new TTAddress with a instance part.
Definition: TTAddress.h:173
An address could be.
Definition: TTAddressBase.h:84
TTAddress normalize()
Normalize an address for lookup and other directory operations This would return an address without d...
Definition: TTAddress.h:149
TTAddressType
Type flags to state about absolute or relative address.
Definition: TTAddressBase.h:63
The TTSymbolBase class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbolBase.h:28
TTAddress(const TTSymbol newDirectory, const TTAddress newParent, const TTSymbol newName, const TTSymbol newInstance, const TTSymbol newAttribute)
TTAddress constructor from directory, parent, name, instance and attribute part.
Definition: TTAddress.h:89
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
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr listNameInstance(TTList &nameInstanceList)
A parsing tool : return a list containing all name.instance part (without any S_SEPARATOR) ...
Definition: TTAddress.h:213
TTSymbol & getAttribute()
Get the attribute part.
Definition: TTAddress.h:130
TTSymbol getNameInstance()
Get the name.instance part.
Definition: TTAddress.h:142
TTSymbolBase * mSymbolPointer
pointer to the symbol that we represent
Definition: TTSymbol.h:30
TTAddressComparisonFlag
Comparison flags between address returned by address1->compare(address2).
Definition: TTAddressBase.h:45
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTAddressType getType()
Get the type.
Definition: TTAddress.h:136
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
signed char TTInt8
8 bit signed integer (char)
Definition: TTBase.h:173
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
TTErr splitAt(TTUInt32 whereToSplit, TTAddress &returnedPart1, TTAddress &returnedPart2)
A parsing tool : split address in two part from a given '/' position.
Definition: TTAddress.h:192
TTAddressComparisonFlag compare(const TTAddress &toCompare, TTInt8 &depthDifference)
A comparison tool.
Definition: TTAddress.h:182
TTAddress removeAttribute()
Return a new TTAddress without attribute part.
Definition: TTAddress.h:155
The TTString class is used to represent a string.
Definition: TTString.h:34
TTUInt32 countSeparator()
A parsing tool : count how many C_SEPARATOR there is in the address.
Definition: TTAddress.h:206
TTSymbol & getName()
Get the name part.
Definition: TTAddress.h:118
TTUInt32 getSymbolTableId() const
Return this symboltable's unique id.
Definition: TTSymbolBase.h:74