*** empty log message ***
[chuck-blob.git] / v2 / chuck_oo.h
blobd2fd8df9600c1a4f942a86651552ee23250bc5f2
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
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_oo.h
27 // desc: ...
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_OO_H__
34 #define __CHUCK_OO_H__
36 #include "chuck_def.h"
37 #include <string>
38 #include <vector>
39 #include <map>
40 #include <queue>
41 #include <fstream>
44 // forward reference
45 struct Chuck_Type;
46 struct Chuck_Value;
47 struct Chuck_Func;
48 struct Chuck_Namespace;
49 struct Chuck_Context;
50 struct Chuck_Env;
51 struct Chuck_VM_Code;
52 struct Chuck_VM_Shred;
53 struct Chuck_VM;
54 struct Chuck_IO_File;
59 //-----------------------------------------------------------------------------
60 // name: struct Chuck_VM_Object
61 // desc: base vm object
62 //-----------------------------------------------------------------------------
63 struct Chuck_VM_Object
65 public:
66 Chuck_VM_Object() { this->init_ref(); }
67 virtual ~Chuck_VM_Object() { }
69 public:
70 // add reference
71 void add_ref();
72 // release reference
73 void release();
74 // lock
75 void lock();
77 public:
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;
83 public:
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
88 public:
89 // where
90 std::vector<Chuck_VM_Object *> * m_v_ref;
92 private:
93 void init_ref();
99 //-----------------------------------------------------------------------------
100 // name: struct Chuck_VM_Alloc
101 // desc: vm object manager
102 //-----------------------------------------------------------------------------
103 struct Chuck_VM_Alloc
105 public:
106 static Chuck_VM_Alloc * instance();
108 public:
109 void add_object( Chuck_VM_Object * obj );
110 void free_object( Chuck_VM_Object * obj );
112 protected:
113 static Chuck_VM_Alloc * our_instance;
115 protected:
116 Chuck_VM_Alloc();
117 ~Chuck_VM_Alloc();
119 protected: // data
120 std::map<Chuck_VM_Object *, void *> m_objects;
126 //-----------------------------------------------------------------------------
127 // name: struct Chuck_VTable
128 // desc: virtual table
129 //-----------------------------------------------------------------------------
130 struct Chuck_VTable
132 public:
133 std::vector<Chuck_Func *> funcs;
139 //-----------------------------------------------------------------------------
140 // name: struct Chuck_Object
141 // dsec: base object
142 //-----------------------------------------------------------------------------
143 struct Chuck_Object : Chuck_VM_Object
145 public:
146 Chuck_Object();
147 virtual ~Chuck_Object();
149 public:
150 // virtual table
151 Chuck_VTable * vtable;
152 // reference to type
153 Chuck_Type * type_ref;
154 // the size of the data region
155 t_CKUINT size;
156 // data for the object
157 t_CKBYTE * data;
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...
174 public:
175 // Chuck_Array();
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
197 public:
198 Chuck_Array4( t_CKBOOL is_obj, t_CKINT capacity = 8 );
199 virtual ~Chuck_Array4();
201 public:
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 );
209 t_CKINT pop_back( );
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; }
222 public:
223 std::vector<t_CKUINT> m_vector;
224 std::map<std::string, t_CKUINT> m_map;
225 t_CKBOOL m_is_obj;
226 // t_CKINT m_size;
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
239 public:
240 Chuck_Array8( t_CKINT capacity = 8 );
241 virtual ~Chuck_Array8();
243 public:
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 );
251 t_CKINT pop_back( );
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; }
264 public:
265 std::vector<t_CKFLOAT> m_vector;
266 std::map<std::string, t_CKFLOAT> m_map;
267 // t_CKINT m_size;
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
279 public:
280 Chuck_Array16( t_CKINT capacity = 8 );
281 virtual ~Chuck_Array16();
283 public:
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 );
291 t_CKINT pop_back( );
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; }
304 public:
305 std::vector<t_CKCOMPLEX> m_vector;
306 std::map<std::string, t_CKCOMPLEX> m_map;
307 // t_CKINT m_size;
308 // t_CKINT m_capacity;
314 #include "util_thread.h"
315 //-----------------------------------------------------------------------------
316 // name: Chuck_Event
317 // desc: base Chuck Event class
318 //-----------------------------------------------------------------------------
319 struct Chuck_Event : Chuck_Object
321 public:
322 void signal();
323 void broadcast();
324 void wait( Chuck_VM_Shred * shred, Chuck_VM * vm );
325 t_CKBOOL remove( Chuck_VM_Shred * shred );
327 public: // internal
328 void queue_broadcast();
329 static t_CKUINT our_can_wait;
331 std::queue<Chuck_VM_Shred *> m_queue;
332 XMutex m_queue_lock;
338 //-----------------------------------------------------------------------------
339 // name: Chuck_String
340 // desc: base Chuck string class
341 //-----------------------------------------------------------------------------
342 struct Chuck_String : Chuck_Object
344 public:
345 Chuck_String( const std::string & s = "" ) { str = s; }
346 ~Chuck_String() { }
348 public:
349 std::string str;
355 //-----------------------------------------------------------------------------
356 // name: Chuck_IO
357 // desc: base Chuck IO class
358 //-----------------------------------------------------------------------------
359 struct Chuck_IO : Chuck_Object
361 public:
362 virtual ~Chuck_IO();
364 public:
365 // query
366 virtual t_CKBOOL more() = 0;
367 virtual t_CKBOOL eof() = 0;
368 virtual t_CKBOOL good2read() = 0;
369 virtual t_CKBOOL good2write() = 0;
371 // ascii
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;
381 // binary
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;
389 // done
390 virtual t_CKBOOL close() = 0;
392 // constants
393 public:
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;
401 // static utilities
402 public:
403 static Chuck_IO_File * openFile( const std::string & path, t_CKINT flags );
405 // more stuff
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
415 protected:
416 Chuck_IO();
422 //-----------------------------------------------------------------------------
423 // name: Chuck_IO_File
424 // desc: Chuck File IO class
425 //-----------------------------------------------------------------------------
426 struct Chuck_IO_File : Chuck_IO
428 public:
429 Chuck_IO_File();
430 virtual ~Chuck_IO_File();
432 public:
433 // open
434 t_CKBOOL open( const std::string & path, t_CKINT flags );
435 t_CKBOOL close();
437 // query
438 virtual t_CKBOOL more();
439 virtual t_CKBOOL eof();
440 virtual t_CKBOOL good2read();
441 virtual t_CKBOOL good2write();
443 // ascii
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 );
453 // binary
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();
461 protected:
462 // open floags
463 t_CKINT m_flags;
464 // ready flags
465 t_CKINT m_ready_flags;
466 // in file
467 std::fstream m_io;
468 // path
469 std::string m_path;
475 #endif