Debian Codegraph @

src/core/chuck_oo.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_oo.h
28 // desc: chuck baes objects
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // Ananya Misra ([email protected])
32 // Andrew Schran ([email protected]) - fileIO implementation
33 // date: Autumn 2004
34 //-----------------------------------------------------------------------------
35 #ifndef __CHUCK_OO_H__
36 #define __CHUCK_OO_H__
37
38 #include "chuck_def.h"
39 #include "chuck_carrier.h"
40 #ifndef __DISABLE_THREADS__
41 #include "util_thread.h" // added 1.3.0.0
42 #endif
43
44 #include <string>
45 #include <vector>
46 #include <map>
47 #include <queue>
48
49
50
51
52 // forward reference
53 struct Chuck_Type;
54 struct Chuck_Value;
55 struct Chuck_Func;
56 struct Chuck_Namespace;
57 struct Chuck_Context;
58 struct Chuck_Env;
59 struct Chuck_VM_Code;
60 struct Chuck_VM_Shred;
61 struct Chuck_VM;
62 struct Chuck_IO_File;
63 // utilities
64 class CBufferSimple;
65
66
67
68
69 //-----------------------------------------------------------------------------
70 // name: struct Chuck_VM_Object
71 // desc: base vm object
72 //-----------------------------------------------------------------------------
73 struct Chuck_VM_Object
74 {
75 public:
76 Chuck_VM_Object();
77 virtual ~Chuck_VM_Object();
78
79 public:
80 // add reference (ge: april 2013: made these virtual)
81 virtual void add_ref();
82 // decrement reference; deletes objects when refcount reaches 0
83 virtual void release();
84 // decrement reference only; no deletion | 1.5.4.3 (ge) added
85 virtual void dec_ref_no_release();
86 // lock
87 virtual void lock();
88 // unlock | 1.5.0.0 (ge) added
89 virtual void unlock();
90
91 // NOTE: be careful when overriding these, should always
92 // explicitly call up to ChucK_VM_Object (ge: 2013)
93
94 public:
95 // get reference count
96 t_CKUINT refcount() const;
97
98 public:
99 // unlock_all: dis/allow deletion of locked objects
100 static void lock_all();
101 static void unlock_all();
102 static t_CKBOOL our_locks_in_effect;
103
104 public:
105 t_CKUINT m_ref_count; // reference count
106 t_CKBOOL m_pooled; // if true, this allocates from a pool
107 t_CKBOOL m_locked; // if true, this should never be deleted
108
109 private:
110 void init_ref();
111 };
112
113
114
115
116 //-----------------------------------------------------------------------------
117 // name: struct Chuck_VTable
118 // desc: virtual table
119 //-----------------------------------------------------------------------------
120 struct Chuck_VTable
121 {
122 public:
123 std::vector<Chuck_Func *> funcs;
124 };
125
126
127
128
129 //-----------------------------------------------------------------------------
130 // name: struct Chuck_Object
131 // dsec: base object
132 //-----------------------------------------------------------------------------
133 struct Chuck_Object : public Chuck_VM_Object
134 {
135 public:
136 Chuck_Object();
137 virtual ~Chuck_Object();
138
139 public:
140 // output type info (can be overriden; but probably shouldn't be)
141 virtual void help();
142 // output current state (can be overridden)
143 virtual void dump();
144
145 public:
146 // virtual table
147 Chuck_VTable * vtable;
148 // reference to type
149 Chuck_Type * type_ref;
150 // data for the object
151 t_CKBYTE * data;
152 // the size of the data region
153 t_CKUINT data_size;
154
155 public:
156 // set VM on which this object was instantiated | 1.5.1.5
157 void setOriginVM( Chuck_VM * vm );
158 // set shred on which this object was instantiated | 1.5.1.5
159 void setOriginShred( Chuck_VM_Shred * shred );
160 // get VM on which this object was instantiated | 1.5.1.5
161 Chuck_VM * originVM() const { return origin_vm; }
162 // get shred on which this object was instantiated | 1.5.1.5
163 Chuck_VM_Shred * originShred() const { return origin_shred; }
164
165 protected:
166 // the shred on which this object was instantiated | 1.5.1.5
167 Chuck_VM_Shred * origin_shred;
168 // the VM on which this object was instantiated | 1.5.1.5
169 Chuck_VM * origin_vm;
170
171 public: // static
172 // vtable offset for toString()
173 static t_CKUINT our_vt_toString;
174 };
175
176
177
178
179 // ISSUE: 64-bit (fixed 1.3.1.0)
180 #define CHUCK_ARRAYINT_DATASIZE sz_INT
181 #define CHUCK_ARRAYFLOAT_DATASIZE sz_FLOAT
182 #define CHUCK_ARRAY16_DATASIZE sz_COMPLEX
183 #define CHUCK_ARRAY24_DATASIZE sz_VEC3 // 1.3.5.3
184 #define CHUCK_ARRAY32_DATASIZE sz_VEC4 // 1.3.5.3
185 #define CHUCK_ARRAYINT_DATAKIND kindof_INT
186 #define CHUCK_ARRAYFLOAT_DATAKIND kindof_FLOAT
187 #define CHUCK_ARRAYVEC2_DATAKIND kindof_VEC2 // 1.5.1.7
188 #define CHUCK_ARRAYVEC3_DATAKIND kindof_VEC3 // 1.3.5.3
189 #define CHUCK_ARRAYVEC4_DATAKIND kindof_VEC4 // 1.3.5.3
190 //-----------------------------------------------------------------------------
191 // name: struct Chuck_Array
192 // desc: native ChucK array (virtual base class)
193 // NOTE due to certain specialized operations, e.g., ArrayInt is also
194 // used to hold and manage Object references, this class is currently
195 // not templated (and probably cannot be fully templated). For this
196 // reason, some functionalities that requires type-specific arguments
197 // (like get/set and insert) are not in the base class and are
198 // implemented in array subclasses
199 //-----------------------------------------------------------------------------
200 struct Chuck_Array : public Chuck_Object
201 {
202 public:
203 // constructor
204 Chuck_Array();
205 // destructor
206 virtual ~Chuck_Array();
207
208 public: // interface common to all Chuck_Arary*
209 // get array size
210 virtual t_CKINT size() = 0;
211 // get array capacity
212 virtual t_CKINT capacity() = 0;
213 // set array size
214 virtual t_CKINT set_size( t_CKINT size ) = 0;
215 // set array capacity
216 virtual t_CKINT set_capacity( t_CKINT capacity ) = 0;
217 // size of stored type (from type_ref)
218 virtual t_CKINT data_type_size() = 0;
219 // kind of stored type (from kindof)
220 virtual t_CKINT data_type_kind() = 0;
221 // clear
222 virtual void clear() = 0;
223 // zero out the array by region [start,end)
224 virtual void zero( t_CKUINT start, t_CKUINT end ) = 0;
225 // zero out all elements
226 virtual void zero() = 0;
227
228 // pop the back of the array | 1.5.0.8 (ge) moved into common
229 virtual t_CKINT pop_back() = 0;
230 // pop the front of array | 1.5.0.8 (ge) added
231 virtual t_CKINT pop_front() = 0;
232 // erase | 1.5.0.8 (ge) was pop_out()
233 virtual t_CKINT erase( t_CKINT pos ) = 0;
234 // erase by range [begin,end) | 1.5.0.8 (ge) added
235 virtual t_CKINT erase( t_CKINT begin, t_CKINT end ) = 0;
236
237 // reverse array order | added (1.5.0.0) azaday
238 virtual void reverse() = 0;
239 // reverse array order | added (1.5.0.0) kunwoo, nshaheed, azaday, ge
240 virtual void shuffle() = 0;
241 // sort contents of array in ascending order | added (1.5.1.0) ge
242 virtual void sort() = 0;
243
244 // typed as Object array? | 1.5.0.8 (ge) moved to common
245 virtual t_CKBOOL contains_objects() { return FALSE; }
246
247 public: // map-only operations
248 // get all keys in map | added (1.4.2.0) nshaheed
249 virtual void get_keys( std::vector<std::string> & keys ) = 0;
250 // (map only) test is key is in map | 1.5.0.8 (ge) was find()
251 virtual t_CKINT map_find( const std::string & key ) = 0;
252 // (map only) erase entry by key | 1.5.0.8 (ge) was erase()
253 virtual t_CKINT map_erase( const std::string & key ) = 0;
254
255 public:
256 // Chuck_Type * m_array_type;
257 };
258
259
260
261
262 //-----------------------------------------------------------------------------
263 // name: struct Chuck_ArrayInt
264 // desc: native ChucK arrays
265 // (for 4-byte/8-byte int, depending on 32-bit vs 64-bit & Object refs)
266 // 1.5.1.5 (ge) renamed from Chuck_Array4 to Chuck_ArrayInt
267 //-----------------------------------------------------------------------------
268 struct Chuck_ArrayInt : public Chuck_Array
269 {
270 public:
271 Chuck_ArrayInt( t_CKBOOL is_obj, t_CKINT capacity = 8 );
272 virtual ~Chuck_ArrayInt();
273
274 public: // specific to this class
275 // get address
276 t_CKUINT addr( t_CKINT i );
277 t_CKUINT addr( const std::string & key );
278 // get value
279 t_CKINT get( t_CKINT i, t_CKUINT * val );
280 t_CKINT get( const std::string & key, t_CKUINT * val );
281 t_CKINT get( t_CKINT i, t_CKINT * val ); // signed | 1.5.2.0
282 t_CKINT get( const std::string & key, t_CKINT * val ); // signed | 1.5.2.0
283 // set value
284 t_CKINT set( t_CKINT i, t_CKUINT val );
285 t_CKINT set( const std::string & key, t_CKUINT val );
286
287 // append value
288 t_CKINT push_back( t_CKUINT val );
289 // get value of last element
290 t_CKINT back( t_CKUINT * val ) const;
291 // prepend value | 1.5.0.8 (ge) added
292 // NOTE linear time O(n) operation
293 t_CKINT push_front( t_CKUINT val );
294 // get value of last element
295 t_CKINT front( t_CKUINT * val ) const;
296 // insert before position | 1.5.0.8 (ge)
297 // NOTE linear time O(n) operation
298 t_CKINT insert( t_CKINT pos, t_CKUINT val );
299
300 public: // array interface implementation
301 // get array size
302 virtual t_CKINT size() { return m_vector.size(); }
303 // get array capacity
304 virtual t_CKINT capacity() { return m_vector.capacity(); }
305 // set array size
306 virtual t_CKINT set_size( t_CKINT size );
307 // set array capacity
308 virtual t_CKINT set_capacity( t_CKINT capacity );
309 // size of stored type (from type_ref)
310 virtual t_CKINT data_type_size() { return CHUCK_ARRAYINT_DATASIZE; }
311 // kind of stored type (from kindof)
312 virtual t_CKINT data_type_kind() { return CHUCK_ARRAYINT_DATAKIND; }
313 // clear
314 virtual void clear();
315 // zero out the array by range [start,end)
316 virtual void zero( t_CKUINT start, t_CKUINT end );
317 // zero out all elements
318 virtual void zero();
319
320 // pop the back of the array
321 virtual t_CKINT pop_back();
322 // pop the front of array
323 virtual t_CKINT pop_front();
324 // pop out an element
325 virtual t_CKINT erase( t_CKINT pos );
326 // erase a range of elements [begin,end)
327 virtual t_CKINT erase( t_CKINT begin, t_CKINT end );
328
329 // reverse array order
330 virtual void reverse();
331 // reverse array order
332 virtual void shuffle();
333 // sort contents of array in ascending order
334 virtual void sort();
335
336 // typed as Object array?
337 virtual t_CKBOOL contains_objects() { return m_is_obj; }
338
339 public: // map only
340 // get all keys in map
341 virtual void get_keys( std::vector<std::string> & keys );
342 // (map only) test is key is in map
343 virtual t_CKINT map_find( const std::string & key );
344 // (map only) erase entry by key
345 virtual t_CKINT map_erase( const std::string & key );
346
347 public:
348 std::vector<t_CKUINT> m_vector;
349 std::map<std::string, t_CKUINT> m_map;
350 t_CKBOOL m_is_obj;
351
352 // TODO: may need additional information here for set_size, if this is part of a multi-dim array
353
354 // t_CKINT m_size;
355 // t_CKINT m_capacity;
356 };
357
358
359
360
361 //-----------------------------------------------------------------------------
362 // name: struct Chuck_ArrayFloat
363 // desc: native ChucK arrays (for 8-byte float)
364 // 1.5.1.5 (ge) renamed from Chuck_Array8 to Chuck_ArrayFloat
365 //-----------------------------------------------------------------------------
366 struct Chuck_ArrayFloat : public Chuck_Array
367 {
368 public:
369 Chuck_ArrayFloat( t_CKINT capacity = 8 );
370 virtual ~Chuck_ArrayFloat();
371
372 public: // specific to this class
373 // get address
374 t_CKUINT addr( t_CKINT i );
375 t_CKUINT addr( const std::string & key );
376 // get value
377 t_CKINT get( t_CKINT i, t_CKFLOAT * val );
378 t_CKINT get( const std::string & key, t_CKFLOAT * val );
379 // set value
380 t_CKINT set( t_CKINT i, t_CKFLOAT val );
381 t_CKINT set( const std::string & key, t_CKFLOAT val );
382
383 // append value
384 t_CKINT push_back( t_CKFLOAT val );
385 // get value of last element
386 t_CKINT back( t_CKFLOAT * val ) const;
387 // prepend value | 1.5.0.8 (ge) added
388 // NOTE linear time O(n) operation
389 t_CKINT push_front( t_CKFLOAT val );
390 // get value of last element
391 t_CKINT front( t_CKFLOAT * val ) const;
392 // insert before position | 1.5.0.8 (ge)
393 // NOTE linear time O(n) operation
394 t_CKINT insert( t_CKINT pos, t_CKFLOAT val );
395
396 public: // array interface implementation
397 // get array size
398 virtual t_CKINT size() { return m_vector.size(); }
399 // get array capacity
400 virtual t_CKINT capacity() { return m_vector.capacity(); }
401 // set array size
402 virtual t_CKINT set_size( t_CKINT size );
403 // set array capacity
404 virtual t_CKINT set_capacity( t_CKINT capacity );
405 // size of stored type (from type_ref)
406 virtual t_CKINT data_type_size() { return CHUCK_ARRAYFLOAT_DATASIZE; }
407 // kind of stored type (from kindof)
408 virtual t_CKINT data_type_kind() { return CHUCK_ARRAYFLOAT_DATAKIND; }
409 // clear
410 virtual void clear();
411 // zero out the array by range [start,end)
412 virtual void zero( t_CKUINT start, t_CKUINT end );
413 // zero out all elements
414 virtual void zero() { this->zero(0, m_vector.size()); }
415
416 // pop the back of the array
417 virtual t_CKINT pop_back();
418 // pop the front of array
419 virtual t_CKINT pop_front();
420 // pop out an element
421 virtual t_CKINT erase( t_CKINT pos );
422 // erase a range of elements [begin,end)
423 virtual t_CKINT erase( t_CKINT begin, t_CKINT end );
424
425 // reverse array order
426 virtual void reverse();
427 // reverse array order
428 virtual void shuffle();
429 // sort contents of array in ascending order
430 virtual void sort();
431
432 public: // map only
433 // get all keys in map
434 virtual void get_keys( std::vector<std::string> & keys );
435 // (map only) test is key is in map
436 virtual t_CKINT map_find( const std::string & key );
437 // (map only) erase entry by key
438 virtual t_CKINT map_erase( const std::string & key );
439
440 public:
441 std::vector<t_CKFLOAT> m_vector;
442 std::map<std::string, t_CKFLOAT> m_map;
443 // t_CKINT m_size;
444 // t_CKINT m_capacity;
445 };
446
447
448
449
450 //-----------------------------------------------------------------------------
451 // name: struct Chuck_ArrayVec2
452 // desc: native ChucK arrays (for 16-byte) | vec2, complex, polar
453 // 1.5.2.0 (ge) renamed from Chuck_Array16 to Chuck_ArrayVec2
454 //-----------------------------------------------------------------------------
455 struct Chuck_ArrayVec2 : public Chuck_Array
456 {
457 public:
458 Chuck_ArrayVec2( t_CKINT capacity = 8 );
459 virtual ~Chuck_ArrayVec2();
460
461 public: // specific to this class
462 // get address
463 t_CKUINT addr( t_CKINT i );
464 t_CKUINT addr( const std::string & key );
465 // get value
466 t_CKINT get( t_CKINT i, t_CKVEC2 * val );
467 t_CKINT get( t_CKINT i, t_CKCOMPLEX * val );
468 t_CKINT get( const std::string & key, t_CKVEC2 * val );
469 t_CKINT get( const std::string & key, t_CKCOMPLEX * val );
470 // set value
471 t_CKINT set( t_CKINT i, const t_CKVEC2 & val );
472 t_CKINT set( t_CKINT i, const t_CKCOMPLEX & val );
473 t_CKINT set( const std::string & key, const t_CKVEC2 & val );
474 t_CKINT set( const std::string & key, const t_CKCOMPLEX & val );
475
476 // append value
477 t_CKINT push_back( const t_CKVEC2 & val );
478 // get value of last element
479 t_CKINT back( t_CKVEC2 * val ) const;
480 // prepend value | 1.5.0.8 (ge) added
481 // NOTE linear time O(n) operation
482 t_CKINT push_front( const t_CKVEC2 & val );
483 // get value of last element
484 t_CKINT front( t_CKVEC2 * val ) const;
485 // insert before position | 1.5.0.8 (ge)
486 // NOTE linear time O(n) operation
487 t_CKINT insert( t_CKINT pos, const t_CKVEC2 & val );
488
489 public: // array interface implementation
490 // get array size
491 virtual t_CKINT size() { return m_vector.size(); }
492 // get array capacity
493 virtual t_CKINT capacity() { return m_vector.capacity(); }
494 // set array size
495 virtual t_CKINT set_size( t_CKINT size );
496 // set array capacity
497 virtual t_CKINT set_capacity( t_CKINT capacity );
498 // size of stored type (from type_ref)
499 virtual t_CKINT data_type_size() { return CHUCK_ARRAY16_DATASIZE; }
500 // kind of stored type (from kindof)
501 virtual t_CKINT data_type_kind() { return CHUCK_ARRAYVEC2_DATAKIND; }
502 // clear
503 virtual void clear();
504 // zero out the array by range [start,end)
505 virtual void zero( t_CKUINT start, t_CKUINT end );
506 // zero out all elements
507 virtual void zero() { this->zero(0, m_vector.size()); }
508
509 // pop the back of the array
510 virtual t_CKINT pop_back();
511 // pop the front of array
512 virtual t_CKINT pop_front();
513 // pop out an element
514 virtual t_CKINT erase( t_CKINT pos );
515 // erase a range of elements [begin,end)
516 virtual t_CKINT erase( t_CKINT begin, t_CKINT end );
517
518 // reverse array order
519 virtual void reverse();
520 // reverse array order
521 virtual void shuffle();
522 // sort contents of array in ascending order
523 virtual void sort();
524
525 public: // map only
526 // get all keys in map
527 virtual void get_keys( std::vector<std::string> & keys );
528 // (map only) test is key is in map
529 virtual t_CKINT map_find( const std::string & key );
530 // (map only) erase entry by key
531 virtual t_CKINT map_erase( const std::string & key );
532
533 public:
534 std::vector<t_CKVEC2> m_vector;
535 std::map<std::string, t_CKVEC2> m_map;
536 // semantic hint; in certain situations (like sorting)
537 // need to distinguish between complex and polar | 1.5.1.0
538 t_CKBOOL m_isPolarType;
539 };
540
541
542
543
544 //-----------------------------------------------------------------------------
545 // name: struct Chuck_ArrayVec3
546 // desc: native ChucK arrays (for vec3)
547 // 1.5.2.0 (ge) renamed from Chuck_Array24 to Chuck_ArrayVec3
548 //-----------------------------------------------------------------------------
549 struct Chuck_ArrayVec3 : public Chuck_Array
550 {
551 public:
552 Chuck_ArrayVec3( t_CKINT capacity = 8 );
553 virtual ~Chuck_ArrayVec3();
554
555 public: // specific to this class
556 // get address
557 t_CKUINT addr( t_CKINT i );
558 t_CKUINT addr( const std::string & key );
559 // get value
560 t_CKINT get( t_CKINT i, t_CKVEC3 * val );
561 t_CKINT get( const std::string & key, t_CKVEC3 * val );
562 // set value
563 t_CKINT set( t_CKINT i, const t_CKVEC3 & val );
564 t_CKINT set( const std::string & key, const t_CKVEC3 & val );
565
566 // append value
567 t_CKINT push_back( const t_CKVEC3 & val );
568 // get value of last element
569 t_CKINT back( t_CKVEC3 * val ) const;
570 // prepend value | 1.5.0.8 (ge) added
571 // NOTE linear time O(n) operation
572 t_CKINT push_front( const t_CKVEC3 & val );
573 // get value of last element
574 t_CKINT front( t_CKVEC3 * val ) const;
575 // insert before position | 1.5.0.8 (ge)
576 // NOTE linear time O(n) operation
577 t_CKINT insert( t_CKINT pos, const t_CKVEC3 & val );
578
579 public: // array interface implementation
580 // get array size
581 virtual t_CKINT size() { return m_vector.size(); }
582 // get array capacity
583 virtual t_CKINT capacity() { return m_vector.capacity(); }
584 // set array size
585 virtual t_CKINT set_size( t_CKINT size );
586 // set array capacity
587 virtual t_CKINT set_capacity( t_CKINT capacity );
588 // size of stored type (from type_ref)
589 virtual t_CKINT data_type_size() { return CHUCK_ARRAY24_DATASIZE; }
590 // kind of stored type (from kindof)
591 virtual t_CKINT data_type_kind() { return CHUCK_ARRAYVEC3_DATAKIND; }
592 // clear
593 virtual void clear();
594 // zero out the array by range [start,end)
595 virtual void zero( t_CKUINT start, t_CKUINT end );
596 // zero out all elements
597 virtual void zero() { this->zero(0, m_vector.size()); }
598
599 // pop the back of the array
600 virtual t_CKINT pop_back();
601 // pop the front of array
602 virtual t_CKINT pop_front();
603 // pop out an element
604 virtual t_CKINT erase( t_CKINT pos );
605 // erase a range of elements [begin,end)
606 virtual t_CKINT erase( t_CKINT begin, t_CKINT end );
607
608 // reverse array order
609 virtual void reverse();
610 // reverse array order
611 virtual void shuffle();
612 // sort contents of array in ascending order
613 virtual void sort();
614
615 public: // map only
616 // get all keys in map
617 virtual void get_keys( std::vector<std::string> & keys );
618 // (map only) test is key is in map
619 virtual t_CKINT map_find( const std::string & key );
620 // (map only) erase entry by key
621 virtual t_CKINT map_erase( const std::string & key );
622
623 public:
624 std::vector<t_CKVEC3> m_vector;
625 std::map<std::string, t_CKVEC3> m_map;
626 };
627
628
629
630
631 //-----------------------------------------------------------------------------
632 // name: struct Chuck_ArrayVec4
633 // desc: native ChucK arrays (for vec4)
634 // 1.5.2.0 (ge) renamed from Chuck_Array32 to Chuck_ArrayVec4
635 //-----------------------------------------------------------------------------
636 struct Chuck_ArrayVec4 : public Chuck_Array
637 {
638 public:
639 Chuck_ArrayVec4( t_CKINT capacity = 8 );
640 virtual ~Chuck_ArrayVec4();
641
642 public: // specific to this class
643 // get address
644 t_CKUINT addr( t_CKINT i );
645 t_CKUINT addr( const std::string & key );
646 // get value
647 t_CKINT get( t_CKINT i, t_CKVEC4 * val );
648 t_CKINT get( const std::string & key, t_CKVEC4 * val );
649 // set value
650 t_CKINT set( t_CKINT i, const t_CKVEC4 & val );
651 t_CKINT set( const std::string & key, const t_CKVEC4 & val );
652
653 // append value
654 t_CKINT push_back( const t_CKVEC4 & val );
655 // get value of last element
656 t_CKINT back( t_CKVEC4 * val ) const;
657 // prepend value | 1.5.0.8 (ge) added
658 // NOTE linear time O(n) operation
659 t_CKINT push_front( const t_CKVEC4 & val );
660 // get value of last element
661 t_CKINT front( t_CKVEC4 * val ) const;
662 // insert before position | 1.5.0.8 (ge)
663 // NOTE linear time O(n) operation
664 t_CKINT insert( t_CKINT pos, const t_CKVEC4 & val );
665
666 public: // array interface implementation
667 // get array size
668 virtual t_CKINT size() { return m_vector.size(); }
669 // get array capacity
670 virtual t_CKINT capacity() { return m_vector.capacity(); }
671 // set array size
672 virtual t_CKINT set_size( t_CKINT size );
673 // set array capacity
674 virtual t_CKINT set_capacity( t_CKINT capacity );
675 // size of stored type (from type_ref)
676 virtual t_CKINT data_type_size() { return CHUCK_ARRAY32_DATASIZE; }
677 // kind of stored type (from kindof)
678 virtual t_CKINT data_type_kind() { return CHUCK_ARRAYVEC4_DATAKIND; }
679 // clear
680 virtual void clear();
681 // zero out the array by range [start,end)
682 virtual void zero( t_CKUINT start, t_CKUINT end );
683 // zero out all elements
684 virtual void zero() { this->zero(0, m_vector.size()); }
685
686 // pop the back of the array
687 virtual t_CKINT pop_back();
688 // pop the front of array
689 virtual t_CKINT pop_front();
690 // pop out an element
691 virtual t_CKINT erase( t_CKINT pos );
692 // erase a range of elements [begin,end)
693 virtual t_CKINT erase( t_CKINT begin, t_CKINT end );
694
695 // reverse array order
696 virtual void reverse();
697 // reverse array order
698 virtual void shuffle();
699 // sort contents of array in ascending order
700 virtual void sort();
701
702 public: // map only
703 // get all keys in map
704 virtual void get_keys( std::vector<std::string> & keys );
705 // (map only) test is key is in map
706 virtual t_CKINT map_find( const std::string & key );
707 // (map only) erase entry by key
708 virtual t_CKINT map_erase( const std::string & key );
709
710 public:
711 std::vector<t_CKVEC4> m_vector;
712 std::map<std::string, t_CKVEC4> m_map;
713 };
714
715
716
717
718 //-----------------------------------------------------------------------------
719 // name: enum Chuck_Global_Get_Callback_Type
720 // desc: enumeration for type of callback, used in globals
721 //-----------------------------------------------------------------------------
722 enum Chuck_Global_Get_Callback_Type
723 {
724 ck_get_plain,
725 ck_get_name,
726 ck_get_id
727 };
728
729
730
731
732 //-----------------------------------------------------------------------------
733 // name: Chuck_Global_Event_Listener
734 // desc: base Listener struct
735 //-----------------------------------------------------------------------------
736 struct Chuck_Global_Event_Listener
737 {
738 union {
739 void (* void_callback)(void);
740 void (* named_callback)(const char *);
741 void (* id_callback)(t_CKINT);
742 };
743 t_CKBOOL listen_forever;
744 Chuck_Global_Get_Callback_Type callback_type;
745 std::string name;
746 t_CKINT id;
747 Chuck_Global_Event_Listener() : void_callback(NULL), listen_forever(FALSE),
748 callback_type(ck_get_plain), name(""), id(0) {};
749 };
750
751
752
753
754 //-----------------------------------------------------------------------------
755 // name: Chuck_Event
756 // desc: base Chuck Event class
757 //-----------------------------------------------------------------------------
758 struct Chuck_Event : public Chuck_Object
759 {
760 public:
761 // signal/broadcast "local" -- signal ChucK Events
762 void signal_local();
763 void broadcast_local();
764 void wait( Chuck_VM_Shred * shred, Chuck_VM * vm );
765 t_CKBOOL remove( Chuck_VM_Shred * shred );
766
767 public:
768 // 1.4.0.0 (jack): global events -- signal/broadcast code running
769 // externally, elsewhere in the host
770 void signal_global();
771 void broadcast_global();
772
773 // 1.4.1.0 (jack): global events multiple callbacks.
774 // can listen with a callback(), callback( event name ), or callback( id )
775 void global_listen( void (* cb)(void), t_CKBOOL listen_forever );
776 void global_listen( std::string name, void (* cb)(const char *), t_CKBOOL listen_forever );
777 void global_listen( t_CKINT id, void (* cb)(t_CKINT), t_CKBOOL listen_forever );
778 t_CKBOOL remove_listen( void (* cb)(void) );
779 t_CKBOOL remove_listen( std::string name, void (* cb)(const char *) );
780 t_CKBOOL remove_listen( t_CKINT id, void (* cb)(t_CKINT) );
781
782 public: // internal
783 // added 1.3.0.0: queue_broadcast now takes event_buffer
784 void queue_broadcast( CBufferSimple * event_buffer = NULL );
785
786 public:
787 // virtual table offset for can_wait()
788 static t_CKUINT our_can_wait;
789 // virtual table offset for waiting_on() | 1.5.1.5 (ge/andrew) added
790 static t_CKUINT our_waiting_on;
791
792 protected:
793 std::queue<Chuck_VM_Shred *> m_queue;
794 std::queue<Chuck_Global_Event_Listener> m_global_queue;
795
796 #ifndef __DISABLE_THREADS__
797 // 1.4.1.0 (ge/jack) TODO: rewrite queue_broadcast to use a lock-free queue
798 // and avoid the use of a lock in events
799 XMutex m_queue_lock;
800 #endif
801 };
802
803
804
805
806 //-----------------------------------------------------------------------------
807 // name: Chuck_String
808 // desc: base Chuck string class
809 //-----------------------------------------------------------------------------
810 struct Chuck_String : public Chuck_Object
811 {
812 public:
813 // constructor
814 Chuck_String( const std::string & s = "" ) { set( s ); }
815 // destructor | 1.5.0.1 (ge) made destructor virtual
816 virtual ~Chuck_String() { }
817
818 // set string (makes copy)
819 void set( const std::string & s ) { m_str = s; m_charptr = m_str.c_str(); }
820 // get as standard c++ string
821 const std::string & str() { return m_str; }
822 // get as C string (NOTE: use this in dynamical modules like chugins!)
823 const char * c_str() { return m_charptr; }
824
825 private:
826 // c pointer | HACK: needed for ensure string passing in dynamic modules
827 const char * m_charptr; // REFACTOR-2017
828 // c++ string
829 std::string m_str;
830 };
831
832
833
834
835 #endif