-
Notifications
You must be signed in to change notification settings - Fork 14
/
TTValue.h
709 lines (587 loc) · 18.3 KB
/
TTValue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/** @file
@ingroup foundationLibrary
@brief Provides a common way of representing composite values.
@details #TTValue is the primary interface used to pass values to and from methods in Jamoma Core.
Methods for a given #TTObject should be passed both an input and output value to complete its operation, while the return is reserved for a #TTErr.
@n@n
Each #TTValue may be composed of a single element or many elements because it has been defined as a subclass of the C++ standard library's <a href="http://www.cplusplus.com/reference/vector/vector/">vector class</a>.
This also enables our class to inherit familiar functions such as size() and from its parent class.
@n@n
Individual items within the #TTValue are defined by the #TTElement class.
These individual elements may be one of the defined types in the #TTDataType enumeration.
@author Tim Place, Théo de la Hogue, Nathan Wolek, Julien Rabin, Nils Peters, Trond Lossius
@copyright Copyright © 2008, Timothy Place @n
This code is licensed under the terms of the "New BSD License" @n
http://creativecommons.org/licenses/BSD/
*/
#ifndef __TT_VALUE_H__
#define __TT_VALUE_H__
#include "TTElement.h"
#include <functional>
//! [doxygenAppendixC_copyExample]
/** @copybrief TTValue.h
@copydetails TTValue.h
*/
class TTValue : public TTElementVector {
//! [doxygenAppendixC_copyExample]
public:
/** @brief Constructor for an empty value */
TTValue()
{
// we reserve 1 because we want to always be assured that there is a memory for at least one element.
// it is exceedingly rare that a TTValue will be used empty, and most common that it will have one element.
// for speed, it is possible to use abuse TTValue by setting and getting the first element without range or type checking
// and even bypassing the setter methods entirely.
// in these cases it is critical to be able to rely upon the memory being valid for the first element.
reserve(1);
}
/** @brief Constructor with a single initial element. */
template<class T>
TTValue(const T& anInitialValue)
{
resize(1);
(*this)[0] = anInitialValue;
}
/** @brief Specialized constructor for the float64 case because it is so often performance sensitive. */
TTValue(const TTFloat64 v)
{
resize(1);
(*this)[0].float64(v);
}
/** @brief Constructor with two initial elements. */
template <class T, class U>
TTValue(const T& aFirstElementInitialValue, const U& aSecondElementInitialValue)
{
resize(2);
(*this)[0] = aFirstElementInitialValue;
(*this)[1] = aSecondElementInitialValue;
}
/** @brief Constructor with three initial elements. */
template <class T, class U, class V>
TTValue(const T& aFirstElementInitialValue, const U& aSecondElementInitialValue, const V& aThirdElementInitialValue)
{
resize(3);
(*this)[0] = aFirstElementInitialValue;
(*this)[1] = aSecondElementInitialValue;
(*this)[2] = aThirdElementInitialValue;
}
/** @brief Constructor with four initial elements. */
template <class T, class U, class V, class W>
TTValue(const T& aFirstElementInitialValue, const U& aSecondElementInitialValue, const V& aThirdElementInitialValue, const W& aFourthElementInitialValue)
{
resize(4);
(*this)[0] = aFirstElementInitialValue;
(*this)[1] = aSecondElementInitialValue;
(*this)[2] = aThirdElementInitialValue;
(*this)[3] = aFourthElementInitialValue;
}
// force the destructor to be non-virtual
// we don't want subclasses of TTValue so it won't be a problem, and this solves linking snafus in some edge cases
/** @brief Destructor */
~TTValue()
{;}
private:
/** @brief Internal method used by the constructors. */
void init();
/** @brief Performs a deep copy of the object */
inline void copy(const TTValue& obj);
public:
/** Fast fetch of float64 elements without range/type checking or conversion */
TTFloat64 float64() const
{
return (*this)[0].float64();
}
/** Fast assignment of float64 elements without range/type checking or conversion
@param v the float to assign to this value.
@return a reference to this value object.
*/
TTValue& float64(TTFloat64 v)
{
(*this)[0].float64(v);
return *this;
}
/** @brief Clear all values from the vector, leaving with size of 0 */
void clear()
{
TTElementVector::clear();
}
/** @brief Copy a value starting from an index until another index */
void copyRange(const TTValue& obj, TTUInt16 startIndex, TTUInt16 endIndex)
{
resize(endIndex - startIndex);
for (size_t i=0; i<size(); i++)
(*this)[i] = obj[startIndex+i];
}
/** @brief Copy a value starting from an index until the last element */
void copyFrom(const TTValue& obj, TTUInt16 index)
{
copyRange(obj, index, static_cast<TTUInt16>(obj.size()));
}
/** @brief Insert another TTValue before the first element.
@details
The following example code would result in TTValue b having elements ordered <1, 2, 3, ga, bu, zo, meu>:
@code{.cpp}
TTValue a(1, 2, 3);
TTValue b(ga, bu, zo, meu);
b.prepend(a);
@endcode
*/
void prepend(const TTValue& aValueToPrepend)
{
TTValue v = aValueToPrepend;
v.append(*this);
*this = v;
}
/** @brief Assign a value to TTValue.
@details Overwrites current elements.
*/
template<class T>
TTValue& operator = (T value)
{
resize(1);
(*this)[0] = value;
return *this;
}
/** @brief Test equality of two values */
friend bool operator == (const TTValue& a, const TTValue& b)
{
if (a.size() == b.size()) {
for (size_t i=0; i<a.size(); i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
return false;
}
#if !defined(TT_PLATFORM_WIN) && (!defined(__GNUC__) || defined(__clang__))
/** @overload
*/
template<class T>
friend bool operator == (const TTValue& a, const T b)
{
if (a.size() == 1 && a[0] == b)
return true;
else
return false;
}
#endif
/** @brief Get a value from TTValue */
template<class T>
operator T() const
{
if (size())
return T((*this)[0]);
else
return T(0);
}
// TTSymbol needs to be manually wrapped to avoid ambiguity as interpretted by the clang compiler
/** @overload
*/
operator TTSymbol() const
{
if (size())
return (*this)[0];
else
return kTTSymEmpty;
}
// TTObject needs to be manually wrapped to avoid ambiguity as interpretted by the clang compiler
/** @overload
*/
operator TTObject() const
{
if (size())
return (*this)[0];
else
return TTObject();
}
/** @brief Insert a single TTElement at the end */
template<class T>
void append(const T& anElementValueToAppend)
{
TTElement e(anElementValueToAppend);
push_back(e);
}
/** @brief Insert another TTValue after the last element.
@details
The following example code would result in TTValue b having elements ordered <ga, bu, zo, meu, 1, 2, 3>:
@code{.cpp}
TTValue a(1, 2, 3);
TTValue b(ga, bu, zo, meu);
b.append(a);
@endcode
*/
void append(const TTValue& aValueToAppend)
{
TTUInt32 appendingElementCount = static_cast<TTUInt32>(aValueToAppend.size());
TTUInt32 oldElementCount = static_cast<TTUInt32>(size());
TTUInt32 newElementCount = oldElementCount + appendingElementCount;
resize(newElementCount);
for (TTUInt32 i=0; i<appendingElementCount; i++) {
TTElement e = aValueToAppend[i];
(*this)[oldElementCount+i] = e;
}
}
/** @brief Clip numerical values between low and high boundaries
@param[in] aLowBound Lowest value that should be preserved. Anything lower will be set to this value.
@param[in] aHighBound Highest value that should be preserved. Anything higher will be set to this value.
@return none
*/
void clip(const TTFloat64& aLowBound, const TTFloat64& aHighBound)
{
for (TTElementIter i = this->begin(); i != this->end(); i++)
i->clip(aLowBound, aHighBound);
}
/** @brief Clip numerical values below a specified boundary
@param[in] aLowBound Lowest value that should be preserved. Anything lower will be set to this value.
@return none
*/
void cliplow(const TTFloat64& aLowBound)
{
for (TTElementIter i = this->begin(); i != this->end(); i++)
i->cliplow(aLowBound);
}
/** @brief Clip numerical values above a specified boundary
@param[in] aHighBound Highest value that should be preserved. Anything higher will be set to this value.
@return none
*/
void cliphigh(const TTFloat64& aHighBound)
{
for (TTElementIter i = this->begin(); i != this->end(); i++)
i->cliphigh(aHighBound);
}
/** @brief Fold numerical values to remain between low and high boundaries
@param[in] aLowBound Lowest value that should be preserved. Anything lower will be folder.
@param[in] aHighBound Highest value that should be preserved. Anything higher will be folded.
@return none
*/
void fold(const TTFloat64& aLowBound, const TTFloat64& aHighBound)
{
for (TTElementIter i = this->begin(); i != this->end(); i++)
i->fold(aLowBound, aHighBound);
}
/** @brief Wrap numerical values to remain between low and high boundaries
@param[in] aLowBound Lowest value that should be preserved. Anything lower will be wrapped.
@param[in] aHighBound Highest value that should be preserved. Anything higher will be wrapped.
@return none
*/
void wrap(const TTFloat64& aLowBound, const TTFloat64& aHighBound)
{
for (TTElementIter i = this->begin(); i != this->end(); i++)
i->wrap(aLowBound, aHighBound);
}
/** @brief Round float & double elements to the nearest whole number */
void round()
{
for_each(this->begin(), this->end(), std::mem_fun_ref(&TTElement::round));
}
/** @brief Truncate float & double elements so that only whole number remains */
void truncate()
{
for_each(this->begin(), this->end(), std::mem_fun_ref(&TTElement::truncate));
}
/** @brief Booleanize numerical elements
@details Sets all non-zero numerical elements to true, while those that are zero will be set to false. Changes the #TTDataType of these elements to kTypeBoolean.
*/
void booleanize()
{
for_each(this->begin(), this->end(), std::mem_fun_ref(&TTElement::booleanize));
}
/** @brief Return the content as a single string with spaces between elements
@param none
@return #TTString that contains the content of all elements in the #TTValue
*/
TTString toString(TTBoolean quotes = YES) const
{
TTString temp;
for (size_t i=0; i<size(); i++) {
(*this)[i].string(temp, quotes); // get a string for each item
if (i < (size()-1)) // add a space between each item, but no space at the end
temp.append(" ");
}
return temp;
}
// TODO: Could this be DRYer?
/** @overload
*/
void toString(TTBoolean quotes = YES)
{
TTString temp;
for (size_t i=0; i<size(); i++) {
(*this)[i].string(temp, quotes); // get a string for each item
if (i < (size()-1)) // add a space between each item, but no space at the end
temp.append(" ");
}
// now set the value to the new string
clear();
append(temp);
}
/** @brief Convert a single string into individual elements using space to divide items
@param numberAsSymbol optional #TTBoolean determines whether method leaves numbers as symbols, default is NO
@return none
*/
void fromString(TTBoolean numberAsSymbol = NO)
{
if (at(0).type() != kTypeString) { // if the first element isn't a string
clear(); // clear the contents of the value
return; // and do nothing else
}
TTUInt32 n = 0;
TTInt32 convertedInt;
TTUInt32 convertedUInt;
TTFloat32 convertedFloat;
std::vector<std::string> strList;
std::string str(TTString(at(0)));
std::istringstream iss(str);
std::copy(
std::istream_iterator<std::string>( iss ),
std::istream_iterator<std::string>(),
back_inserter( strList ) );
if (strList.size() < 1) {
clear();
return;
}
resize(strList.size());
for (unsigned int i = 0; i < strList.size(); ++i) {
TTString currentString = strList.at(i).c_str();
if (currentString.toTTInt32(convertedInt) && !numberAsSymbol) {
at(n) = int(convertedInt);
n++;
}
else if (currentString.toTTUInt32(convertedUInt) && !numberAsSymbol) {
at(n) = TTUInt32(convertedUInt);
n++;
}
else if (currentString.toTTFloat32(convertedFloat) && !numberAsSymbol) {
at(n) = TTFloat64(convertedFloat); // cast float32 into float64
n++;
}
else {
if (currentString.c_str()[0] == '"') {
TTString editString = currentString.substr(1, currentString.size()); // don't keep the leading "
while (currentString.c_str()[currentString.size()-1] != '"' && (i != (strList.size() - 1))) {
i++;
currentString = strList.at(i);
editString += " ";
editString += currentString;
}
at(n) = TTSymbol(editString.substr(0, editString.size()-1));
n++;
}
else {
at(n) = TTSymbol(currentString.c_str());
n++;
}
}
}
// resize value
resize(n);
}
/** @brief Convert a comma-separated-value string into an array of TTSymbols.
@return kTTErrInvalidType if first item is not kTypeString, else kTTErrNone
*/
TTErr transformCSVStringToSymbolArray()
{
if (at(0).type() != kTypeString)
return kTTErrInvalidType;
const TTString& str = at(0);
char* cStr;
char* current;
clear();
cStr = new char[str.size()+1];
strncpy(cStr, str.c_str(), str.size()+1);
current = strrchr(cStr, ',');
while (current) {
*current = 0;
current++;
// Do some basic whitespace stripping from the ends
while (*current == ' ')
current++;
while (current[strlen(current)-1] == ' ')
current[strlen(current)-1] = 0;
append(TTSymbol(current));
current = strrchr(cStr, ',');
}
append(TTSymbol(cStr));
delete[] cStr;
return kTTErrNone;
}
// inherited functions from Vector class
#ifdef _DOXY_
/** @brief Return the number of elements
@details Inherited from the C++ standard library's <a href="http://www.cplusplus.com/reference/vector/vector/">vector class</a>
@param none
@return number of elements currently in #TTValue
*/
size_type size() const noexcept;
/** @brief Change the number of elements
@details Inherited from the C++ standard library's <a href="http://www.cplusplus.com/reference/vector/vector/">vector class</a>
@param n number of elements for resulting #TTValue
@return void
*/
void resize (size_type n);
#endif
// deprecated functions
/** @brief DEPRECATED
@deprecated instead, please call the size() method */
TT_DEPRECATED( TTUInt16 getSize() const )
{
return static_cast<TTUInt16>(size());
}
/** @brief DEPRECATED
@deprecated instead, please call the resize() method */
TT_DEPRECATED( void setSize(const TTUInt16 arg) )
{
resize(arg);
}
/** @brief DEPRECATED
@deprecated instead, please call TTElement::type() on the element itself.
@details
Old syntax:
@code{.cpp}
TTValue v(1,2,3);
TTDataType thetype = v.getType(1);
@endcode
New syntax:
@code{.cpp}
TTValue v(1,2,3);
TTDataType thetype = v[1].type();
@endcode
*/
TT_DEPRECATED( TTDataType getType(const TTUInt16 index=0) const )
{
return at(index).type();
}
/** @brief DEPRECATED
@deprecated instead, please make an assignment using standard C array syntax.
@details
Old syntax:
@code{.cpp}
TTValue v;
v.set(0, 3.14);
@endcode
New syntax:
@code{.cpp}
TTValue v;
v[0] = 3.14;
@endcode
*/
template<class T>
TT_DEPRECATED ( void set(const TTUInt16 index, const T& anElementValue) )
{
at(index) = anElementValue;
}
/** @brief DEPRECATED
@deprecated instead, please fetch the value of an element using standard C array syntax.
@details
Old syntax:
@code{.cpp}
TTValue v(3.14);
TTFloat64 mypi;
v.get(0, mypi);
@endcode
New syntax:
@code{.cpp}
TTValue v(3.14);
TTFloat64 mypi;
mypi = v[0];
@endcode
*/
template<class T>
TT_DEPRECATED ( void get(const TTUInt16 index, T& returnedElementValue) const )
{
returnedElementValue = at(index);
}
/*
TT_DEPRECATED ( void get(const TTUInt16 index, TTObjectBase** value) const )
{
if (at(index).type() == kTypeObject)
*value = at(index);
}
*/
TT_DEPRECATED ( void get(const TTUInt16 index, TTPtr* value) const )
{
if (at(index).type() == kTypePointer)
*value = at(index);
}
TT_DEPRECATED ( void get(const TTUInt16 index, TTString& value) const )
{
value = (TTString)at(index);
}
// inlined for speed (e.g. for use in the matrix)
/** @brief DEPRECATED
@deprecated inlined function formerly used by #TTMatrix or @ref foundationDataspaceLib
*/
TT_DEPRECATED( TTFloat64 getUInt8(TTUInt16 index = 0) const )
{
return TTUInt8(at(index));
}
// inlined for speed (e.g. for use in the matrix)
/** @copydoc getUInt8()
*/
TT_DEPRECATED( TTFloat64 getInt32(TTUInt16 index = 0) const )
{
return TTInt32(at(index));
}
/** @copydoc getUInt8()
*/
TT_DEPRECATED( TTFloat64 getFloat32(TTUInt16 index = 0) const )
{
return TTFloat32(at(index));
}
/** @copydoc getUInt8()
*/
TT_DEPRECATED( TTFloat64 getFloat64(TTUInt16 index = 0) const )
{
return TTFloat64(at(index));
}
/** @copydoc getUInt8()
*/
TT_DEPRECATED( void getArray(TTUInt8* arrayToFill, TTUInt16 maxSize) const )
{
for (size_t i=0; i<size(); i++) {
if (i == maxSize)
break;
*(arrayToFill+i) = TTUInt8(at(i));
}
}
/** @overload
*/
TT_DEPRECATED( void getArray(TTInt32* arrayToFill, TTUInt16 maxSize) const )
{
for (size_t i=0; i<size(); i++) {
if (i == maxSize)
break;
*(arrayToFill+i) = TTInt32(at(i));
}
}
/** @overload
*/
TT_DEPRECATED( void getArray(TTFloat32* arrayToFill, TTUInt16 maxSize) const )
{
for (size_t i=0; i<size(); i++) {
if (i == maxSize)
break;
*(arrayToFill+i) = TTFloat32(at(i));
}
}
/** @overload
*/
TT_DEPRECATED( void getArray(TTFloat64* arrayToFill, TTUInt16 maxSize) const )
{
for (size_t i=0; i<size(); i++) {
if (i == maxSize)
break;
*(arrayToFill+i) = TTFloat64(at(i));
}
}
};
typedef TTValue* TTValuePtr;
typedef TTValue& TTValueRef;
typedef const TTValue& TTValueConstRef;
// dumb global which is an empty / uninitialized symbol -- you shouldn't use it.
// it's only here for backwards compatibility reasons.
typedef void* TTNoValue;
#define kTTValNONE (TTNoValue(0))
#endif // __TT_VALUE_H__