1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_OO_H__
34 #define __CHUCK_OO_H__
36 #include "chuck_def.h"
48 struct Chuck_Namespace
;
52 struct Chuck_VM_Shred
;
59 //-----------------------------------------------------------------------------
60 // name: struct Chuck_VM_Object
61 // desc: base vm object
62 //-----------------------------------------------------------------------------
63 struct Chuck_VM_Object
66 Chuck_VM_Object() { this->init_ref(); }
67 virtual ~Chuck_VM_Object() { }
78 // unlock_all: dis/allow deletion of locked objects
79 static void lock_all();
80 static void unlock_all();
81 static t_CKBOOL our_locks_in_effect
;
84 t_CKUINT m_ref_count
; // reference count
85 t_CKBOOL m_pooled
; // if true, this allocates from a pool
86 t_CKBOOL m_locked
; // if true, this should never be deleted
90 std::vector
<Chuck_VM_Object
*> * m_v_ref
;
99 //-----------------------------------------------------------------------------
100 // name: struct Chuck_VM_Alloc
101 // desc: vm object manager
102 //-----------------------------------------------------------------------------
103 struct Chuck_VM_Alloc
106 static Chuck_VM_Alloc
* instance();
109 void add_object( Chuck_VM_Object
* obj
);
110 void free_object( Chuck_VM_Object
* obj
);
113 static Chuck_VM_Alloc
* our_instance
;
120 std::map
<Chuck_VM_Object
*, void *> m_objects
;
126 //-----------------------------------------------------------------------------
127 // name: struct Chuck_VTable
128 // desc: virtual table
129 //-----------------------------------------------------------------------------
133 std::vector
<Chuck_Func
*> funcs
;
139 //-----------------------------------------------------------------------------
140 // name: struct Chuck_Object
142 //-----------------------------------------------------------------------------
143 struct Chuck_Object
: Chuck_VM_Object
147 virtual ~Chuck_Object();
151 Chuck_VTable
* vtable
;
153 Chuck_Type
* type_ref
;
154 // the size of the data region
156 // data for the object
163 #define CHUCK_ARRAY4_DATASIZE 4
164 #define CHUCK_ARRAY8_DATASIZE 8
165 #define CHUCK_ARRAY16_DATASIZE 16
166 //-----------------------------------------------------------------------------
167 // name: struct Chuck_Array
168 // desc: native ChucK arrays ( virtual base class )
169 //-----------------------------------------------------------------------------
170 struct Chuck_Array
: Chuck_Object
172 // functionality that we can keep in common...
176 // virtual ~Chuck_Array() { }
178 virtual t_CKINT
size( ) = 0; // const { return m_size; } // array size
179 virtual t_CKINT
capacity( ) = 0; // const { return m_capacity; } // array capacity
180 virtual t_CKINT
set_size( t_CKINT size
) = 0; // array size
181 virtual t_CKINT
set_capacity( t_CKINT capacity
) = 0; // set
182 virtual t_CKINT
data_type_size( ) = 0; // size of stored type ( from type_ref )
183 virtual t_CKINT
find( const std::string
& key
) = 0; // find
184 virtual t_CKINT
erase( const std::string
& key
) = 0; // erase
185 virtual void clear( ) = 0; // clear
191 //-----------------------------------------------------------------------------
192 // name: struct Chuck_Array4
193 // desc: native ChucK arrays (for 4-byte)
194 //-----------------------------------------------------------------------------
195 struct Chuck_Array4
: Chuck_Array
198 Chuck_Array4( t_CKBOOL is_obj
, t_CKINT capacity
= 8 );
199 virtual ~Chuck_Array4();
202 t_CKUINT
addr( t_CKINT i
);
203 t_CKUINT
addr( const std::string
& key
);
204 t_CKINT
get( t_CKINT i
, t_CKUINT
* val
);
205 t_CKINT
get( const std::string
& key
, t_CKUINT
* val
);
206 t_CKINT
set( t_CKINT i
, t_CKUINT val
);
207 t_CKINT
set( const std::string
& key
, t_CKUINT val
);
208 t_CKINT
push_back( t_CKUINT val
);
210 t_CKINT
back( t_CKUINT
* val
) const;
211 void zero( t_CKUINT start
, t_CKUINT end
);
213 virtual void clear( );
214 virtual t_CKINT
size( ) { return m_vector
.size(); }
215 virtual t_CKINT
capacity( ) { return m_vector
.capacity(); }
216 virtual t_CKINT
set_size( t_CKINT size
);
217 virtual t_CKINT
set_capacity( t_CKINT capacity
);
218 virtual t_CKINT
find( const std::string
& key
);
219 virtual t_CKINT
erase( const std::string
& key
);
220 virtual t_CKINT
data_type_size( ) { return CHUCK_ARRAY4_DATASIZE
; }
223 std::vector
<t_CKUINT
> m_vector
;
224 std::map
<std::string
, t_CKUINT
> m_map
;
227 // t_CKINT m_capacity;
233 //-----------------------------------------------------------------------------
234 // name: struct Chuck_Array8
235 // desc: native ChucK arrays (for 8-byte)
236 //-----------------------------------------------------------------------------
237 struct Chuck_Array8
: Chuck_Array
240 Chuck_Array8( t_CKINT capacity
= 8 );
241 virtual ~Chuck_Array8();
244 t_CKUINT
addr( t_CKINT i
);
245 t_CKUINT
addr( const std::string
& key
);
246 t_CKINT
get( t_CKINT i
, t_CKFLOAT
* val
);
247 t_CKINT
get( const std::string
& key
, t_CKFLOAT
* val
);
248 t_CKINT
set( t_CKINT i
, t_CKFLOAT val
);
249 t_CKINT
set( const std::string
& key
, t_CKFLOAT val
);
250 t_CKINT
push_back( t_CKFLOAT val
);
252 t_CKINT
back( t_CKFLOAT
* val
) const;
253 void zero( t_CKUINT start
, t_CKUINT end
);
255 virtual void clear( );
256 virtual t_CKINT
size( ) { return m_vector
.size(); }
257 virtual t_CKINT
capacity( ) { return m_vector
.capacity(); }
258 virtual t_CKINT
set_size( t_CKINT size
);
259 virtual t_CKINT
set_capacity( t_CKINT capacity
);
260 virtual t_CKINT
find( const std::string
& key
);
261 virtual t_CKINT
erase( const std::string
& key
);
262 virtual t_CKINT
data_type_size( ) { return CHUCK_ARRAY8_DATASIZE
; }
265 std::vector
<t_CKFLOAT
> m_vector
;
266 std::map
<std::string
, t_CKFLOAT
> m_map
;
268 // t_CKINT m_capacity;
273 //-----------------------------------------------------------------------------
274 // name: struct Chuck_Array16
275 // desc: native ChucK arrays (for 16-byte)
276 //-----------------------------------------------------------------------------
277 struct Chuck_Array16
: Chuck_Array
280 Chuck_Array16( t_CKINT capacity
= 8 );
281 virtual ~Chuck_Array16();
284 t_CKUINT
addr( t_CKINT i
);
285 t_CKUINT
addr( const std::string
& key
);
286 t_CKINT
get( t_CKINT i
, t_CKCOMPLEX
* val
);
287 t_CKINT
get( const std::string
& key
, t_CKCOMPLEX
* val
);
288 t_CKINT
set( t_CKINT i
, t_CKCOMPLEX val
);
289 t_CKINT
set( const std::string
& key
, t_CKCOMPLEX val
);
290 t_CKINT
push_back( t_CKCOMPLEX val
);
292 t_CKINT
back( t_CKCOMPLEX
* val
) const;
293 void zero( t_CKUINT start
, t_CKUINT end
);
295 virtual void clear( );
296 virtual t_CKINT
size( ) { return m_vector
.size(); }
297 virtual t_CKINT
capacity( ) { return m_vector
.capacity(); }
298 virtual t_CKINT
set_size( t_CKINT size
);
299 virtual t_CKINT
set_capacity( t_CKINT capacity
);
300 virtual t_CKINT
find( const std::string
& key
);
301 virtual t_CKINT
erase( const std::string
& key
);
302 virtual t_CKINT
data_type_size( ) { return CHUCK_ARRAY16_DATASIZE
; }
305 std::vector
<t_CKCOMPLEX
> m_vector
;
306 std::map
<std::string
, t_CKCOMPLEX
> m_map
;
308 // t_CKINT m_capacity;
314 #include "util_thread.h"
315 //-----------------------------------------------------------------------------
317 // desc: base Chuck Event class
318 //-----------------------------------------------------------------------------
319 struct Chuck_Event
: Chuck_Object
324 void wait( Chuck_VM_Shred
* shred
, Chuck_VM
* vm
);
325 t_CKBOOL
remove( Chuck_VM_Shred
* shred
);
328 void queue_broadcast();
329 static t_CKUINT our_can_wait
;
331 std::queue
<Chuck_VM_Shred
*> m_queue
;
338 //-----------------------------------------------------------------------------
339 // name: Chuck_String
340 // desc: base Chuck string class
341 //-----------------------------------------------------------------------------
342 struct Chuck_String
: Chuck_Object
345 Chuck_String( const std::string
& s
= "" ) { str
= s
; }
355 //-----------------------------------------------------------------------------
357 // desc: base Chuck IO class
358 //-----------------------------------------------------------------------------
359 struct Chuck_IO
: Chuck_Object
366 virtual t_CKBOOL
more() = 0;
367 virtual t_CKBOOL
eof() = 0;
368 virtual t_CKBOOL
good2read() = 0;
369 virtual t_CKBOOL
good2write() = 0;
372 virtual t_CKINT
readInt() = 0;
373 virtual t_CKFLOAT
readFloat() = 0;
374 virtual std::string
readString() = 0;
375 virtual std::string
readLine() = 0;
376 virtual t_CKINT
writeInt() = 0;
377 virtual t_CKFLOAT
writeFloat() = 0;
378 virtual std::string
writeString() = 0;
379 virtual std::string
writeLine() = 0;
382 virtual t_CKINT
read32i() = 0;
383 virtual t_CKINT
read24i() = 0;
384 virtual t_CKINT
read16i() = 0;
385 virtual t_CKINT
read8i() = 0;
386 virtual t_CKSINGLE
read32f() = 0;
387 virtual t_CKDOUBLE
read64f() = 0;
390 virtual t_CKBOOL
close() = 0;
394 static const t_CKINT READ
;
395 static const t_CKINT WRITE
;
396 static const t_CKINT APPEND
;
397 static const t_CKINT TRUNCATE
;
398 static const t_CKINT ASCII
;
399 static const t_CKINT BINARY
;
403 static Chuck_IO_File
* openFile( const std::string
& path
, t_CKINT flags
);
406 static t_CKBOOL
isFile( const std::string
& path
);
407 static t_CKBOOL
isDir( const std::string
& path
);
408 static t_CKINT
getSize( const std::string
& path
);
409 static std::string
currentDir();
410 static std::string
changeDir( const std::string
& to
);
411 static void getContent( std::vector
<std::string
> & content
);
412 static std::string
baseName( const std::string
& path
);
414 // can't instantiate one of these
422 //-----------------------------------------------------------------------------
423 // name: Chuck_IO_File
424 // desc: Chuck File IO class
425 //-----------------------------------------------------------------------------
426 struct Chuck_IO_File
: Chuck_IO
430 virtual ~Chuck_IO_File();
434 t_CKBOOL
open( const std::string
& path
, t_CKINT flags
);
438 virtual t_CKBOOL
more();
439 virtual t_CKBOOL
eof();
440 virtual t_CKBOOL
good2read();
441 virtual t_CKBOOL
good2write();
444 virtual t_CKINT
readInt();
445 virtual t_CKFLOAT
readFloat();
446 virtual std::string
readString();
447 virtual std::string
readLine();
448 virtual t_CKBOOL
writeInt( t_CKINT val
);
449 virtual t_CKBOOL
writeFloat( t_CKFLOAT val
);
450 virtual t_CKBOOL
writeString( const std::string
& val
);
451 virtual t_CKBOOL
writeLine( const std::string
& val
);
454 virtual t_CKINT
read32i();
455 virtual t_CKINT
read24i();
456 virtual t_CKINT
read16i();
457 virtual t_CKINT
read8i();
458 virtual t_CKSINGLE
read32f();
459 virtual t_CKDOUBLE
read64f();
465 t_CKINT m_ready_flags
;