| 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_globals.cpp |
| 28 | // desc: system to support in-language globals; added 1.4.1.0 |
| 29 | // |
| 30 | // author: Jack Atherton ([email protected]) |
| 31 | // Ge Wang ([email protected]) |
| 32 | // date: Spring 2021 |
| 33 | //----------------------------------------------------------------------------- |
| 34 | #include "chuck_globals.h" |
| 35 | #include "chuck_vm.h" |
| 36 | #include "chuck_instr.h" |
| 37 | using namespace std; |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | // name: struct Chuck_Set_Global_Int_Request |
| 44 | // desc: container for messages to set global ints (REFACTOR-2017) |
| 45 | //----------------------------------------------------------------------------- |
| 46 | struct Chuck_Set_Global_Int_Request |
| 47 | { |
| 48 | std::string name; |
| 49 | t_CKINT val; |
| 50 | // constructor |
| 51 | Chuck_Set_Global_Int_Request() : val(0) { } |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | //----------------------------------------------------------------------------- |
| 58 | // name: struct Chuck_Get_Global_Int_Request |
| 59 | // desc: container for messages to get global ints (REFACTOR-2017) |
| 60 | //----------------------------------------------------------------------------- |
| 61 | struct Chuck_Get_Global_Int_Request |
| 62 | { |
| 63 | std::string name; |
| 64 | union { |
| 65 | void (* cb_with_name)(const char *, t_CKINT); |
| 66 | void (* cb_with_id)(t_CKINT, t_CKINT); |
| 67 | void (* cb)(t_CKINT); |
| 68 | }; |
| 69 | Chuck_Global_Get_Callback_Type cb_type; |
| 70 | t_CKINT id; |
| 71 | // constructor |
| 72 | Chuck_Get_Global_Int_Request() : cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | //----------------------------------------------------------------------------- |
| 79 | // name: struct Chuck_Set_Global_Float_Request |
| 80 | // desc: container for messages to set global floats (REFACTOR-2017) |
| 81 | //----------------------------------------------------------------------------- |
| 82 | struct Chuck_Set_Global_Float_Request |
| 83 | { |
| 84 | std::string name; |
| 85 | t_CKFLOAT val; |
| 86 | // constructor |
| 87 | Chuck_Set_Global_Float_Request() : val(0) { } |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | //----------------------------------------------------------------------------- |
| 94 | // name: struct Chuck_Get_Global_Float_Request |
| 95 | // desc: container for messages to get global floats (REFACTOR-2017) |
| 96 | //----------------------------------------------------------------------------- |
| 97 | struct Chuck_Get_Global_Float_Request |
| 98 | { |
| 99 | std::string name; |
| 100 | union { |
| 101 | void (* cb_with_name)(const char *, t_CKFLOAT); |
| 102 | void (* cb_with_id)(t_CKINT, t_CKFLOAT); |
| 103 | void (* cb)(t_CKFLOAT); |
| 104 | }; |
| 105 | Chuck_Global_Get_Callback_Type cb_type; |
| 106 | t_CKINT id; |
| 107 | // constructor |
| 108 | Chuck_Get_Global_Float_Request() : cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | //----------------------------------------------------------------------------- |
| 115 | // name: struct Chuck_Signal_Global_Event_Request |
| 116 | // desc: container for messages to signal global events (REFACTOR-2017) |
| 117 | //----------------------------------------------------------------------------- |
| 118 | struct Chuck_Signal_Global_Event_Request |
| 119 | { |
| 120 | std::string name; |
| 121 | t_CKBOOL is_broadcast; |
| 122 | // constructor |
| 123 | Chuck_Signal_Global_Event_Request() : is_broadcast(TRUE) { } |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | //----------------------------------------------------------------------------- |
| 130 | // name: struct Chuck_Listen_For_Global_Event_Request |
| 131 | // desc: container for messages to wait on global events (REFACTOR-2017) |
| 132 | //----------------------------------------------------------------------------- |
| 133 | struct Chuck_Listen_For_Global_Event_Request |
| 134 | { |
| 135 | std::string name; |
| 136 | t_CKBOOL listen_forever; |
| 137 | t_CKBOOL deregister; |
| 138 | union { |
| 139 | void (* cb_with_name)(const char *); |
| 140 | void (* cb_with_id)(t_CKINT); |
| 141 | void (* cb)(void); |
| 142 | }; |
| 143 | Chuck_Global_Get_Callback_Type cb_type; |
| 144 | t_CKINT id; |
| 145 | // constructor |
| 146 | Chuck_Listen_For_Global_Event_Request() : listen_forever(FALSE), |
| 147 | deregister(FALSE), cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | //----------------------------------------------------------------------------- |
| 154 | // name: struct Chuck_Set_Global_String_Request |
| 155 | // desc: container for messages to set global strings (REFACTOR-2017) |
| 156 | //----------------------------------------------------------------------------- |
| 157 | struct Chuck_Set_Global_String_Request |
| 158 | { |
| 159 | std::string name; |
| 160 | std::string val; |
| 161 | // constructor |
| 162 | Chuck_Set_Global_String_Request() { } |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | //----------------------------------------------------------------------------- |
| 169 | // name: struct Chuck_Get_Global_String_Request |
| 170 | // desc: container for messages to get global strings (REFACTOR-2017) |
| 171 | //----------------------------------------------------------------------------- |
| 172 | struct Chuck_Get_Global_String_Request |
| 173 | { |
| 174 | std::string name; |
| 175 | union { |
| 176 | void (* cb_with_name)(const char *, const char *); |
| 177 | void (* cb_with_id)(t_CKINT, const char *); |
| 178 | void (* cb)(const char *); |
| 179 | }; |
| 180 | Chuck_Global_Get_Callback_Type cb_type; |
| 181 | t_CKINT id; |
| 182 | // constructor |
| 183 | Chuck_Get_Global_String_Request() : cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | //----------------------------------------------------------------------------- |
| 190 | // name: struct Chuck_Set_Global_Int_Array_Request |
| 191 | // desc: container for messages to set global int arrays (REFACTOR-2017) |
| 192 | //----------------------------------------------------------------------------- |
| 193 | struct Chuck_Set_Global_Int_Array_Request |
| 194 | { |
| 195 | std::string name; |
| 196 | std::vector< t_CKINT > arrayValues; |
| 197 | // constructor |
| 198 | Chuck_Set_Global_Int_Array_Request() { } |
| 199 | }; |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | //----------------------------------------------------------------------------- |
| 205 | // name: struct Chuck_Get_Global_Int_Array_Request |
| 206 | // desc: container for messages to get global int arrays (REFACTOR-2017) |
| 207 | //----------------------------------------------------------------------------- |
| 208 | struct Chuck_Get_Global_Int_Array_Request |
| 209 | { |
| 210 | std::string name; |
| 211 | union { |
| 212 | void (* cb_with_name)(const char *, t_CKINT[], t_CKUINT); |
| 213 | void (* cb_with_id)(t_CKINT, t_CKINT[], t_CKUINT); |
| 214 | void (* cb)(t_CKINT[], t_CKUINT); |
| 215 | }; |
| 216 | Chuck_Global_Get_Callback_Type cb_type; |
| 217 | t_CKINT id; |
| 218 | // constructor |
| 219 | Chuck_Get_Global_Int_Array_Request() : cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | //----------------------------------------------------------------------------- |
| 226 | // name: struct Chuck_Set_Global_Int_Array_Value_Request |
| 227 | // desc: container for messages to set individual elements of int arrays (REFACTOR-2017) |
| 228 | //----------------------------------------------------------------------------- |
| 229 | struct Chuck_Set_Global_Int_Array_Value_Request |
| 230 | { |
| 231 | std::string name; |
| 232 | t_CKUINT index; |
| 233 | t_CKINT value; |
| 234 | // constructor |
| 235 | Chuck_Set_Global_Int_Array_Value_Request() : index(-1), value(0) { } |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | //----------------------------------------------------------------------------- |
| 242 | // name: struct Chuck_Get_Global_Int_Array_Value_Request |
| 243 | // desc: container for messages to get individual elements of int arrays (REFACTOR-2017) |
| 244 | //----------------------------------------------------------------------------- |
| 245 | struct Chuck_Get_Global_Int_Array_Value_Request |
| 246 | { |
| 247 | std::string name; |
| 248 | t_CKUINT index; |
| 249 | union { |
| 250 | void (* cb_with_name)(const char *, t_CKINT); |
| 251 | void (* cb_with_id)(t_CKINT, t_CKINT); |
| 252 | void (* cb)(t_CKINT); |
| 253 | }; |
| 254 | Chuck_Global_Get_Callback_Type cb_type; |
| 255 | t_CKINT id; |
| 256 | // constructor |
| 257 | Chuck_Get_Global_Int_Array_Value_Request() : index(-1), cb(NULL), |
| 258 | cb_type(ck_get_plain), id(0) { } |
| 259 | }; |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | //----------------------------------------------------------------------------- |
| 265 | // name: struct Chuck_Set_Global_Associative_Int_Array_Value_Request |
| 266 | // desc: container for messages to set elements of associative int arrays (REFACTOR-2017) |
| 267 | //----------------------------------------------------------------------------- |
| 268 | struct Chuck_Set_Global_Associative_Int_Array_Value_Request |
| 269 | { |
| 270 | std::string name; |
| 271 | std::string key; |
| 272 | t_CKINT value; |
| 273 | // constructor |
| 274 | Chuck_Set_Global_Associative_Int_Array_Value_Request() : value(0) { } |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | //----------------------------------------------------------------------------- |
| 281 | // name: struct Chuck_Get_Global_Associative_Int_Array_Value_Request |
| 282 | // desc: container for messages to get elements of associative int arrays (REFACTOR-2017) |
| 283 | //----------------------------------------------------------------------------- |
| 284 | struct Chuck_Get_Global_Associative_Int_Array_Value_Request |
| 285 | { |
| 286 | std::string name; |
| 287 | std::string key; |
| 288 | union { |
| 289 | void (* cb_with_name)(const char *, t_CKINT); |
| 290 | void (* cb_with_id)(t_CKINT, t_CKINT); |
| 291 | void (* cb)(t_CKINT); |
| 292 | }; |
| 293 | Chuck_Global_Get_Callback_Type cb_type; |
| 294 | t_CKINT id; |
| 295 | // constructor |
| 296 | Chuck_Get_Global_Associative_Int_Array_Value_Request() : cb(NULL), |
| 297 | cb_type(ck_get_plain), id(0) { } |
| 298 | }; |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | //----------------------------------------------------------------------------- |
| 304 | // name: struct Chuck_Set_Global_Float_Array_Request |
| 305 | // desc: container for messages to set global float arrays (REFACTOR-2017) |
| 306 | //----------------------------------------------------------------------------- |
| 307 | struct Chuck_Set_Global_Float_Array_Request |
| 308 | { |
| 309 | std::string name; |
| 310 | std::vector< t_CKFLOAT > arrayValues; |
| 311 | // constructor |
| 312 | Chuck_Set_Global_Float_Array_Request() { } |
| 313 | }; |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | //----------------------------------------------------------------------------- |
| 319 | // name: struct Chuck_Get_Global_Float_Array_Request |
| 320 | // desc: container for messages to get global float arrays (REFACTOR-2017) |
| 321 | //----------------------------------------------------------------------------- |
| 322 | struct Chuck_Get_Global_Float_Array_Request |
| 323 | { |
| 324 | std::string name; |
| 325 | union { |
| 326 | void (* cb_with_name)(const char *, t_CKFLOAT[], t_CKUINT); |
| 327 | void (* cb_with_id)(t_CKINT, t_CKFLOAT[], t_CKUINT); |
| 328 | void (* cb)(t_CKFLOAT[], t_CKUINT); |
| 329 | }; |
| 330 | Chuck_Global_Get_Callback_Type cb_type; |
| 331 | t_CKINT id; |
| 332 | // constructor |
| 333 | Chuck_Get_Global_Float_Array_Request() : cb(NULL), cb_type(ck_get_plain), id(0) { } |
| 334 | }; |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | //----------------------------------------------------------------------------- |
| 340 | // name: struct Chuck_Set_Global_Float_Array_Value_Request |
| 341 | // desc: container for messages to set individual elements of float arrays (REFACTOR-2017) |
| 342 | //----------------------------------------------------------------------------- |
| 343 | struct Chuck_Set_Global_Float_Array_Value_Request |
| 344 | { |
| 345 | std::string name; |
| 346 | t_CKUINT index; |
| 347 | t_CKFLOAT value; |
| 348 | // constructor |
| 349 | Chuck_Set_Global_Float_Array_Value_Request() : index(-1), value(0) { } |
| 350 | }; |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | //----------------------------------------------------------------------------- |
| 356 | // name: struct Chuck_Get_Global_Float_Array_Value_Request |
| 357 | // desc: container for messages to get individual elements of float arrays (REFACTOR-2017) |
| 358 | //----------------------------------------------------------------------------- |
| 359 | struct Chuck_Get_Global_Float_Array_Value_Request |
| 360 | { |
| 361 | std::string name; |
| 362 | t_CKUINT index; |
| 363 | union { |
| 364 | void (* cb_with_name)(const char *, t_CKFLOAT); |
| 365 | void (* cb_with_id)(t_CKINT, t_CKFLOAT); |
| 366 | void (* cb)(t_CKFLOAT); |
| 367 | }; |
| 368 | Chuck_Global_Get_Callback_Type cb_type; |
| 369 | t_CKINT id; |
| 370 | // constructor |
| 371 | Chuck_Get_Global_Float_Array_Value_Request() : index(-1), cb(NULL), |
| 372 | cb_type(ck_get_plain), id(0) { } |
| 373 | }; |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | //----------------------------------------------------------------------------- |
| 379 | // name: struct Chuck_Set_Global_Associative_Float_Array_Value_Request |
| 380 | // desc: container for messages to set elements of associative float arrays (REFACTOR-2017) |
| 381 | //----------------------------------------------------------------------------- |
| 382 | struct Chuck_Set_Global_Associative_Float_Array_Value_Request |
| 383 | { |
| 384 | std::string name; |
| 385 | std::string key; |
| 386 | t_CKFLOAT value; |
| 387 | // constructor |
| 388 | Chuck_Set_Global_Associative_Float_Array_Value_Request() : value(0) { } |
| 389 | }; |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | //----------------------------------------------------------------------------- |
| 395 | // name: struct Chuck_Get_Global_Associative_Float_Array_Value_Request |
| 396 | // desc: container for messages to get elements of associative float arrays (REFACTOR-2017) |
| 397 | //----------------------------------------------------------------------------- |
| 398 | struct Chuck_Get_Global_Associative_Float_Array_Value_Request |
| 399 | { |
| 400 | std::string name; |
| 401 | std::string key; |
| 402 | union { |
| 403 | void (* cb_with_name)(const char *, t_CKFLOAT); |
| 404 | void (* cb_with_id)(t_CKINT, t_CKFLOAT); |
| 405 | void (* cb)(t_CKFLOAT); |
| 406 | }; |
| 407 | Chuck_Global_Get_Callback_Type cb_type; |
| 408 | t_CKINT id; |
| 409 | // constructor |
| 410 | Chuck_Get_Global_Associative_Float_Array_Value_Request() : cb(NULL), |
| 411 | cb_type(ck_get_plain), id(0) { } |
| 412 | }; |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | //----------------------------------------------------------------------------- |
| 418 | // name: struct Chuck_Execute_Chuck_Msg_Request |
| 419 | // desc: container for Chuck_Msg (but to be called from the globals callback |
| 420 | // rather than the Chuck_Msg callback) |
| 421 | //----------------------------------------------------------------------------- |
| 422 | struct Chuck_Execute_Chuck_Msg_Request |
| 423 | { |
| 424 | Chuck_Msg * msg; |
| 425 | // constructor |
| 426 | Chuck_Execute_Chuck_Msg_Request() : msg(NULL) { } |
| 427 | }; |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | //----------------------------------------------------------------------------- |
| 433 | // name: struct Chuck_Get_Global_All_Request |
| 434 | // desc: container for messages to get all global variables |
| 435 | //----------------------------------------------------------------------------- |
| 436 | struct Chuck_Get_Global_All_Request |
| 437 | { |
| 438 | // callback |
| 439 | void (* cb)( const std::vector<Chuck_Globals_TypeValue> & list, void * data ); |
| 440 | // data |
| 441 | void * data; |
| 442 | |
| 443 | // constructor |
| 444 | Chuck_Get_Global_All_Request() : cb(NULL), data(NULL) { } |
| 445 | }; |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | //----------------------------------------------------------------------------- |
| 451 | // name: struct Chuck_Global_Int_Container |
| 452 | // desc: container for global ints |
| 453 | //----------------------------------------------------------------------------- |
| 454 | struct Chuck_Global_Int_Container |
| 455 | { |
| 456 | t_CKINT val; |
| 457 | |
| 458 | Chuck_Global_Int_Container() { val = 0; } |
| 459 | }; |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | //----------------------------------------------------------------------------- |
| 465 | // name: struct Chuck_Global_Float_Container |
| 466 | // desc: container for global floats |
| 467 | //----------------------------------------------------------------------------- |
| 468 | struct Chuck_Global_Float_Container |
| 469 | { |
| 470 | t_CKFLOAT val; |
| 471 | |
| 472 | Chuck_Global_Float_Container() { val = 0; } |
| 473 | }; |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | //----------------------------------------------------------------------------- |
| 479 | // name: struct Chuck_Global_String_Container |
| 480 | // desc: container for global ints |
| 481 | //----------------------------------------------------------------------------- |
| 482 | struct Chuck_Global_String_Container |
| 483 | { |
| 484 | Chuck_String * val; |
| 485 | |
| 486 | Chuck_Global_String_Container() { val = NULL; } |
| 487 | }; |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | //----------------------------------------------------------------------------- |
| 493 | // name: struct Chuck_Global_Event_Container |
| 494 | // desc: container for global events |
| 495 | //----------------------------------------------------------------------------- |
| 496 | struct Chuck_Global_Event_Container |
| 497 | { |
| 498 | Chuck_Event * val; |
| 499 | Chuck_Type * type; |
| 500 | t_CKBOOL ctor_needs_to_be_called; |
| 501 | |
| 502 | Chuck_Global_Event_Container() { val = NULL; type = NULL; |
| 503 | ctor_needs_to_be_called = TRUE; } |
| 504 | }; |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | //----------------------------------------------------------------------------- |
| 510 | // name: struct Chuck_Global_UGen_Container |
| 511 | // desc: container for global ugens |
| 512 | //----------------------------------------------------------------------------- |
| 513 | struct Chuck_Global_UGen_Container |
| 514 | { |
| 515 | Chuck_UGen * val; |
| 516 | Chuck_Type * type; |
| 517 | t_CKBOOL ctor_needs_to_be_called; |
| 518 | |
| 519 | Chuck_Global_UGen_Container() { val = NULL; type = NULL; |
| 520 | ctor_needs_to_be_called = TRUE; } |
| 521 | }; |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | //----------------------------------------------------------------------------- |
| 527 | // name: struct Chuck_Global_Array_Container |
| 528 | // desc: container for global arrays |
| 529 | //----------------------------------------------------------------------------- |
| 530 | struct Chuck_Global_Array_Container |
| 531 | { |
| 532 | Chuck_Object * array; |
| 533 | Chuck_Type * type; |
| 534 | te_GlobalType array_type; |
| 535 | t_CKBOOL ctor_needs_to_be_called; |
| 536 | |
| 537 | // constructor |
| 538 | Chuck_Global_Array_Container( Chuck_Type * t, te_GlobalType arr_type ) |
| 539 | { |
| 540 | array = NULL; |
| 541 | ctor_needs_to_be_called = FALSE; |
| 542 | type = t; |
| 543 | array_type = arr_type; |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | //----------------------------------------------------------------------------- |
| 551 | // name: struct Chuck_Global_Object_Container |
| 552 | // desc: container for global ChucK objects |
| 553 | //----------------------------------------------------------------------------- |
| 554 | struct Chuck_Global_Object_Container |
| 555 | { |
| 556 | Chuck_Object * val; |
| 557 | Chuck_Type * type; |
| 558 | t_CKBOOL ctor_needs_to_be_called; |
| 559 | |
| 560 | Chuck_Global_Object_Container() { val = NULL; type = NULL; |
| 561 | ctor_needs_to_be_called = TRUE; } |
| 562 | }; |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | //----------------------------------------------------------------------------- |
| 568 | // name: Chuck_Globals_Manager() |
| 569 | // desc: constructor: size queues appropriately |
| 570 | //----------------------------------------------------------------------------- |
| 571 | Chuck_Globals_Manager::Chuck_Globals_Manager( Chuck_VM * vm ) |
| 572 | { |
| 573 | // store |
| 574 | m_vm = vm; |
| 575 | |
| 576 | // REFACTOR-2017: TODO might want to dynamically grow queue? |
| 577 | m_global_request_queue.init( 16384 ); |
| 578 | m_global_request_retry_queue.init( 16384 ); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | //----------------------------------------------------------------------------- |
| 585 | // name: ~Chuck_Globals_Manager() |
| 586 | // desc: destructor: clean up |
| 587 | //----------------------------------------------------------------------------- |
| 588 | Chuck_Globals_Manager::~Chuck_Globals_Manager() |
| 589 | { |
| 590 | cleanup_global_variables(); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | //----------------------------------------------------------------------------- |
| 597 | // name: add_request |
| 598 | // desc: add a request to the queue |
| 599 | //----------------------------------------------------------------------------- |
| 600 | t_CKBOOL Chuck_Globals_Manager::add_request( Chuck_Global_Request request ) |
| 601 | { |
| 602 | m_global_request_queue.put( request ); |
| 603 | return TRUE; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | //----------------------------------------------------------------------------- |
| 610 | // name: more_requests |
| 611 | // desc: are there more requests left? |
| 612 | //----------------------------------------------------------------------------- |
| 613 | t_CKBOOL Chuck_Globals_Manager::more_requests() |
| 614 | { |
| 615 | return m_global_request_queue.more(); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | //----------------------------------------------------------------------------- |
| 622 | // name: getGlobalInt() |
| 623 | // desc: get a global int by name |
| 624 | //----------------------------------------------------------------------------- |
| 625 | t_CKBOOL Chuck_Globals_Manager::getGlobalInt( const char * name, |
| 626 | void (* callback)(t_CKINT) ) |
| 627 | { |
| 628 | Chuck_Get_Global_Int_Request * get_int_message = |
| 629 | new Chuck_Get_Global_Int_Request; |
| 630 | get_int_message->name = name; |
| 631 | get_int_message->cb = callback; |
| 632 | get_int_message->cb_type = ck_get_plain; |
| 633 | |
| 634 | Chuck_Global_Request r; |
| 635 | r.type = get_global_int_request; |
| 636 | r.getIntRequest = get_int_message; |
| 637 | |
| 638 | m_global_request_queue.put( r ); |
| 639 | |
| 640 | return TRUE; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | //----------------------------------------------------------------------------- |
| 647 | // name: getGlobalInt() |
| 648 | // desc: get a global int by name, with a by name callback |
| 649 | //----------------------------------------------------------------------------- |
| 650 | t_CKBOOL Chuck_Globals_Manager::getGlobalInt( const char * name, |
| 651 | void (* callback)(const char *, t_CKINT) ) |
| 652 | { |
| 653 | Chuck_Get_Global_Int_Request * get_int_message = |
| 654 | new Chuck_Get_Global_Int_Request; |
| 655 | get_int_message->name = name; |
| 656 | get_int_message->cb_with_name = callback; |
| 657 | get_int_message->cb_type = ck_get_name; |
| 658 | |
| 659 | Chuck_Global_Request r; |
| 660 | r.type = get_global_int_request; |
| 661 | r.getIntRequest = get_int_message; |
| 662 | |
| 663 | m_global_request_queue.put( r ); |
| 664 | |
| 665 | return TRUE; |
| 666 | } |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | //----------------------------------------------------------------------------- |
| 672 | // name: getGlobalInt() |
| 673 | // desc: get a global int by name, with id in callback |
| 674 | //----------------------------------------------------------------------------- |
| 675 | t_CKBOOL Chuck_Globals_Manager::getGlobalInt( const char * name, t_CKINT id, |
| 676 | void (* callback)(t_CKINT, t_CKINT) ) |
| 677 | { |
| 678 | Chuck_Get_Global_Int_Request * get_int_message = |
| 679 | new Chuck_Get_Global_Int_Request; |
| 680 | get_int_message->name = name; |
| 681 | get_int_message->cb_with_id = callback; |
| 682 | get_int_message->id = id; |
| 683 | get_int_message->cb_type = ck_get_id; |
| 684 | |
| 685 | Chuck_Global_Request r; |
| 686 | r.type = get_global_int_request; |
| 687 | r.getIntRequest = get_int_message; |
| 688 | |
| 689 | m_global_request_queue.put( r ); |
| 690 | |
| 691 | return TRUE; |
| 692 | } |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | //----------------------------------------------------------------------------- |
| 699 | // name: setGlobalInt() |
| 700 | // desc: set a global int by name |
| 701 | //----------------------------------------------------------------------------- |
| 702 | t_CKBOOL Chuck_Globals_Manager::setGlobalInt( const char * name, t_CKINT val ) |
| 703 | { |
| 704 | Chuck_Set_Global_Int_Request * set_int_message = |
| 705 | new Chuck_Set_Global_Int_Request; |
| 706 | set_int_message->name = name; |
| 707 | set_int_message->val = val; |
| 708 | |
| 709 | Chuck_Global_Request r; |
| 710 | r.type = set_global_int_request; |
| 711 | r.setIntRequest = set_int_message; |
| 712 | |
| 713 | m_global_request_queue.put( r ); |
| 714 | |
| 715 | return TRUE; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | //----------------------------------------------------------------------------- |
| 722 | // name: init_global_int() |
| 723 | // desc: tell the vm that a global int is now available |
| 724 | //----------------------------------------------------------------------------- |
| 725 | t_CKBOOL Chuck_Globals_Manager::init_global_int( const std::string & name ) |
| 726 | { |
| 727 | if( m_global_ints.count( name ) == 0 ) |
| 728 | { |
| 729 | m_global_ints[name] = new Chuck_Global_Int_Container; |
| 730 | } |
| 731 | |
| 732 | return TRUE; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | //----------------------------------------------------------------------------- |
| 739 | // name: get_global_int_value() |
| 740 | // desc: get a value directly from the vm (internal) |
| 741 | //----------------------------------------------------------------------------- |
| 742 | t_CKINT Chuck_Globals_Manager::get_global_int_value( const std::string & name ) |
| 743 | { |
| 744 | // ensure exists |
| 745 | init_global_int( name ); |
| 746 | |
| 747 | return m_global_ints[name]->val; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | //----------------------------------------------------------------------------- |
| 754 | // name: get_ptr_to_global_int() |
| 755 | // desc: get a pointer directly from the vm (internal) |
| 756 | //----------------------------------------------------------------------------- |
| 757 | t_CKINT * Chuck_Globals_Manager::get_ptr_to_global_int( const std::string & name ) |
| 758 | { |
| 759 | return &( m_global_ints[name]->val ); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | //----------------------------------------------------------------------------- |
| 766 | // name: getGlobalFloat() |
| 767 | // desc: get a global float by name |
| 768 | //----------------------------------------------------------------------------- |
| 769 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloat( const char * name, |
| 770 | void (* callback)(t_CKFLOAT) ) |
| 771 | { |
| 772 | Chuck_Get_Global_Float_Request * get_float_message = |
| 773 | new Chuck_Get_Global_Float_Request; |
| 774 | get_float_message->name = name; |
| 775 | get_float_message->cb = callback; |
| 776 | get_float_message->cb_type = ck_get_plain; |
| 777 | |
| 778 | Chuck_Global_Request r; |
| 779 | r.type = get_global_float_request; |
| 780 | r.getFloatRequest = get_float_message; |
| 781 | |
| 782 | m_global_request_queue.put( r ); |
| 783 | |
| 784 | return TRUE; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | //----------------------------------------------------------------------------- |
| 791 | // name: getGlobalFloat() |
| 792 | // desc: get a global float by name, with a named callback |
| 793 | //----------------------------------------------------------------------------- |
| 794 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloat( const char * name, |
| 795 | void (* callback)(const char *, t_CKFLOAT) ) |
| 796 | { |
| 797 | Chuck_Get_Global_Float_Request * get_float_message = |
| 798 | new Chuck_Get_Global_Float_Request; |
| 799 | get_float_message->name = name; |
| 800 | get_float_message->cb_with_name = callback; |
| 801 | get_float_message->cb_type = ck_get_name; |
| 802 | |
| 803 | Chuck_Global_Request r; |
| 804 | r.type = get_global_float_request; |
| 805 | r.getFloatRequest = get_float_message; |
| 806 | |
| 807 | m_global_request_queue.put( r ); |
| 808 | |
| 809 | return TRUE; |
| 810 | } |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | //----------------------------------------------------------------------------- |
| 816 | // name: getGlobalFloat() |
| 817 | // desc: get a global float by name, with id in callback |
| 818 | //----------------------------------------------------------------------------- |
| 819 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloat( const char * name, t_CKINT id, |
| 820 | void (* callback)(t_CKINT, t_CKFLOAT) ) |
| 821 | { |
| 822 | Chuck_Get_Global_Float_Request * get_float_message = |
| 823 | new Chuck_Get_Global_Float_Request; |
| 824 | get_float_message->name = name; |
| 825 | get_float_message->cb_with_id = callback; |
| 826 | get_float_message->id = id; |
| 827 | get_float_message->cb_type = ck_get_id; |
| 828 | |
| 829 | Chuck_Global_Request r; |
| 830 | r.type = get_global_float_request; |
| 831 | r.getFloatRequest = get_float_message; |
| 832 | |
| 833 | m_global_request_queue.put( r ); |
| 834 | |
| 835 | return TRUE; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | //----------------------------------------------------------------------------- |
| 842 | // name: setGlobalFloat() |
| 843 | // desc: set a global float by name |
| 844 | //----------------------------------------------------------------------------- |
| 845 | t_CKBOOL Chuck_Globals_Manager::setGlobalFloat( const char * name, |
| 846 | t_CKFLOAT val ) |
| 847 | { |
| 848 | Chuck_Set_Global_Float_Request * set_float_message = |
| 849 | new Chuck_Set_Global_Float_Request; |
| 850 | set_float_message->name = name; |
| 851 | set_float_message->val = val; |
| 852 | |
| 853 | Chuck_Global_Request r; |
| 854 | r.type = set_global_float_request; |
| 855 | r.setFloatRequest = set_float_message; |
| 856 | |
| 857 | m_global_request_queue.put( r ); |
| 858 | |
| 859 | return TRUE; |
| 860 | } |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | //----------------------------------------------------------------------------- |
| 866 | // name: init_global_float() |
| 867 | // desc: tell the vm that a global float is now available |
| 868 | //----------------------------------------------------------------------------- |
| 869 | t_CKBOOL Chuck_Globals_Manager::init_global_float( const std::string & name ) |
| 870 | { |
| 871 | if( m_global_floats.count( name ) == 0 ) |
| 872 | { |
| 873 | m_global_floats[name] = new Chuck_Global_Float_Container; |
| 874 | } |
| 875 | |
| 876 | return TRUE; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | //----------------------------------------------------------------------------- |
| 883 | // name: get_global_float_value() |
| 884 | // desc: get a value directly from the vm (internal) |
| 885 | //----------------------------------------------------------------------------- |
| 886 | t_CKFLOAT Chuck_Globals_Manager::get_global_float_value( const std::string & name ) |
| 887 | { |
| 888 | // ensure exists |
| 889 | init_global_float( name ); |
| 890 | |
| 891 | return m_global_floats[name]->val; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | //----------------------------------------------------------------------------- |
| 898 | // name: get_ptr_to_global_float() |
| 899 | // desc: get a pointer directly to the vm (internal) |
| 900 | //----------------------------------------------------------------------------- |
| 901 | t_CKFLOAT * Chuck_Globals_Manager::get_ptr_to_global_float( const std::string & name ) |
| 902 | { |
| 903 | return &( m_global_floats[name]->val ); |
| 904 | } |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | //----------------------------------------------------------------------------- |
| 910 | // name: getGlobalString() |
| 911 | // desc: get a global string by name |
| 912 | //----------------------------------------------------------------------------- |
| 913 | t_CKBOOL Chuck_Globals_Manager::getGlobalString( const char * name, |
| 914 | void (* callback)(const char *) ) |
| 915 | { |
| 916 | Chuck_Get_Global_String_Request * get_string_message = |
| 917 | new Chuck_Get_Global_String_Request; |
| 918 | get_string_message->name = name; |
| 919 | get_string_message->cb = callback; |
| 920 | get_string_message->cb_type = ck_get_plain; |
| 921 | |
| 922 | Chuck_Global_Request r; |
| 923 | r.type = get_global_string_request; |
| 924 | r.getStringRequest = get_string_message; |
| 925 | |
| 926 | m_global_request_queue.put( r ); |
| 927 | |
| 928 | return TRUE; |
| 929 | } |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | //----------------------------------------------------------------------------- |
| 935 | // name: getGlobalString() |
| 936 | // desc: get a global string by name, with a named callback |
| 937 | //----------------------------------------------------------------------------- |
| 938 | t_CKBOOL Chuck_Globals_Manager::getGlobalString( const char * name, |
| 939 | void (* callback)(const char *, const char *) ) |
| 940 | { |
| 941 | Chuck_Get_Global_String_Request * get_string_message = |
| 942 | new Chuck_Get_Global_String_Request; |
| 943 | get_string_message->name = name; |
| 944 | get_string_message->cb_with_name = callback; |
| 945 | get_string_message->cb_type = ck_get_name; |
| 946 | |
| 947 | Chuck_Global_Request r; |
| 948 | r.type = get_global_string_request; |
| 949 | r.getStringRequest = get_string_message; |
| 950 | |
| 951 | m_global_request_queue.put( r ); |
| 952 | |
| 953 | return TRUE; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | //----------------------------------------------------------------------------- |
| 960 | // name: getGlobalString() |
| 961 | // desc: get a global string by name, with id in callback |
| 962 | //----------------------------------------------------------------------------- |
| 963 | t_CKBOOL Chuck_Globals_Manager::getGlobalString( const char * name, t_CKINT id, |
| 964 | void (* callback)(t_CKINT, const char *) ) |
| 965 | { |
| 966 | Chuck_Get_Global_String_Request * get_string_message = |
| 967 | new Chuck_Get_Global_String_Request; |
| 968 | get_string_message->name = name; |
| 969 | get_string_message->cb_with_id = callback; |
| 970 | get_string_message->id = id; |
| 971 | get_string_message->cb_type = ck_get_id; |
| 972 | |
| 973 | Chuck_Global_Request r; |
| 974 | r.type = get_global_string_request; |
| 975 | r.getStringRequest = get_string_message; |
| 976 | |
| 977 | m_global_request_queue.put( r ); |
| 978 | |
| 979 | return TRUE; |
| 980 | } |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | //----------------------------------------------------------------------------- |
| 986 | // name: setGlobalString() |
| 987 | // desc: set a global string by name |
| 988 | //----------------------------------------------------------------------------- |
| 989 | t_CKBOOL Chuck_Globals_Manager::setGlobalString( const char * name, |
| 990 | const char * val ) |
| 991 | { |
| 992 | Chuck_Set_Global_String_Request * set_string_message = |
| 993 | new Chuck_Set_Global_String_Request; |
| 994 | set_string_message->name = name; |
| 995 | set_string_message->val = val; |
| 996 | |
| 997 | Chuck_Global_Request r; |
| 998 | r.type = set_global_string_request; |
| 999 | r.setStringRequest = set_string_message; |
| 1000 | |
| 1001 | m_global_request_queue.put( r ); |
| 1002 | |
| 1003 | return TRUE; |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | //----------------------------------------------------------------------------- |
| 1010 | // name: init_global_string() |
| 1011 | // desc: tell the vm that a global string is now available |
| 1012 | //----------------------------------------------------------------------------- |
| 1013 | t_CKBOOL Chuck_Globals_Manager::init_global_string( const std::string & name ) |
| 1014 | { |
| 1015 | if( m_global_strings.count( name ) == 0 ) |
| 1016 | { |
| 1017 | // make container |
| 1018 | m_global_strings[name] = new Chuck_Global_String_Container; |
| 1019 | // init |
| 1020 | m_global_strings[name]->val = (Chuck_String *) |
| 1021 | instantiate_and_initialize_object( m_vm->env()->ckt_string, m_vm ); |
| 1022 | |
| 1023 | // add reference to prevent deletion |
| 1024 | m_global_strings[name]->val->add_ref(); |
| 1025 | |
| 1026 | } |
| 1027 | |
| 1028 | return TRUE; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | //----------------------------------------------------------------------------- |
| 1035 | // name: get_global_string_value() |
| 1036 | // desc: get a value directly from the vm (internal) |
| 1037 | //----------------------------------------------------------------------------- |
| 1038 | Chuck_String * Chuck_Globals_Manager::get_global_string( const std::string & name ) |
| 1039 | { |
| 1040 | // ensure exists |
| 1041 | init_global_string( name ); |
| 1042 | |
| 1043 | return m_global_strings[name]->val; |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | //----------------------------------------------------------------------------- |
| 1050 | // name: set_global_string_value() |
| 1051 | // desc: set a value directly to the vm (internal) |
| 1052 | //----------------------------------------------------------------------------- |
| 1053 | Chuck_String * * Chuck_Globals_Manager::get_ptr_to_global_string( const std::string & name ) |
| 1054 | { |
| 1055 | return &( m_global_strings[name]->val ); |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | //----------------------------------------------------------------------------- |
| 1062 | // name: signalGlobalEvent() |
| 1063 | // desc: signal() an Event by name |
| 1064 | //----------------------------------------------------------------------------- |
| 1065 | t_CKBOOL Chuck_Globals_Manager::signalGlobalEvent( const char * name ) |
| 1066 | { |
| 1067 | Chuck_Signal_Global_Event_Request * signal_event_message = |
| 1068 | new Chuck_Signal_Global_Event_Request; |
| 1069 | signal_event_message->name = name; |
| 1070 | signal_event_message->is_broadcast = FALSE; |
| 1071 | |
| 1072 | Chuck_Global_Request r; |
| 1073 | r.type = signal_global_event_request; |
| 1074 | r.signalEventRequest = signal_event_message; |
| 1075 | |
| 1076 | m_global_request_queue.put( r ); |
| 1077 | |
| 1078 | return TRUE; |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | //----------------------------------------------------------------------------- |
| 1085 | // name: broadcastGlobalEvent() |
| 1086 | // desc: broadcast() an Event by name |
| 1087 | //----------------------------------------------------------------------------- |
| 1088 | t_CKBOOL Chuck_Globals_Manager::broadcastGlobalEvent( const char * name ) |
| 1089 | { |
| 1090 | Chuck_Signal_Global_Event_Request * signal_event_message = |
| 1091 | new Chuck_Signal_Global_Event_Request; |
| 1092 | signal_event_message->name = name; |
| 1093 | signal_event_message->is_broadcast = TRUE; |
| 1094 | |
| 1095 | Chuck_Global_Request r; |
| 1096 | r.type = signal_global_event_request; |
| 1097 | r.signalEventRequest = signal_event_message; |
| 1098 | // chuck object might not be constructed on time. retry only once |
| 1099 | r.retries = 1; |
| 1100 | |
| 1101 | m_global_request_queue.put( r ); |
| 1102 | |
| 1103 | return TRUE; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | //----------------------------------------------------------------------------- |
| 1109 | // name: listenForGlobalEvent() |
| 1110 | // desc: listen for an Event by name |
| 1111 | //----------------------------------------------------------------------------- |
| 1112 | t_CKBOOL Chuck_Globals_Manager::listenForGlobalEvent( const char * name, |
| 1113 | void (* callback)(void), |
| 1114 | t_CKBOOL listen_forever ) |
| 1115 | { |
| 1116 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1117 | new Chuck_Listen_For_Global_Event_Request; |
| 1118 | listen_event_message->cb = callback; |
| 1119 | listen_event_message->cb_type = ck_get_plain; |
| 1120 | listen_event_message->name = name; |
| 1121 | listen_event_message->listen_forever = listen_forever; |
| 1122 | listen_event_message->deregister = FALSE; |
| 1123 | |
| 1124 | Chuck_Global_Request r; |
| 1125 | r.type = listen_for_global_event_request; |
| 1126 | r.listenForEventRequest = listen_event_message; |
| 1127 | // chuck object might not be constructed on time. retry only once |
| 1128 | r.retries = 1; |
| 1129 | |
| 1130 | m_global_request_queue.put( r ); |
| 1131 | |
| 1132 | return TRUE; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | //----------------------------------------------------------------------------- |
| 1139 | // name: listenForGlobalEvent() |
| 1140 | // desc: listen for an Event by name, with a named callback |
| 1141 | //----------------------------------------------------------------------------- |
| 1142 | t_CKBOOL Chuck_Globals_Manager::listenForGlobalEvent( const char * name, |
| 1143 | void (* callback)(const char *), |
| 1144 | t_CKBOOL listen_forever ) |
| 1145 | { |
| 1146 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1147 | new Chuck_Listen_For_Global_Event_Request; |
| 1148 | listen_event_message->cb_with_name = callback; |
| 1149 | listen_event_message->cb_type = ck_get_name; |
| 1150 | listen_event_message->name = name; |
| 1151 | listen_event_message->listen_forever = listen_forever; |
| 1152 | listen_event_message->deregister = FALSE; |
| 1153 | |
| 1154 | Chuck_Global_Request r; |
| 1155 | r.type = listen_for_global_event_request; |
| 1156 | r.listenForEventRequest = listen_event_message; |
| 1157 | // chuck object might not be constructed on time. retry only once |
| 1158 | r.retries = 1; |
| 1159 | |
| 1160 | m_global_request_queue.put( r ); |
| 1161 | |
| 1162 | return TRUE; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | //----------------------------------------------------------------------------- |
| 1169 | // name: listenForGlobalEvent() |
| 1170 | // desc: listen for an Event by name, with id in callback |
| 1171 | //----------------------------------------------------------------------------- |
| 1172 | t_CKBOOL Chuck_Globals_Manager::listenForGlobalEvent( const char * name, |
| 1173 | t_CKINT id, |
| 1174 | void (* callback)(t_CKINT), |
| 1175 | t_CKBOOL listen_forever ) |
| 1176 | { |
| 1177 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1178 | new Chuck_Listen_For_Global_Event_Request; |
| 1179 | listen_event_message->cb_with_id = callback; |
| 1180 | listen_event_message->id = id; |
| 1181 | listen_event_message->cb_type = ck_get_id; |
| 1182 | listen_event_message->name = name; |
| 1183 | listen_event_message->listen_forever = listen_forever; |
| 1184 | listen_event_message->deregister = FALSE; |
| 1185 | |
| 1186 | Chuck_Global_Request r; |
| 1187 | r.type = listen_for_global_event_request; |
| 1188 | r.listenForEventRequest = listen_event_message; |
| 1189 | // chuck object might not be constructed on time. retry only once |
| 1190 | r.retries = 1; |
| 1191 | |
| 1192 | m_global_request_queue.put( r ); |
| 1193 | |
| 1194 | return TRUE; |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | //----------------------------------------------------------------------------- |
| 1201 | // name: stopListeningForGlobalEvent() |
| 1202 | // desc: stop listening for an event by name |
| 1203 | //----------------------------------------------------------------------------- |
| 1204 | t_CKBOOL Chuck_Globals_Manager::stopListeningForGlobalEvent( const char * name, |
| 1205 | void (* callback)(void) ) |
| 1206 | { |
| 1207 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1208 | new Chuck_Listen_For_Global_Event_Request; |
| 1209 | listen_event_message->cb = callback; |
| 1210 | listen_event_message->cb_type = ck_get_plain; |
| 1211 | listen_event_message->name = name; |
| 1212 | listen_event_message->listen_forever = FALSE; |
| 1213 | listen_event_message->deregister = TRUE; |
| 1214 | |
| 1215 | Chuck_Global_Request r; |
| 1216 | r.type = listen_for_global_event_request; |
| 1217 | r.listenForEventRequest = listen_event_message; |
| 1218 | |
| 1219 | m_global_request_queue.put( r ); |
| 1220 | |
| 1221 | return TRUE; |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | //----------------------------------------------------------------------------- |
| 1228 | // name: stopListeningForGlobalEvent() |
| 1229 | // desc: stop listening for an Event by name, with a named callback |
| 1230 | //----------------------------------------------------------------------------- |
| 1231 | t_CKBOOL Chuck_Globals_Manager::stopListeningForGlobalEvent( const char * name, |
| 1232 | void (* callback)(const char *) ) |
| 1233 | { |
| 1234 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1235 | new Chuck_Listen_For_Global_Event_Request; |
| 1236 | listen_event_message->cb_with_name = callback; |
| 1237 | listen_event_message->cb_type = ck_get_name; |
| 1238 | listen_event_message->name = name; |
| 1239 | listen_event_message->listen_forever = FALSE; |
| 1240 | listen_event_message->deregister = TRUE; |
| 1241 | |
| 1242 | Chuck_Global_Request r; |
| 1243 | r.type = listen_for_global_event_request; |
| 1244 | r.listenForEventRequest = listen_event_message; |
| 1245 | |
| 1246 | m_global_request_queue.put( r ); |
| 1247 | |
| 1248 | return TRUE; |
| 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | //----------------------------------------------------------------------------- |
| 1255 | // name: stopListeningForGlobalEvent() |
| 1256 | // desc: stop listening for an Event by name, with id in callback |
| 1257 | //----------------------------------------------------------------------------- |
| 1258 | t_CKBOOL Chuck_Globals_Manager::stopListeningForGlobalEvent( const char * name, |
| 1259 | t_CKINT id, void (* callback)(t_CKINT) ) |
| 1260 | { |
| 1261 | Chuck_Listen_For_Global_Event_Request * listen_event_message = |
| 1262 | new Chuck_Listen_For_Global_Event_Request; |
| 1263 | listen_event_message->cb_with_id = callback; |
| 1264 | listen_event_message->id = id; |
| 1265 | listen_event_message->cb_type = ck_get_id; |
| 1266 | listen_event_message->name = name; |
| 1267 | listen_event_message->listen_forever = FALSE; |
| 1268 | listen_event_message->deregister = TRUE; |
| 1269 | |
| 1270 | Chuck_Global_Request r; |
| 1271 | r.type = listen_for_global_event_request; |
| 1272 | r.listenForEventRequest = listen_event_message; |
| 1273 | |
| 1274 | m_global_request_queue.put( r ); |
| 1275 | |
| 1276 | return TRUE; |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | //----------------------------------------------------------------------------- |
| 1283 | // name: init_global_event() |
| 1284 | // desc: tell the vm that a global event is now available |
| 1285 | //----------------------------------------------------------------------------- |
| 1286 | t_CKBOOL Chuck_Globals_Manager::init_global_event( const std::string & name, |
| 1287 | Chuck_Type * type ) |
| 1288 | { |
| 1289 | // if it hasn't been initted yet |
| 1290 | if( m_global_events.count( name ) == 0 ) |
| 1291 | { |
| 1292 | // create a new storage container |
| 1293 | m_global_events[name] = new Chuck_Global_Event_Container; |
| 1294 | // create the chuck object |
| 1295 | m_global_events[name]->val = |
| 1296 | (Chuck_Event *) instantiate_and_initialize_object( type, m_vm ); |
| 1297 | // add a reference to it so it won't be deleted until we're done |
| 1298 | // cleaning up the VM |
| 1299 | m_global_events[name]->val->add_ref(); |
| 1300 | // store its type in the container, too (is it a user-defined class?) |
| 1301 | m_global_events[name]->type = type; |
| 1302 | } |
| 1303 | // already exists. check if there's a type mismatch. |
| 1304 | else if( type->base_name != m_global_events[name]->type->base_name ) |
| 1305 | { |
| 1306 | return FALSE; |
| 1307 | } |
| 1308 | |
| 1309 | return TRUE; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | //----------------------------------------------------------------------------- |
| 1316 | // name: get_global_event() |
| 1317 | // desc: get a pointer directly from the vm (internal) |
| 1318 | //----------------------------------------------------------------------------- |
| 1319 | Chuck_Event * Chuck_Globals_Manager::get_global_event( const std::string & name ) |
| 1320 | { |
| 1321 | return m_global_events[name]->val; |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | //----------------------------------------------------------------------------- |
| 1328 | // name: get_ptr_to_global_event() |
| 1329 | // desc: get a pointer pointer directly from the vm (internal) |
| 1330 | //----------------------------------------------------------------------------- |
| 1331 | Chuck_Event * * Chuck_Globals_Manager::get_ptr_to_global_event( const std::string & name ) |
| 1332 | { |
| 1333 | return &( m_global_events[name]->val ); |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | //----------------------------------------------------------------------------- |
| 1340 | // name: getGlobalUGenSamples() |
| 1341 | // desc: get buffer samples |
| 1342 | //----------------------------------------------------------------------------- |
| 1343 | t_CKBOOL Chuck_Globals_Manager::getGlobalUGenSamples( const char * name, |
| 1344 | SAMPLE * buffer, int numFrames ) |
| 1345 | { |
| 1346 | // if hasn't been init, or it has been init and hasn't been constructed, |
| 1347 | if( m_global_ugens.count( name ) == 0 || |
| 1348 | should_call_global_ctor( name, te_globalUGen ) ) |
| 1349 | { |
| 1350 | // fail without doing anything |
| 1351 | return FALSE; |
| 1352 | } |
| 1353 | |
| 1354 | // else, fill (if the ugen isn't buffered, then it will fill with zeroes) |
| 1355 | m_global_ugens[name]->val->get_buffer( buffer, numFrames ); |
| 1356 | |
| 1357 | return TRUE; |
| 1358 | } |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | |
| 1363 | //----------------------------------------------------------------------------- |
| 1364 | // name: getGlobalUGenSamplesMulti() |
| 1365 | // desc: get buffer samples, multichannel edition | 1.5.3.2 (eito, nick, ge) |
| 1366 | //----------------------------------------------------------------------------- |
| 1367 | t_CKBOOL Chuck_Globals_Manager::getGlobalUGenSamplesMulti( const char * name, |
| 1368 | SAMPLE * buffer, |
| 1369 | int numFrames, |
| 1370 | int numChannels ) |
| 1371 | { |
| 1372 | // if hasn't been init, or it has been init and hasn't been constructed, |
| 1373 | if( m_global_ugens.count( name ) == 0 || |
| 1374 | should_call_global_ctor( name, te_globalUGen ) ) |
| 1375 | { |
| 1376 | // fail without doing anything |
| 1377 | return FALSE; |
| 1378 | } |
| 1379 | |
| 1380 | // get the UGen |
| 1381 | Chuck_UGen * ugen = m_global_ugens[name]->val; |
| 1382 | // get number of channels |
| 1383 | t_CKINT multichans = ugen->m_multi_chan_size; |
| 1384 | |
| 1385 | // check that # of channels match |
| 1386 | if( multichans != numChannels ) |
| 1387 | { |
| 1388 | // fail without doing anything |
| 1389 | return FALSE; |
| 1390 | } |
| 1391 | |
| 1392 | // if > mono |
| 1393 | if( multichans ) |
| 1394 | { |
| 1395 | // loop over channel |
| 1396 | for( t_CKINT c = 0; c < multichans; c++ ) |
| 1397 | { |
| 1398 | // copy in chunk (non-interleaved) |
| 1399 | ugen->m_multi_chan[c]->get_buffer( buffer, numFrames ); |
| 1400 | // advance buffer pointer |
| 1401 | buffer += numFrames; |
| 1402 | } |
| 1403 | } |
| 1404 | else // mono |
| 1405 | { |
| 1406 | // fill (if the ugen isn't buffered, then it will fill with zeroes) |
| 1407 | ugen->get_buffer( buffer, numFrames ); |
| 1408 | } |
| 1409 | |
| 1410 | return TRUE; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | //----------------------------------------------------------------------------- |
| 1417 | // name: init_global_ugen() |
| 1418 | // desc: tell the vm that a global ugen is now available |
| 1419 | //----------------------------------------------------------------------------- |
| 1420 | t_CKBOOL Chuck_Globals_Manager::init_global_ugen( const std::string & name, |
| 1421 | Chuck_Type * type ) |
| 1422 | { |
| 1423 | |
| 1424 | // if it hasn't been initted yet |
| 1425 | if( m_global_ugens.count( name ) == 0 ) |
| 1426 | { |
| 1427 | // create a new storage container |
| 1428 | m_global_ugens[name] = new Chuck_Global_UGen_Container; |
| 1429 | // create the chuck object |
| 1430 | m_global_ugens[name]->val = |
| 1431 | (Chuck_UGen *) instantiate_and_initialize_object( type, m_vm ); |
| 1432 | // add a reference to it so it won't be deleted until we're done |
| 1433 | // cleaning up the VM |
| 1434 | m_global_ugens[name]->val->add_ref(); |
| 1435 | // store its type in the container, too (is it a user-defined class?) |
| 1436 | m_global_ugens[name]->type = type; |
| 1437 | } |
| 1438 | // already exists. check if there's a type mismatch. |
| 1439 | else if( type->base_name != m_global_ugens[name]->type->base_name ) |
| 1440 | { |
| 1441 | return FALSE; |
| 1442 | } |
| 1443 | |
| 1444 | return TRUE; |
| 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | //----------------------------------------------------------------------------- |
| 1451 | // name: is_global_ugen_init() |
| 1452 | // desc: has a global ugen been initialized during emit? |
| 1453 | //----------------------------------------------------------------------------- |
| 1454 | t_CKBOOL Chuck_Globals_Manager::is_global_ugen_init( const std::string & name ) |
| 1455 | { |
| 1456 | return m_global_ugens.count( name ) > 0; |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | //----------------------------------------------------------------------------- |
| 1463 | // name: is_global_ugen_valid() |
| 1464 | // desc: has a global ugen been initialized during emit |
| 1465 | // and constructed during runtime? |
| 1466 | //----------------------------------------------------------------------------- |
| 1467 | t_CKBOOL Chuck_Globals_Manager::is_global_ugen_valid( const std::string & name ) |
| 1468 | { |
| 1469 | return is_global_ugen_init( name ) && |
| 1470 | !should_call_global_ctor( name, te_globalUGen ); |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | //----------------------------------------------------------------------------- |
| 1477 | // name: get_global_ugen() |
| 1478 | // desc: get directly from the vm (internal) |
| 1479 | //----------------------------------------------------------------------------- |
| 1480 | Chuck_UGen * Chuck_Globals_Manager::get_global_ugen( const std::string & name ) |
| 1481 | { |
| 1482 | return m_global_ugens[name]->val; |
| 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | //----------------------------------------------------------------------------- |
| 1489 | // name: get_ptr_to_global_ugen() |
| 1490 | // desc: get ptr directly from the vm (internal) |
| 1491 | //----------------------------------------------------------------------------- |
| 1492 | Chuck_UGen * * Chuck_Globals_Manager::get_ptr_to_global_ugen( const std::string & name ) |
| 1493 | { |
| 1494 | return &( m_global_ugens[name]->val ); |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | |
| 1499 | |
| 1500 | //----------------------------------------------------------------------------- |
| 1501 | // name: setGlobalIntArray() |
| 1502 | // desc: tell the vm to set an entire int array |
| 1503 | //----------------------------------------------------------------------------- |
| 1504 | t_CKBOOL Chuck_Globals_Manager::setGlobalIntArray( const char * name, |
| 1505 | t_CKINT arrayValues[], |
| 1506 | t_CKUINT numValues ) |
| 1507 | { |
| 1508 | Chuck_Set_Global_Int_Array_Request * message = |
| 1509 | new Chuck_Set_Global_Int_Array_Request; |
| 1510 | message->name = name; |
| 1511 | message->arrayValues.resize( numValues ); |
| 1512 | for( int i = 0; i < numValues; i++ ) |
| 1513 | { |
| 1514 | message->arrayValues[i] = arrayValues[i]; |
| 1515 | } |
| 1516 | |
| 1517 | Chuck_Global_Request r; |
| 1518 | r.type = set_global_int_array_request; |
| 1519 | r.setIntArrayRequest = message; |
| 1520 | // chuck object might not be constructed on time. retry only once |
| 1521 | r.retries = 1; |
| 1522 | |
| 1523 | m_global_request_queue.put( r ); |
| 1524 | |
| 1525 | return TRUE; |
| 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | //----------------------------------------------------------------------------- |
| 1532 | // name: getGlobalIntArray() |
| 1533 | // desc: tell the vm to get an entire int array |
| 1534 | //----------------------------------------------------------------------------- |
| 1535 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArray( const char * name, |
| 1536 | void (* callback)(t_CKINT[], t_CKUINT) ) |
| 1537 | { |
| 1538 | Chuck_Get_Global_Int_Array_Request * message = |
| 1539 | new Chuck_Get_Global_Int_Array_Request; |
| 1540 | message->name = name; |
| 1541 | message->cb = callback; |
| 1542 | message->cb_type = ck_get_plain; |
| 1543 | |
| 1544 | Chuck_Global_Request r; |
| 1545 | r.type = get_global_int_array_request; |
| 1546 | r.getIntArrayRequest = message; |
| 1547 | // chuck object might not be constructed on time. retry only once |
| 1548 | r.retries = 1; |
| 1549 | |
| 1550 | m_global_request_queue.put( r ); |
| 1551 | |
| 1552 | return TRUE; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | //----------------------------------------------------------------------------- |
| 1559 | // name: getGlobalIntArray() |
| 1560 | // desc: tell the vm to get an entire int array, with a named callback |
| 1561 | //----------------------------------------------------------------------------- |
| 1562 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArray( const char * name, |
| 1563 | void (* callback)(const char *, t_CKINT[], t_CKUINT) ) |
| 1564 | { |
| 1565 | Chuck_Get_Global_Int_Array_Request * message = |
| 1566 | new Chuck_Get_Global_Int_Array_Request; |
| 1567 | message->name = name; |
| 1568 | message->cb_with_name = callback; |
| 1569 | message->cb_type = ck_get_name; |
| 1570 | |
| 1571 | Chuck_Global_Request r; |
| 1572 | r.type = get_global_int_array_request; |
| 1573 | r.getIntArrayRequest = message; |
| 1574 | // chuck object might not be constructed on time. retry only once |
| 1575 | r.retries = 1; |
| 1576 | |
| 1577 | m_global_request_queue.put( r ); |
| 1578 | |
| 1579 | return TRUE; |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | //----------------------------------------------------------------------------- |
| 1586 | // name: getGlobalIntArray() |
| 1587 | // desc: tell the vm to get an entire int array, with id in callback |
| 1588 | //----------------------------------------------------------------------------- |
| 1589 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArray( const char * name, |
| 1590 | t_CKINT id, void (* callback)(t_CKINT, t_CKINT[], t_CKUINT) ) |
| 1591 | { |
| 1592 | Chuck_Get_Global_Int_Array_Request * message = |
| 1593 | new Chuck_Get_Global_Int_Array_Request; |
| 1594 | message->name = name; |
| 1595 | message->cb_with_id = callback; |
| 1596 | message->id = id; |
| 1597 | message->cb_type = ck_get_id; |
| 1598 | |
| 1599 | Chuck_Global_Request r; |
| 1600 | r.type = get_global_int_array_request; |
| 1601 | r.getIntArrayRequest = message; |
| 1602 | // chuck object might not be constructed on time. retry only once |
| 1603 | r.retries = 1; |
| 1604 | |
| 1605 | m_global_request_queue.put( r ); |
| 1606 | |
| 1607 | return TRUE; |
| 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | //----------------------------------------------------------------------------- |
| 1614 | // name: setGlobalIntArrayValue() |
| 1615 | // desc: tell the vm to set one value of an int array by index |
| 1616 | //----------------------------------------------------------------------------- |
| 1617 | t_CKBOOL Chuck_Globals_Manager::setGlobalIntArrayValue( const char * name, |
| 1618 | t_CKUINT index, t_CKINT value ) |
| 1619 | { |
| 1620 | Chuck_Set_Global_Int_Array_Value_Request * message = |
| 1621 | new Chuck_Set_Global_Int_Array_Value_Request; |
| 1622 | message->name = name; |
| 1623 | message->index = index; |
| 1624 | message->value = value; |
| 1625 | |
| 1626 | Chuck_Global_Request r; |
| 1627 | r.type = set_global_int_array_value_request; |
| 1628 | r.setIntArrayValueRequest = message; |
| 1629 | // chuck object might not be constructed on time. retry only once |
| 1630 | r.retries = 1; |
| 1631 | |
| 1632 | m_global_request_queue.put( r ); |
| 1633 | |
| 1634 | return TRUE; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | //----------------------------------------------------------------------------- |
| 1641 | // name: getGlobalIntArrayValue() |
| 1642 | // desc: tell the vm to get one value of an int array by index |
| 1643 | //----------------------------------------------------------------------------- |
| 1644 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArrayValue( const char * name, |
| 1645 | t_CKUINT index, void (* callback)(t_CKINT) ) |
| 1646 | { |
| 1647 | Chuck_Get_Global_Int_Array_Value_Request * message = |
| 1648 | new Chuck_Get_Global_Int_Array_Value_Request; |
| 1649 | message->name = name; |
| 1650 | message->index = index; |
| 1651 | message->cb = callback; |
| 1652 | message->cb_type = ck_get_plain; |
| 1653 | |
| 1654 | Chuck_Global_Request r; |
| 1655 | r.type = get_global_int_array_value_request; |
| 1656 | r.getIntArrayValueRequest = message; |
| 1657 | // chuck object might not be constructed on time. retry only once |
| 1658 | r.retries = 1; |
| 1659 | |
| 1660 | m_global_request_queue.put( r ); |
| 1661 | |
| 1662 | return TRUE; |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | //----------------------------------------------------------------------------- |
| 1669 | // name: getGlobalIntArrayValue() |
| 1670 | // desc: tell the vm to get one value of an int array by index, with named callback |
| 1671 | //----------------------------------------------------------------------------- |
| 1672 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArrayValue( const char * name, |
| 1673 | t_CKUINT index, void (* callback)(const char *, t_CKINT) ) |
| 1674 | { |
| 1675 | Chuck_Get_Global_Int_Array_Value_Request * message = |
| 1676 | new Chuck_Get_Global_Int_Array_Value_Request; |
| 1677 | message->name = name; |
| 1678 | message->index = index; |
| 1679 | message->cb_with_name = callback; |
| 1680 | message->cb_type = ck_get_name; |
| 1681 | |
| 1682 | Chuck_Global_Request r; |
| 1683 | r.type = get_global_int_array_value_request; |
| 1684 | r.getIntArrayValueRequest = message; |
| 1685 | // chuck object might not be constructed on time. retry only once |
| 1686 | r.retries = 1; |
| 1687 | |
| 1688 | m_global_request_queue.put( r ); |
| 1689 | |
| 1690 | return TRUE; |
| 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | //----------------------------------------------------------------------------- |
| 1697 | // name: getGlobalIntArrayValue() |
| 1698 | // desc: tell the vm to get one value of an int array by index, with id in callback |
| 1699 | //----------------------------------------------------------------------------- |
| 1700 | t_CKBOOL Chuck_Globals_Manager::getGlobalIntArrayValue( const char * name, |
| 1701 | t_CKINT id, t_CKUINT index, void (* callback)(t_CKINT, t_CKINT) ) |
| 1702 | { |
| 1703 | Chuck_Get_Global_Int_Array_Value_Request * message = |
| 1704 | new Chuck_Get_Global_Int_Array_Value_Request; |
| 1705 | message->name = name; |
| 1706 | message->index = index; |
| 1707 | message->cb_with_id = callback; |
| 1708 | message->id = id; |
| 1709 | message->cb_type = ck_get_id; |
| 1710 | |
| 1711 | Chuck_Global_Request r; |
| 1712 | r.type = get_global_int_array_value_request; |
| 1713 | r.getIntArrayValueRequest = message; |
| 1714 | // chuck object might not be constructed on time. retry only once |
| 1715 | r.retries = 1; |
| 1716 | |
| 1717 | m_global_request_queue.put( r ); |
| 1718 | |
| 1719 | return TRUE; |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | //----------------------------------------------------------------------------- |
| 1726 | // name: setGlobalAssociativeIntArrayValue() |
| 1727 | // desc: tell the vm to set one value of an associative array by string key |
| 1728 | //----------------------------------------------------------------------------- |
| 1729 | t_CKBOOL Chuck_Globals_Manager::setGlobalAssociativeIntArrayValue( |
| 1730 | const char * name, const char * key, t_CKINT value ) |
| 1731 | { |
| 1732 | Chuck_Set_Global_Associative_Int_Array_Value_Request * message = |
| 1733 | new Chuck_Set_Global_Associative_Int_Array_Value_Request; |
| 1734 | message->name = name; |
| 1735 | message->key = key; |
| 1736 | message->value = value; |
| 1737 | |
| 1738 | Chuck_Global_Request r; |
| 1739 | r.type = set_global_associative_int_array_value_request; |
| 1740 | r.setAssociativeIntArrayValueRequest = message; |
| 1741 | // chuck object might not be constructed on time. retry only once |
| 1742 | r.retries = 1; |
| 1743 | |
| 1744 | m_global_request_queue.put( r ); |
| 1745 | |
| 1746 | return TRUE; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | //----------------------------------------------------------------------------- |
| 1753 | // name: getGlobalAssociativeIntArrayValue() |
| 1754 | // desc: tell the vm to get one value of an associative array by string key |
| 1755 | //----------------------------------------------------------------------------- |
| 1756 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeIntArrayValue( |
| 1757 | const char * name, const char * key, void (* callback)(t_CKINT) ) |
| 1758 | { |
| 1759 | Chuck_Get_Global_Associative_Int_Array_Value_Request * message = |
| 1760 | new Chuck_Get_Global_Associative_Int_Array_Value_Request; |
| 1761 | message->name = name; |
| 1762 | message->key = key; |
| 1763 | message->cb = callback; |
| 1764 | message->cb_type = ck_get_plain; |
| 1765 | |
| 1766 | Chuck_Global_Request r; |
| 1767 | r.type = get_global_associative_int_array_value_request; |
| 1768 | r.getAssociativeIntArrayValueRequest = message; |
| 1769 | // chuck object might not be constructed on time. retry only once |
| 1770 | r.retries = 1; |
| 1771 | |
| 1772 | m_global_request_queue.put( r ); |
| 1773 | |
| 1774 | return TRUE; |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | |
| 1779 | |
| 1780 | //----------------------------------------------------------------------------- |
| 1781 | // name: getGlobalAssociativeIntArrayValue() |
| 1782 | // desc: tell the vm to get one value of an associative array by string key, with named callback |
| 1783 | //----------------------------------------------------------------------------- |
| 1784 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeIntArrayValue( const char * name, |
| 1785 | const char * key, void (* callback)(const char *, t_CKINT) ) |
| 1786 | { |
| 1787 | Chuck_Get_Global_Associative_Int_Array_Value_Request * message = |
| 1788 | new Chuck_Get_Global_Associative_Int_Array_Value_Request; |
| 1789 | message->name = name; |
| 1790 | message->key = key; |
| 1791 | message->cb_with_name = callback; |
| 1792 | message->cb_type = ck_get_name; |
| 1793 | |
| 1794 | Chuck_Global_Request r; |
| 1795 | r.type = get_global_associative_int_array_value_request; |
| 1796 | r.getAssociativeIntArrayValueRequest = message; |
| 1797 | // chuck object might not be constructed on time. retry only once |
| 1798 | r.retries = 1; |
| 1799 | |
| 1800 | m_global_request_queue.put( r ); |
| 1801 | |
| 1802 | return TRUE; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | //----------------------------------------------------------------------------- |
| 1809 | // name: getGlobalAssociativeIntArrayValue() |
| 1810 | // desc: tell the vm to get one value of an associative array by string key, with id in callback |
| 1811 | //----------------------------------------------------------------------------- |
| 1812 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeIntArrayValue( const char * name, |
| 1813 | t_CKINT id, const char * key, void (* callback)(t_CKINT, t_CKINT) ) |
| 1814 | { |
| 1815 | Chuck_Get_Global_Associative_Int_Array_Value_Request * message = |
| 1816 | new Chuck_Get_Global_Associative_Int_Array_Value_Request; |
| 1817 | message->name = name; |
| 1818 | message->key = key; |
| 1819 | message->cb_with_id = callback; |
| 1820 | message->id = id; |
| 1821 | message->cb_type = ck_get_id; |
| 1822 | |
| 1823 | Chuck_Global_Request r; |
| 1824 | r.type = get_global_associative_int_array_value_request; |
| 1825 | r.getAssociativeIntArrayValueRequest = message; |
| 1826 | // chuck object might not be constructed on time. retry only once |
| 1827 | r.retries = 1; |
| 1828 | |
| 1829 | m_global_request_queue.put( r ); |
| 1830 | |
| 1831 | return TRUE; |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | //----------------------------------------------------------------------------- |
| 1838 | // name: setGlobalFloatArray() |
| 1839 | // desc: tell the vm to set an entire float array |
| 1840 | //----------------------------------------------------------------------------- |
| 1841 | t_CKBOOL Chuck_Globals_Manager::setGlobalFloatArray( const char * name, |
| 1842 | t_CKFLOAT arrayValues[], t_CKUINT numValues ) |
| 1843 | { |
| 1844 | Chuck_Set_Global_Float_Array_Request * message = |
| 1845 | new Chuck_Set_Global_Float_Array_Request; |
| 1846 | message->name = name; |
| 1847 | message->arrayValues.resize( numValues ); |
| 1848 | for( int i = 0; i < numValues; i++ ) |
| 1849 | { |
| 1850 | message->arrayValues[i] = arrayValues[i]; |
| 1851 | } |
| 1852 | |
| 1853 | Chuck_Global_Request r; |
| 1854 | r.type = set_global_float_array_request; |
| 1855 | r.setFloatArrayRequest = message; |
| 1856 | // chuck object might not be constructed on time. retry only once |
| 1857 | r.retries = 1; |
| 1858 | |
| 1859 | m_global_request_queue.put( r ); |
| 1860 | |
| 1861 | return TRUE; |
| 1862 | } |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | //----------------------------------------------------------------------------- |
| 1868 | // name: getGlobalFloatArray() |
| 1869 | // desc: tell the vm to get an entire float array |
| 1870 | //----------------------------------------------------------------------------- |
| 1871 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArray( const char * name, |
| 1872 | void (* callback)(t_CKFLOAT[], t_CKUINT)) |
| 1873 | { |
| 1874 | Chuck_Get_Global_Float_Array_Request * message = |
| 1875 | new Chuck_Get_Global_Float_Array_Request; |
| 1876 | message->name = name; |
| 1877 | message->cb = callback; |
| 1878 | message->cb_type = ck_get_plain; |
| 1879 | |
| 1880 | Chuck_Global_Request r; |
| 1881 | r.type = get_global_float_array_request; |
| 1882 | r.getFloatArrayRequest = message; |
| 1883 | // chuck object might not be constructed on time. retry only once |
| 1884 | r.retries = 1; |
| 1885 | |
| 1886 | m_global_request_queue.put( r ); |
| 1887 | |
| 1888 | return TRUE; |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | //----------------------------------------------------------------------------- |
| 1895 | // name: getGlobalFloatArray() |
| 1896 | // desc: tell the vm to get an entire float array, with a named callback |
| 1897 | //----------------------------------------------------------------------------- |
| 1898 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArray( const char * name, |
| 1899 | void (* callback)(const char *, t_CKFLOAT[], t_CKUINT) ) |
| 1900 | { |
| 1901 | Chuck_Get_Global_Float_Array_Request * message = |
| 1902 | new Chuck_Get_Global_Float_Array_Request; |
| 1903 | message->name = name; |
| 1904 | message->cb_with_name = callback; |
| 1905 | message->cb_type = ck_get_name; |
| 1906 | |
| 1907 | Chuck_Global_Request r; |
| 1908 | r.type = get_global_float_array_request; |
| 1909 | r.getFloatArrayRequest = message; |
| 1910 | // chuck object might not be constructed on time. retry only once |
| 1911 | r.retries = 1; |
| 1912 | |
| 1913 | m_global_request_queue.put( r ); |
| 1914 | |
| 1915 | return TRUE; |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | //----------------------------------------------------------------------------- |
| 1922 | // name: getGlobalFloatArray() |
| 1923 | // desc: tell the vm to get an entire float array, with id in callback |
| 1924 | //----------------------------------------------------------------------------- |
| 1925 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArray( const char * name, |
| 1926 | t_CKINT id, void (* callback)(t_CKINT, t_CKFLOAT[], t_CKUINT)) |
| 1927 | { |
| 1928 | Chuck_Get_Global_Float_Array_Request * message = |
| 1929 | new Chuck_Get_Global_Float_Array_Request; |
| 1930 | message->name = name; |
| 1931 | message->cb_with_id = callback; |
| 1932 | message->id = id; |
| 1933 | message->cb_type = ck_get_id; |
| 1934 | |
| 1935 | Chuck_Global_Request r; |
| 1936 | r.type = get_global_float_array_request; |
| 1937 | r.getFloatArrayRequest = message; |
| 1938 | // chuck object might not be constructed on time. retry only once |
| 1939 | r.retries = 1; |
| 1940 | |
| 1941 | m_global_request_queue.put( r ); |
| 1942 | |
| 1943 | return TRUE; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | //----------------------------------------------------------------------------- |
| 1950 | // name: setGlobalFloatArrayValue() |
| 1951 | // desc: tell the vm to set one value of an float array by index |
| 1952 | //----------------------------------------------------------------------------- |
| 1953 | t_CKBOOL Chuck_Globals_Manager::setGlobalFloatArrayValue( const char * name, |
| 1954 | t_CKUINT index, t_CKFLOAT value ) |
| 1955 | { |
| 1956 | Chuck_Set_Global_Float_Array_Value_Request * message = |
| 1957 | new Chuck_Set_Global_Float_Array_Value_Request; |
| 1958 | message->name = name; |
| 1959 | message->index = index; |
| 1960 | message->value = value; |
| 1961 | |
| 1962 | Chuck_Global_Request r; |
| 1963 | r.type = set_global_float_array_value_request; |
| 1964 | r.setFloatArrayValueRequest = message; |
| 1965 | // chuck object might not be constructed on time. retry only once |
| 1966 | r.retries = 1; |
| 1967 | |
| 1968 | m_global_request_queue.put( r ); |
| 1969 | |
| 1970 | return TRUE; |
| 1971 | } |
| 1972 | |
| 1973 | |
| 1974 | |
| 1975 | |
| 1976 | //----------------------------------------------------------------------------- |
| 1977 | // name: getGlobalFloatArrayValue() |
| 1978 | // desc: tell the vm to get one value of an float array by index |
| 1979 | //----------------------------------------------------------------------------- |
| 1980 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArrayValue( const char * name, |
| 1981 | t_CKUINT index, void (* callback)(t_CKFLOAT) ) |
| 1982 | { |
| 1983 | Chuck_Get_Global_Float_Array_Value_Request * message = |
| 1984 | new Chuck_Get_Global_Float_Array_Value_Request; |
| 1985 | message->name = name; |
| 1986 | message->index = index; |
| 1987 | message->cb = callback; |
| 1988 | message->cb_type = ck_get_plain; |
| 1989 | |
| 1990 | Chuck_Global_Request r; |
| 1991 | r.type = get_global_float_array_value_request; |
| 1992 | r.getFloatArrayValueRequest = message; |
| 1993 | // chuck object might not be constructed on time. retry only once |
| 1994 | r.retries = 1; |
| 1995 | |
| 1996 | m_global_request_queue.put( r ); |
| 1997 | |
| 1998 | return TRUE; |
| 1999 | } |
| 2000 | |
| 2001 | |
| 2002 | |
| 2003 | |
| 2004 | //----------------------------------------------------------------------------- |
| 2005 | // name: getGlobalFloatArrayValue() |
| 2006 | // desc: tell the vm to get one value of an float array by index, with a named callback |
| 2007 | //----------------------------------------------------------------------------- |
| 2008 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArrayValue( const char * name, |
| 2009 | t_CKUINT index, void (* callback)(const char *, t_CKFLOAT) ) |
| 2010 | { |
| 2011 | Chuck_Get_Global_Float_Array_Value_Request * message = |
| 2012 | new Chuck_Get_Global_Float_Array_Value_Request; |
| 2013 | message->name = name; |
| 2014 | message->index = index; |
| 2015 | message->cb_with_name = callback; |
| 2016 | message->cb_type = ck_get_name; |
| 2017 | |
| 2018 | Chuck_Global_Request r; |
| 2019 | r.type = get_global_float_array_value_request; |
| 2020 | r.getFloatArrayValueRequest = message; |
| 2021 | // chuck object might not be constructed on time. retry only once |
| 2022 | r.retries = 1; |
| 2023 | |
| 2024 | m_global_request_queue.put( r ); |
| 2025 | |
| 2026 | return TRUE; |
| 2027 | } |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | |
| 2032 | //----------------------------------------------------------------------------- |
| 2033 | // name: getGlobalFloatArrayValue() |
| 2034 | // desc: tell the vm to get one value of an float array by index, with id in callback |
| 2035 | //----------------------------------------------------------------------------- |
| 2036 | t_CKBOOL Chuck_Globals_Manager::getGlobalFloatArrayValue( const char * name, |
| 2037 | t_CKINT id, t_CKUINT index, void (* callback)(t_CKINT, t_CKFLOAT) ) |
| 2038 | { |
| 2039 | Chuck_Get_Global_Float_Array_Value_Request * message = |
| 2040 | new Chuck_Get_Global_Float_Array_Value_Request; |
| 2041 | message->name = name; |
| 2042 | message->index = index; |
| 2043 | message->cb_with_id = callback; |
| 2044 | message->id = id; |
| 2045 | message->cb_type = ck_get_id; |
| 2046 | |
| 2047 | Chuck_Global_Request r; |
| 2048 | r.type = get_global_float_array_value_request; |
| 2049 | r.getFloatArrayValueRequest = message; |
| 2050 | // chuck object might not be constructed on time. retry only once |
| 2051 | r.retries = 1; |
| 2052 | |
| 2053 | m_global_request_queue.put( r ); |
| 2054 | |
| 2055 | return TRUE; |
| 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | //----------------------------------------------------------------------------- |
| 2062 | // name: setGlobalAssociativeFloatArrayValue() |
| 2063 | // desc: tell the vm to set one value of an associative array by string key |
| 2064 | //----------------------------------------------------------------------------- |
| 2065 | t_CKBOOL Chuck_Globals_Manager::setGlobalAssociativeFloatArrayValue( const char * name, |
| 2066 | const char * key, t_CKFLOAT value ) |
| 2067 | { |
| 2068 | Chuck_Set_Global_Associative_Float_Array_Value_Request * message = |
| 2069 | new Chuck_Set_Global_Associative_Float_Array_Value_Request; |
| 2070 | message->name = name; |
| 2071 | message->key = key; |
| 2072 | message->value = value; |
| 2073 | |
| 2074 | Chuck_Global_Request r; |
| 2075 | r.type = set_global_associative_float_array_value_request; |
| 2076 | r.setAssociativeFloatArrayValueRequest = message; |
| 2077 | // chuck object might not be constructed on time. retry only once |
| 2078 | r.retries = 1; |
| 2079 | |
| 2080 | m_global_request_queue.put( r ); |
| 2081 | |
| 2082 | return TRUE; |
| 2083 | } |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | //----------------------------------------------------------------------------- |
| 2089 | // name: getGlobalAssociativeFloatArrayValue() |
| 2090 | // desc: tell the vm to get one value of an associative array by string key |
| 2091 | //----------------------------------------------------------------------------- |
| 2092 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeFloatArrayValue( const char * name, |
| 2093 | const char * key, void (* callback)(t_CKFLOAT) ) |
| 2094 | { |
| 2095 | Chuck_Get_Global_Associative_Float_Array_Value_Request * message = |
| 2096 | new Chuck_Get_Global_Associative_Float_Array_Value_Request; |
| 2097 | message->name = name; |
| 2098 | message->key = key; |
| 2099 | message->cb = callback; |
| 2100 | message->cb_type = ck_get_plain; |
| 2101 | |
| 2102 | Chuck_Global_Request r; |
| 2103 | r.type = get_global_associative_float_array_value_request; |
| 2104 | r.getAssociativeFloatArrayValueRequest = message; |
| 2105 | // chuck object might not be constructed on time. retry only once |
| 2106 | r.retries = 1; |
| 2107 | |
| 2108 | m_global_request_queue.put( r ); |
| 2109 | |
| 2110 | return TRUE; |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | //----------------------------------------------------------------------------- |
| 2117 | // name: getGlobalAssociativeFloatArrayValue() |
| 2118 | // desc: tell the vm to get one value of an associative array by string key, with a named callback |
| 2119 | //----------------------------------------------------------------------------- |
| 2120 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeFloatArrayValue( const char * name, |
| 2121 | const char * key, void (* callback)(const char *, t_CKFLOAT) ) |
| 2122 | { |
| 2123 | Chuck_Get_Global_Associative_Float_Array_Value_Request * message = |
| 2124 | new Chuck_Get_Global_Associative_Float_Array_Value_Request; |
| 2125 | message->name = name; |
| 2126 | message->key = key; |
| 2127 | message->cb_with_name = callback; |
| 2128 | message->cb_type = ck_get_name; |
| 2129 | |
| 2130 | Chuck_Global_Request r; |
| 2131 | r.type = get_global_associative_float_array_value_request; |
| 2132 | r.getAssociativeFloatArrayValueRequest = message; |
| 2133 | // chuck object might not be constructed on time. retry only once |
| 2134 | r.retries = 1; |
| 2135 | |
| 2136 | m_global_request_queue.put( r ); |
| 2137 | |
| 2138 | return TRUE; |
| 2139 | } |
| 2140 | |
| 2141 | |
| 2142 | |
| 2143 | |
| 2144 | //----------------------------------------------------------------------------- |
| 2145 | // name: getGlobalAssociativeFloatArrayValue() |
| 2146 | // desc: tell the vm to get one value of an associative array by string key, with id in callback |
| 2147 | //----------------------------------------------------------------------------- |
| 2148 | t_CKBOOL Chuck_Globals_Manager::getGlobalAssociativeFloatArrayValue( |
| 2149 | const char * name, t_CKINT id, const char * key, |
| 2150 | void (* callback)(t_CKINT, t_CKFLOAT) ) |
| 2151 | { |
| 2152 | Chuck_Get_Global_Associative_Float_Array_Value_Request * message = |
| 2153 | new Chuck_Get_Global_Associative_Float_Array_Value_Request; |
| 2154 | message->name = name; |
| 2155 | message->key = key; |
| 2156 | message->cb_with_id = callback; |
| 2157 | message->id = id; |
| 2158 | message->cb_type = ck_get_id; |
| 2159 | |
| 2160 | Chuck_Global_Request r; |
| 2161 | r.type = get_global_associative_float_array_value_request; |
| 2162 | r.getAssociativeFloatArrayValueRequest = message; |
| 2163 | // chuck object might not be constructed on time. retry only once |
| 2164 | r.retries = 1; |
| 2165 | |
| 2166 | m_global_request_queue.put( r ); |
| 2167 | |
| 2168 | return TRUE; |
| 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | |
| 2173 | |
| 2174 | //----------------------------------------------------------------------------- |
| 2175 | // name: getAllGlobalVariables() |
| 2176 | // desc: get a global float by name |
| 2177 | //----------------------------------------------------------------------------- |
| 2178 | t_CKBOOL Chuck_Globals_Manager::getAllGlobalVariables( |
| 2179 | void (*callback)( const std::vector<Chuck_Globals_TypeValue> & list, void * data ), |
| 2180 | void * data ) |
| 2181 | { |
| 2182 | Chuck_Get_Global_All_Request * get_all_message = new Chuck_Get_Global_All_Request; |
| 2183 | get_all_message->cb = callback; |
| 2184 | get_all_message->data = data; |
| 2185 | |
| 2186 | Chuck_Global_Request r; |
| 2187 | r.type = get_global_all_request; |
| 2188 | r.getAllRequest = get_all_message; |
| 2189 | |
| 2190 | m_global_request_queue.put( r ); |
| 2191 | |
| 2192 | return TRUE; |
| 2193 | } |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | |
| 2198 | //----------------------------------------------------------------------------- |
| 2199 | // name: execute_chuck_msg_with_globals() |
| 2200 | // desc: execute a Chuck_Msg in the globals callback |
| 2201 | //----------------------------------------------------------------------------- |
| 2202 | t_CKBOOL Chuck_Globals_Manager::execute_chuck_msg_with_globals( Chuck_Msg * msg ) |
| 2203 | { |
| 2204 | Chuck_Execute_Chuck_Msg_Request * message = |
| 2205 | new Chuck_Execute_Chuck_Msg_Request; |
| 2206 | message->msg = msg; |
| 2207 | |
| 2208 | Chuck_Global_Request r; |
| 2209 | r.type = execute_chuck_msg_request; |
| 2210 | r.executeChuckMsgRequest = message; |
| 2211 | r.retries = 0; |
| 2212 | |
| 2213 | m_global_request_queue.put( r ); |
| 2214 | |
| 2215 | return TRUE; |
| 2216 | } |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | |
| 2221 | //----------------------------------------------------------------------------- |
| 2222 | // name: init_global_array() |
| 2223 | // desc: tell the vm that a global string is now available |
| 2224 | //----------------------------------------------------------------------------- |
| 2225 | t_CKBOOL Chuck_Globals_Manager::init_global_array( const std::string & name, Chuck_Type * type, te_GlobalType arr_type ) |
| 2226 | { |
| 2227 | if( m_global_arrays.count( name ) == 0 ) |
| 2228 | { |
| 2229 | // make container |
| 2230 | m_global_arrays[name] = new Chuck_Global_Array_Container( type, arr_type ); |
| 2231 | // do not init |
| 2232 | m_global_arrays[name]->array = NULL; |
| 2233 | // global variable type |
| 2234 | m_global_arrays[name]->array_type = arr_type; |
| 2235 | // note: Chuck_Type * is currently unused, but may be necessary later |
| 2236 | // TODO: how to init if user sets it before any script makes it? |
| 2237 | // TODO: how to keep reference to prevent from being deleted if |
| 2238 | // a script ends and takes the array with it? |
| 2239 | } |
| 2240 | |
| 2241 | return TRUE; |
| 2242 | } |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | |
| 2247 | //----------------------------------------------------------------------------- |
| 2248 | // name: get_global_array() |
| 2249 | // desc: get value directly from the vm (internal) |
| 2250 | //----------------------------------------------------------------------------- |
| 2251 | Chuck_Object * Chuck_Globals_Manager::get_global_array( const std::string & name ) |
| 2252 | { |
| 2253 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2254 | if( m_global_arrays.count(name) == 0 ) return NULL; |
| 2255 | |
| 2256 | return m_global_arrays[name]->array; |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | //----------------------------------------------------------------------------- |
| 2263 | // name: _get_global_int_array_value() |
| 2264 | // desc: get value directly from the vm (internal) |
| 2265 | //----------------------------------------------------------------------------- |
| 2266 | t_CKINT Chuck_Globals_Manager::get_global_int_array_value( const std::string & name, |
| 2267 | t_CKUINT index ) |
| 2268 | { |
| 2269 | t_CKUINT result = 0; |
| 2270 | |
| 2271 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2272 | if( m_global_arrays.count(name) == 0 ) return result; |
| 2273 | |
| 2274 | // get the array from corresponding container |
| 2275 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2276 | if( array != NULL && |
| 2277 | m_global_arrays[name]->array_type == te_globalInt ) |
| 2278 | { |
| 2279 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 2280 | // TODO why is it unsigned int storage? |
| 2281 | intArray->get( index, &result ); |
| 2282 | } |
| 2283 | return result; |
| 2284 | } |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | //----------------------------------------------------------------------------- |
| 2290 | // name: _get_global_associative_int_array_value() |
| 2291 | // desc: get value directly from the vm (internal) |
| 2292 | //----------------------------------------------------------------------------- |
| 2293 | t_CKINT Chuck_Globals_Manager::get_global_associative_int_array_value( |
| 2294 | const std::string & name, const std::string & key ) |
| 2295 | { |
| 2296 | t_CKUINT result = 0; |
| 2297 | |
| 2298 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2299 | if( m_global_arrays.count(name) == 0 ) return result; |
| 2300 | |
| 2301 | // get the array from corresponding container |
| 2302 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2303 | if( array != NULL && |
| 2304 | m_global_arrays[name]->array_type == te_globalInt ) |
| 2305 | { |
| 2306 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 2307 | // TODO why is it unsigned int storage? |
| 2308 | intArray->get( key, &result ); |
| 2309 | } |
| 2310 | return result; |
| 2311 | } |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | |
| 2316 | //----------------------------------------------------------------------------- |
| 2317 | // name: _get_global_float_array_value() |
| 2318 | // desc: get value directly from the vm (internal) |
| 2319 | //----------------------------------------------------------------------------- |
| 2320 | t_CKFLOAT Chuck_Globals_Manager::get_global_float_array_value( const std::string & name, |
| 2321 | t_CKUINT index ) |
| 2322 | { |
| 2323 | t_CKFLOAT result = 0; |
| 2324 | |
| 2325 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2326 | if( m_global_arrays.count(name) == 0 ) return result; |
| 2327 | |
| 2328 | // get the array from corresponding container |
| 2329 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2330 | if( array != NULL && |
| 2331 | m_global_arrays[name]->array_type == te_globalFloat ) |
| 2332 | { |
| 2333 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 2334 | floatArray->get( index, &result ); |
| 2335 | } |
| 2336 | return result; |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | |
| 2341 | |
| 2342 | //----------------------------------------------------------------------------- |
| 2343 | // name: _get_global_associative_float_array_value() |
| 2344 | // desc: get value directly from the vm (internal) |
| 2345 | //----------------------------------------------------------------------------- |
| 2346 | t_CKFLOAT Chuck_Globals_Manager::get_global_associative_float_array_value( |
| 2347 | const std::string & name, const std::string & key ) |
| 2348 | { |
| 2349 | t_CKFLOAT result = 0; |
| 2350 | |
| 2351 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2352 | if( m_global_arrays.count(name) == 0 ) return result; |
| 2353 | |
| 2354 | // get the array from corresponding container |
| 2355 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2356 | if( array != NULL && |
| 2357 | m_global_arrays[name]->array_type == te_globalFloat ) |
| 2358 | { |
| 2359 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 2360 | floatArray->get( key, &result ); |
| 2361 | } |
| 2362 | return result; |
| 2363 | } |
| 2364 | |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | //----------------------------------------------------------------------------- |
| 2369 | // name: get_ptr_to_global_array() |
| 2370 | // desc: get ptr directly from the vm (internal) |
| 2371 | //----------------------------------------------------------------------------- |
| 2372 | Chuck_Object * * Chuck_Globals_Manager::get_ptr_to_global_array( const std::string & name ) |
| 2373 | { |
| 2374 | // ensure has entry with name | 1.5.2.2 (ge, eito, nick, andrew) |
| 2375 | if( m_global_arrays.count(name) == 0 ) return NULL; |
| 2376 | |
| 2377 | return &( m_global_arrays[name]->array ); |
| 2378 | } |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | //----------------------------------------------------------------------------- |
| 2384 | // name: set_global_int_array() |
| 2385 | // desc: global array set (internal; audio thread) |
| 2386 | //----------------------------------------------------------------------------- |
| 2387 | t_CKBOOL Chuck_Globals_Manager::set_global_int_array( const std::string & name, |
| 2388 | t_CKINT arrayValues[], |
| 2389 | t_CKUINT numValues ) |
| 2390 | { |
| 2391 | // look for global array with the specified name |
| 2392 | if( m_global_arrays.count(name) == 0 ) |
| 2393 | return FALSE; |
| 2394 | // get chuck array object |
| 2395 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2396 | // check if array exists and is float array |
| 2397 | if( array == NULL || m_global_arrays[name]->array_type != te_globalInt ) |
| 2398 | return FALSE; |
| 2399 | |
| 2400 | // get chuck int array |
| 2401 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *)array; |
| 2402 | // resize array if necessary |
| 2403 | if( intArray->size() < numValues ) |
| 2404 | intArray->set_size( numValues ); |
| 2405 | // copy elements |
| 2406 | for( t_CKUINT i = 0; i < numValues; i++ ) |
| 2407 | intArray->set( i, arrayValues[i] ); |
| 2408 | |
| 2409 | return TRUE; |
| 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | //----------------------------------------------------------------------------- |
| 2416 | // name: set_global_int_array() |
| 2417 | // desc: global array set (internal; audio thread) |
| 2418 | //----------------------------------------------------------------------------- |
| 2419 | t_CKBOOL Chuck_Globals_Manager::set_global_int_array_value( const std::string & name, |
| 2420 | t_CKUINT index, t_CKINT value ) |
| 2421 | { |
| 2422 | // look for global array with the specified name |
| 2423 | if( m_global_arrays.count(name) == 0 ) |
| 2424 | return FALSE; |
| 2425 | // get chuck array object |
| 2426 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2427 | // check if array exists and is int array |
| 2428 | if( array == NULL || m_global_arrays[name]->array_type != te_globalInt ) |
| 2429 | return FALSE; |
| 2430 | |
| 2431 | // get chuck int array |
| 2432 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *)array; |
| 2433 | // resize array if necessary |
| 2434 | if( intArray->size() <= index ) return FALSE; |
| 2435 | // set element at index |
| 2436 | intArray->set( index, value ); |
| 2437 | |
| 2438 | return TRUE; |
| 2439 | } |
| 2440 | |
| 2441 | |
| 2442 | |
| 2443 | |
| 2444 | //----------------------------------------------------------------------------- |
| 2445 | // name: set_global_int_array() |
| 2446 | // desc: global array set (internal; audio thread) |
| 2447 | //----------------------------------------------------------------------------- |
| 2448 | t_CKBOOL Chuck_Globals_Manager::set_global_float_array( const std::string & name, |
| 2449 | t_CKFLOAT arrayValues[], |
| 2450 | t_CKUINT numValues ) |
| 2451 | { |
| 2452 | // look for global array with the specified name |
| 2453 | if( m_global_arrays.count(name) == 0 ) |
| 2454 | return FALSE; |
| 2455 | // get chuck array object |
| 2456 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2457 | // check if array exists and is float array |
| 2458 | if( array == NULL || m_global_arrays[name]->array_type != te_globalFloat ) |
| 2459 | return FALSE; |
| 2460 | |
| 2461 | // get chuck float array |
| 2462 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *)array; |
| 2463 | // resize array if necessary |
| 2464 | if( floatArray->size() < numValues ) |
| 2465 | floatArray->set_size( numValues ); |
| 2466 | // copy elements |
| 2467 | for( t_CKUINT i = 0; i < numValues; i++ ) |
| 2468 | floatArray->set( i, arrayValues[i] ); |
| 2469 | |
| 2470 | return TRUE; |
| 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | //----------------------------------------------------------------------------- |
| 2477 | // name: set_global_int_array() |
| 2478 | // desc: global array set (internal; audio thread) |
| 2479 | //----------------------------------------------------------------------------- |
| 2480 | t_CKBOOL Chuck_Globals_Manager::set_global_float_array_value( const std::string & name, |
| 2481 | t_CKUINT index, t_CKFLOAT value ) |
| 2482 | { |
| 2483 | // look for global array with the specified name |
| 2484 | if( m_global_arrays.count(name) == 0 ) |
| 2485 | return FALSE; |
| 2486 | // get chuck array object |
| 2487 | Chuck_Object * array = m_global_arrays[name]->array; |
| 2488 | // check if array exists and is float array |
| 2489 | if( array == NULL || m_global_arrays[name]->array_type != te_globalFloat ) |
| 2490 | return FALSE; |
| 2491 | |
| 2492 | // get chuck float array |
| 2493 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *)array; |
| 2494 | // resize array if necessary |
| 2495 | if( floatArray->size() <= index ) return FALSE; |
| 2496 | // set element at index |
| 2497 | floatArray->set( index, value ); |
| 2498 | |
| 2499 | return TRUE; |
| 2500 | } |
| 2501 | |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | //----------------------------------------------------------------------------- |
| 2506 | // name: init_global_object() |
| 2507 | // desc: init a global object |
| 2508 | //----------------------------------------------------------------------------- |
| 2509 | t_CKBOOL Chuck_Globals_Manager::init_global_object( const std::string & name, |
| 2510 | Chuck_Type * type ) |
| 2511 | { |
| 2512 | // if it hasn't been initted yet |
| 2513 | if( m_global_objects.count( name ) == 0 ) { |
| 2514 | // create a new storage container |
| 2515 | m_global_objects[name] = new Chuck_Global_Object_Container; |
| 2516 | // create the chuck object |
| 2517 | m_global_objects[name]->val = |
| 2518 | (Chuck_Object *) instantiate_and_initialize_object( type, m_vm ); |
| 2519 | // add a reference to it so it won't be deleted until we're done |
| 2520 | // cleaning up the VM |
| 2521 | m_global_objects[name]->val->add_ref(); |
| 2522 | // store its type in the container, too (is it a user-defined class?) |
| 2523 | m_global_objects[name]->type = type; |
| 2524 | } |
| 2525 | // already exists. check if there's a type mismatch. |
| 2526 | else if( type->base_name != m_global_objects[name]->type->base_name ) |
| 2527 | { |
| 2528 | return FALSE; |
| 2529 | } |
| 2530 | |
| 2531 | return TRUE; |
| 2532 | } |
| 2533 | |
| 2534 | |
| 2535 | |
| 2536 | |
| 2537 | //----------------------------------------------------------------------------- |
| 2538 | // name: is_global_object_init() |
| 2539 | // desc: has a global object been initialized during emit? |
| 2540 | //----------------------------------------------------------------------------- |
| 2541 | t_CKBOOL Chuck_Globals_Manager::is_global_object_init( const std::string & name ) |
| 2542 | { |
| 2543 | return m_global_objects.count( name ) > 0; |
| 2544 | } |
| 2545 | |
| 2546 | |
| 2547 | |
| 2548 | |
| 2549 | //----------------------------------------------------------------------------- |
| 2550 | // name: is_global_object_valid() |
| 2551 | // desc: has a global object been initialized during emit |
| 2552 | // and constructed during runtime? |
| 2553 | //----------------------------------------------------------------------------- |
| 2554 | t_CKBOOL Chuck_Globals_Manager::is_global_object_valid( const std::string & name ) |
| 2555 | { |
| 2556 | return is_global_object_init( name ) && |
| 2557 | !should_call_global_ctor( name, te_globalObject ); |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | |
| 2563 | //----------------------------------------------------------------------------- |
| 2564 | // name: get_global_object() |
| 2565 | // desc: get directly from the vm (internal) |
| 2566 | //----------------------------------------------------------------------------- |
| 2567 | Chuck_Object * Chuck_Globals_Manager::get_global_object( const std::string & name ) |
| 2568 | { |
| 2569 | return m_global_objects[name]->val; |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | |
| 2574 | |
| 2575 | //----------------------------------------------------------------------------- |
| 2576 | // name: get_ptr_to_global_object() |
| 2577 | // desc: get ptr directly from the vm (internal) |
| 2578 | //----------------------------------------------------------------------------- |
| 2579 | Chuck_Object ** Chuck_Globals_Manager::get_ptr_to_global_object( const std::string & name ) |
| 2580 | { |
| 2581 | return &( m_global_objects[name]->val ); |
| 2582 | } |
| 2583 | |
| 2584 | |
| 2585 | |
| 2586 | |
| 2587 | //----------------------------------------------------------------------------- |
| 2588 | // name: get_all_global_variables() | 1.5.1.0 (ge) |
| 2589 | // desc: get list of all global variables as a two-string pair of (type, name) |
| 2590 | //----------------------------------------------------------------------------- |
| 2591 | void Chuck_Globals_Manager::get_all_global_variables( std::vector<Chuck_Globals_TypeValue> & list ) |
| 2592 | { |
| 2593 | // clear the list |
| 2594 | list.clear(); |
| 2595 | |
| 2596 | // int globals |
| 2597 | map<string, Chuck_Global_Int_Container *>::iterator it_int; |
| 2598 | // iterate |
| 2599 | for( it_int = m_global_ints.begin(); it_int != m_global_ints.end(); it_int++ ) |
| 2600 | { |
| 2601 | // append entry |
| 2602 | list.push_back( Chuck_Globals_TypeValue( "int", it_int->first ) ); |
| 2603 | } |
| 2604 | |
| 2605 | // float globals |
| 2606 | map<string, Chuck_Global_Float_Container *>::iterator it_float; |
| 2607 | // iterate |
| 2608 | for( it_float = m_global_floats.begin(); it_float != m_global_floats.end(); it_float++ ) |
| 2609 | { |
| 2610 | // append entry |
| 2611 | list.push_back( Chuck_Globals_TypeValue( "float", it_float->first ) ); |
| 2612 | } |
| 2613 | |
| 2614 | // string globals |
| 2615 | map<string, Chuck_Global_String_Container *>::iterator it_string; |
| 2616 | // iterate |
| 2617 | for( it_string = m_global_strings.begin(); it_string != m_global_strings.end(); it_string++ ) |
| 2618 | { |
| 2619 | // append entry |
| 2620 | list.push_back( Chuck_Globals_TypeValue( "string", it_string->first ) ); |
| 2621 | } |
| 2622 | |
| 2623 | // event globals |
| 2624 | map<string, Chuck_Global_Event_Container *>::iterator it_event; |
| 2625 | // iterate |
| 2626 | for( it_event = m_global_events.begin(); it_event != m_global_events.end(); it_event++ ) |
| 2627 | { |
| 2628 | // container |
| 2629 | Chuck_Global_Event_Container * c = it_event->second; |
| 2630 | // check |
| 2631 | string type = c->type ? c->type->name() : "Event"; |
| 2632 | // append entry |
| 2633 | list.push_back( Chuck_Globals_TypeValue( type, it_event->first ) ); |
| 2634 | } |
| 2635 | |
| 2636 | // ugen globals |
| 2637 | map<string, Chuck_Global_UGen_Container *>::iterator it_ugen; |
| 2638 | // iterate |
| 2639 | for( it_ugen = m_global_ugens.begin(); it_ugen != m_global_ugens.end(); it_ugen++ ) |
| 2640 | { |
| 2641 | // container |
| 2642 | Chuck_Global_UGen_Container * c = it_ugen->second; |
| 2643 | // check |
| 2644 | string type = c->type ? c->type->name() : "UGen"; |
| 2645 | // append entry |
| 2646 | list.push_back( Chuck_Globals_TypeValue( type, it_ugen->first ) ); |
| 2647 | } |
| 2648 | |
| 2649 | // array globals |
| 2650 | map<string, Chuck_Global_Array_Container *>::iterator it_array; |
| 2651 | // iterate |
| 2652 | for( it_array = m_global_arrays.begin(); it_array != m_global_arrays.end(); it_array++ ) |
| 2653 | { |
| 2654 | // container |
| 2655 | Chuck_Global_Array_Container * c = it_array->second; |
| 2656 | // check |
| 2657 | string type = c->type ? c->type->name() : "@array"; |
| 2658 | // append entry |
| 2659 | list.push_back( Chuck_Globals_TypeValue( type, it_array->first ) ); |
| 2660 | } |
| 2661 | |
| 2662 | // object globals |
| 2663 | map<string, Chuck_Global_Object_Container *>::iterator it_object; |
| 2664 | // iterate |
| 2665 | for( it_object = m_global_objects.begin(); it_object != m_global_objects.end(); it_object++ ) |
| 2666 | { |
| 2667 | // container |
| 2668 | Chuck_Global_Object_Container * c = it_object->second; |
| 2669 | // check |
| 2670 | string type = c->type ? c->type->name() : "Object"; |
| 2671 | // append entry |
| 2672 | list.push_back( Chuck_Globals_TypeValue( type, it_object->first ) ); |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | |
| 2679 | //----------------------------------------------------------------------------- |
| 2680 | // name: should_call_global_ctor() |
| 2681 | // desc: ask the vm if a global needs to be constructed after init |
| 2682 | //----------------------------------------------------------------------------- |
| 2683 | t_CKBOOL Chuck_Globals_Manager::should_call_global_ctor( const std::string & name, |
| 2684 | te_GlobalType type ) |
| 2685 | { |
| 2686 | switch( type ) |
| 2687 | { |
| 2688 | case te_globalTypeNone: |
| 2689 | // no type |
| 2690 | case te_globalInt: |
| 2691 | case te_globalFloat: |
| 2692 | case te_globalString: |
| 2693 | // these basic types don't have ctors |
| 2694 | return FALSE; |
| 2695 | break; |
| 2696 | case te_globalEvent: |
| 2697 | return m_global_events.count( name ) > 0 && |
| 2698 | m_global_events[name]->ctor_needs_to_be_called; |
| 2699 | break; |
| 2700 | case te_globalUGen: |
| 2701 | return m_global_ugens.count( name ) > 0 && |
| 2702 | m_global_ugens[name]->ctor_needs_to_be_called; |
| 2703 | break; |
| 2704 | case te_globalArraySymbol: |
| 2705 | // this case is only used for array-as-symbol, not array-as-decl. |
| 2706 | // if arrays need their ctors called, we need a rearchitecture! |
| 2707 | return FALSE; |
| 2708 | break; |
| 2709 | case te_globalObject: |
| 2710 | return m_global_objects.count(name) > 0 && |
| 2711 | m_global_objects[name]->ctor_needs_to_be_called; |
| 2712 | break; |
| 2713 | } |
| 2714 | |
| 2715 | return FALSE; |
| 2716 | } |
| 2717 | |
| 2718 | |
| 2719 | |
| 2720 | |
| 2721 | //----------------------------------------------------------------------------- |
| 2722 | // name: global_ctor_was_called() |
| 2723 | // desc: tell the vm that a global has been constructed after init |
| 2724 | //----------------------------------------------------------------------------- |
| 2725 | void Chuck_Globals_Manager::global_ctor_was_called( const std::string & name, |
| 2726 | te_GlobalType type ) |
| 2727 | { |
| 2728 | switch( type ) |
| 2729 | { |
| 2730 | case te_globalTypeNone: |
| 2731 | // no type |
| 2732 | case te_globalInt: |
| 2733 | case te_globalFloat: |
| 2734 | case te_globalString: |
| 2735 | // do nothing for these basic types |
| 2736 | break; |
| 2737 | case te_globalEvent: |
| 2738 | if( m_global_events.count( name ) > 0 ) |
| 2739 | { |
| 2740 | m_global_events[name]->ctor_needs_to_be_called = FALSE; |
| 2741 | } |
| 2742 | break; |
| 2743 | case te_globalUGen: |
| 2744 | if( m_global_ugens.count( name ) > 0 ) |
| 2745 | { |
| 2746 | m_global_ugens[name]->ctor_needs_to_be_called = FALSE; |
| 2747 | } |
| 2748 | break; |
| 2749 | case te_globalArraySymbol: |
| 2750 | // do nothing |
| 2751 | break; |
| 2752 | case te_globalObject: |
| 2753 | if( m_global_objects.count( name ) > 0 ) |
| 2754 | { |
| 2755 | m_global_objects[name]->ctor_needs_to_be_called = FALSE; |
| 2756 | } |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | |
| 2762 | |
| 2763 | |
| 2764 | //----------------------------------------------------------------------------- |
| 2765 | // name: cleanup_global_variables() |
| 2766 | // desc: set a pointer directly on the vm (internal) |
| 2767 | //----------------------------------------------------------------------------- |
| 2768 | void Chuck_Globals_Manager::cleanup_global_variables() |
| 2769 | { |
| 2770 | // ints: delete containers and clear map |
| 2771 | for( std::map< std::string, Chuck_Global_Int_Container * >::iterator it= |
| 2772 | m_global_ints.begin(); it!=m_global_ints.end(); it++ ) |
| 2773 | { |
| 2774 | delete (it->second); |
| 2775 | } |
| 2776 | m_global_ints.clear(); |
| 2777 | |
| 2778 | // floats: delete containers and clear map |
| 2779 | for( std::map< std::string, Chuck_Global_Float_Container * >::iterator it= |
| 2780 | m_global_floats.begin(); it!=m_global_floats.end(); it++ ) |
| 2781 | { |
| 2782 | delete (it->second); |
| 2783 | } |
| 2784 | m_global_floats.clear(); |
| 2785 | |
| 2786 | // strings: release strings, delete containers, and clear map |
| 2787 | for( std::map< std::string, Chuck_Global_String_Container * >::iterator it= |
| 2788 | m_global_strings.begin(); it!=m_global_strings.end(); it++ ) |
| 2789 | { |
| 2790 | CK_SAFE_RELEASE( it->second->val ); |
| 2791 | delete (it->second); |
| 2792 | } |
| 2793 | m_global_strings.clear(); |
| 2794 | |
| 2795 | // events: release events, delete containers, and clear map |
| 2796 | for( std::map< std::string, Chuck_Global_Event_Container * >::iterator it= |
| 2797 | m_global_events.begin(); it!=m_global_events.end(); it++ ) |
| 2798 | { |
| 2799 | CK_SAFE_RELEASE( it->second->val ); |
| 2800 | delete (it->second); |
| 2801 | } |
| 2802 | m_global_events.clear(); |
| 2803 | |
| 2804 | // ugens: release ugens, delete containers, and clear map |
| 2805 | for( std::map< std::string, Chuck_Global_UGen_Container * >::iterator it= |
| 2806 | m_global_ugens.begin(); it!=m_global_ugens.end(); it++ ) |
| 2807 | { |
| 2808 | CK_SAFE_RELEASE( it->second->val ); |
| 2809 | delete (it->second); |
| 2810 | } |
| 2811 | m_global_ugens.clear(); |
| 2812 | |
| 2813 | // arrays: release arrays, delete containers, and clear map |
| 2814 | for( std::map< std::string, Chuck_Global_Array_Container * >::iterator it= |
| 2815 | m_global_arrays.begin(); it!=m_global_arrays.end(); it++ ) |
| 2816 | { |
| 2817 | // release. array initialization adds a reference, |
| 2818 | // but the release instruction is prevented from being |
| 2819 | // used on all global objects (including arrays) |
| 2820 | CK_SAFE_RELEASE( it->second->array ); |
| 2821 | delete (it->second); |
| 2822 | } |
| 2823 | m_global_arrays.clear(); |
| 2824 | |
| 2825 | // ugens: release objects, delete containers, and clear map |
| 2826 | for( std::map< std::string, Chuck_Global_Object_Container * >::iterator it= |
| 2827 | m_global_objects.begin(); it!=m_global_objects.end(); it++ ) |
| 2828 | { |
| 2829 | CK_SAFE_RELEASE( it->second->val ); |
| 2830 | delete (it->second); |
| 2831 | } |
| 2832 | m_global_objects.clear(); |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | |
| 2837 | |
| 2838 | //----------------------------------------------------------------------------- |
| 2839 | // name: handle_global_queue_messages() |
| 2840 | // desc: update vm with set, get, listen, spork, etc. messages |
| 2841 | //----------------------------------------------------------------------------- |
| 2842 | void Chuck_Globals_Manager::handle_global_queue_messages() |
| 2843 | { |
| 2844 | bool should_retry_this_request = false; |
| 2845 | |
| 2846 | while( m_global_request_queue.more() ) |
| 2847 | { |
| 2848 | should_retry_this_request = false; |
| 2849 | |
| 2850 | Chuck_Global_Request message; |
| 2851 | if( m_global_request_queue.get( & message ) ) |
| 2852 | { |
| 2853 | switch( message.type ) |
| 2854 | { |
| 2855 | case global_request_none: // 1.5.0.1 (ge) added |
| 2856 | // do nothing |
| 2857 | break; |
| 2858 | |
| 2859 | case execute_chuck_msg_request: |
| 2860 | if( message.executeChuckMsgRequest->msg != NULL ) |
| 2861 | { |
| 2862 | // this will automatically delete msg if applicable |
| 2863 | m_vm->process_msg( message.executeChuckMsgRequest->msg ); |
| 2864 | } |
| 2865 | // clean up request storage |
| 2866 | CK_SAFE_DELETE( message.executeChuckMsgRequest ); |
| 2867 | break; |
| 2868 | |
| 2869 | case spork_shred_request: |
| 2870 | // spork the shred! |
| 2871 | m_vm->spork( message.shred ); |
| 2872 | break; |
| 2873 | |
| 2874 | case set_global_int_request: |
| 2875 | // ensure the container exists |
| 2876 | init_global_int( message.setIntRequest->name ); |
| 2877 | // set int |
| 2878 | m_global_ints[message.setIntRequest->name]->val = message.setIntRequest->val; |
| 2879 | // clean up request storage |
| 2880 | CK_SAFE_DELETE( message.setIntRequest ); |
| 2881 | break; |
| 2882 | |
| 2883 | case get_global_int_request: |
| 2884 | // ensure one of the callbacks is not null (union) |
| 2885 | if( message.getIntRequest->cb != NULL ) |
| 2886 | { |
| 2887 | // ensure the value exists |
| 2888 | init_global_int( message.getIntRequest->name ); |
| 2889 | // call the callback with the value |
| 2890 | switch( message.getIntRequest->cb_type ) |
| 2891 | { |
| 2892 | case ck_get_plain: |
| 2893 | message.getIntRequest->cb( m_global_ints[message.getIntRequest->name]->val ); |
| 2894 | break; |
| 2895 | case ck_get_name: |
| 2896 | message.getIntRequest->cb_with_name( message.getIntRequest->name.c_str(), m_global_ints[message.getIntRequest->name]->val ); |
| 2897 | break; |
| 2898 | case ck_get_id: |
| 2899 | message.getIntRequest->cb_with_id( message.getIntRequest->id, m_global_ints[message.getIntRequest->name]->val ); |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | // clean up request storage |
| 2904 | CK_SAFE_DELETE( message.getIntRequest ); |
| 2905 | break; |
| 2906 | |
| 2907 | case set_global_float_request: |
| 2908 | // ensure the container exists |
| 2909 | init_global_float( message.setFloatRequest->name ); |
| 2910 | // set float |
| 2911 | m_global_floats[message.setFloatRequest->name]->val = message.setFloatRequest->val; |
| 2912 | // clean up request storage |
| 2913 | CK_SAFE_DELETE( message.setFloatRequest ); |
| 2914 | break; |
| 2915 | |
| 2916 | case get_global_float_request: |
| 2917 | // ensure one cb is not null (union) |
| 2918 | if( message.getFloatRequest->cb != NULL ) |
| 2919 | { |
| 2920 | // ensure value exists |
| 2921 | init_global_float( message.getFloatRequest->name ); |
| 2922 | // call callback with float |
| 2923 | switch( message.getFloatRequest->cb_type ) |
| 2924 | { |
| 2925 | case ck_get_plain: |
| 2926 | message.getFloatRequest->cb( m_global_floats[message.getFloatRequest->name]->val ); |
| 2927 | break; |
| 2928 | case ck_get_name: |
| 2929 | message.getFloatRequest->cb_with_name( message.getFloatRequest->name.c_str(), m_global_floats[message.getFloatRequest->name]->val ); |
| 2930 | break; |
| 2931 | case ck_get_id: |
| 2932 | message.getFloatRequest->cb_with_id( message.getFloatRequest->id, m_global_floats[message.getFloatRequest->name]->val ); |
| 2933 | break; |
| 2934 | } |
| 2935 | } |
| 2936 | // clean up request storage |
| 2937 | CK_SAFE_DELETE( message.getFloatRequest ); |
| 2938 | break; |
| 2939 | |
| 2940 | case get_global_all_request: |
| 2941 | // ensure one cb is not null (union) |
| 2942 | if( message.getAllRequest->cb != NULL ) |
| 2943 | { |
| 2944 | // the vector |
| 2945 | std::vector<Chuck_Globals_TypeValue> list; |
| 2946 | // get all |
| 2947 | get_all_global_variables( list ); |
| 2948 | // call back |
| 2949 | message.getAllRequest->cb( list, message.getAllRequest->data ); |
| 2950 | } |
| 2951 | // clean up request storage |
| 2952 | CK_SAFE_DELETE( message.getAllRequest ); |
| 2953 | break; |
| 2954 | |
| 2955 | case set_global_string_request: |
| 2956 | // ensure the container exists |
| 2957 | init_global_string( message.setStringRequest->name ); |
| 2958 | // set string |
| 2959 | m_global_strings[message.setStringRequest->name]->val->set( message.setStringRequest->val ); |
| 2960 | // clean up request storage |
| 2961 | CK_SAFE_DELETE( message.setStringRequest ); |
| 2962 | break; |
| 2963 | |
| 2964 | case get_global_string_request: |
| 2965 | // ensure one callback is not null (union) |
| 2966 | if( message.getStringRequest->cb != NULL ) |
| 2967 | { |
| 2968 | // ensure value exists |
| 2969 | init_global_string( message.getStringRequest->name ); |
| 2970 | // call callback with string |
| 2971 | switch( message.getStringRequest->cb_type ) |
| 2972 | { |
| 2973 | case ck_get_plain: |
| 2974 | message.getStringRequest->cb( m_global_strings[message.getStringRequest->name]->val->c_str() ); |
| 2975 | break; |
| 2976 | case ck_get_name: |
| 2977 | message.getStringRequest->cb_with_name( message.getStringRequest->name.c_str(), m_global_strings[message.getStringRequest->name]->val->c_str() ); |
| 2978 | break; |
| 2979 | case ck_get_id: |
| 2980 | message.getStringRequest->cb_with_id( message.getStringRequest->id, m_global_strings[message.getStringRequest->name]->val->c_str() ); |
| 2981 | break; |
| 2982 | } |
| 2983 | } |
| 2984 | // clean up request storage |
| 2985 | CK_SAFE_DELETE( message.getStringRequest ); |
| 2986 | break; |
| 2987 | |
| 2988 | case signal_global_event_request: |
| 2989 | // ensure it exists and it doesn't need its ctor called |
| 2990 | if( m_global_events.count( message.signalEventRequest->name ) > 0 && |
| 2991 | !should_call_global_ctor( message.signalEventRequest->name, te_globalEvent ) ) |
| 2992 | { |
| 2993 | // get the event |
| 2994 | Chuck_Event * event = get_global_event( message.signalEventRequest->name ); |
| 2995 | // signal it, or broadcast it |
| 2996 | if( message.signalEventRequest->is_broadcast ) |
| 2997 | { |
| 2998 | event->broadcast_local(); |
| 2999 | event->broadcast_global(); |
| 3000 | } |
| 3001 | else |
| 3002 | { |
| 3003 | event->signal_local(); |
| 3004 | event->signal_global(); |
| 3005 | } |
| 3006 | } |
| 3007 | else |
| 3008 | { |
| 3009 | // name does not exist. possible that object just doesn't exist yet. |
| 3010 | if( message.retries > 0 ) |
| 3011 | { |
| 3012 | should_retry_this_request = true; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | // clean up request storage |
| 3017 | if( should_retry_this_request ) |
| 3018 | { |
| 3019 | m_global_request_retry_queue.put( message ); |
| 3020 | } |
| 3021 | else |
| 3022 | { |
| 3023 | CK_SAFE_DELETE( message.signalEventRequest ); |
| 3024 | } |
| 3025 | break; |
| 3026 | |
| 3027 | case listen_for_global_event_request: |
| 3028 | // ensure a callback is not null (union) |
| 3029 | if( message.listenForEventRequest->cb != NULL ) |
| 3030 | { |
| 3031 | // ensure it exists |
| 3032 | if( m_global_events.count( message.listenForEventRequest->name ) > 0 ) |
| 3033 | { |
| 3034 | // get event |
| 3035 | Chuck_Event * event = get_global_event( message.listenForEventRequest->name ); |
| 3036 | if( message.listenForEventRequest->deregister ) |
| 3037 | { |
| 3038 | // deregister |
| 3039 | switch( message.listenForEventRequest->cb_type ) |
| 3040 | { |
| 3041 | case ck_get_plain: |
| 3042 | event->remove_listen( message.listenForEventRequest->cb ); |
| 3043 | break; |
| 3044 | case ck_get_name: |
| 3045 | event->remove_listen( message.listenForEventRequest->name, |
| 3046 | message.listenForEventRequest->cb_with_name ); |
| 3047 | break; |
| 3048 | case ck_get_id: |
| 3049 | event->remove_listen( message.listenForEventRequest->id, |
| 3050 | message.listenForEventRequest->cb_with_id ); |
| 3051 | break; |
| 3052 | } |
| 3053 | |
| 3054 | } |
| 3055 | else |
| 3056 | { |
| 3057 | // register |
| 3058 | switch( message.listenForEventRequest->cb_type ) |
| 3059 | { |
| 3060 | case ck_get_plain: |
| 3061 | event->global_listen( message.listenForEventRequest->cb, |
| 3062 | message.listenForEventRequest->listen_forever ); |
| 3063 | break; |
| 3064 | case ck_get_name: |
| 3065 | event->global_listen( message.listenForEventRequest->name, |
| 3066 | message.listenForEventRequest->cb_with_name, |
| 3067 | message.listenForEventRequest->listen_forever ); |
| 3068 | break; |
| 3069 | case ck_get_id: |
| 3070 | event->global_listen( message.listenForEventRequest->id, |
| 3071 | message.listenForEventRequest->cb_with_id, |
| 3072 | message.listenForEventRequest->listen_forever ); |
| 3073 | break; |
| 3074 | } |
| 3075 | |
| 3076 | } |
| 3077 | } |
| 3078 | else |
| 3079 | { |
| 3080 | // name does not exist. possible that object just doesn't exist yet. |
| 3081 | if( message.retries > 0 ) |
| 3082 | { |
| 3083 | should_retry_this_request = true; |
| 3084 | } |
| 3085 | } |
| 3086 | } |
| 3087 | // clean up request storage |
| 3088 | if( should_retry_this_request ) |
| 3089 | { |
| 3090 | m_global_request_retry_queue.put( message ); |
| 3091 | } |
| 3092 | else |
| 3093 | { |
| 3094 | CK_SAFE_DELETE( message.listenForEventRequest ); |
| 3095 | } |
| 3096 | break; |
| 3097 | |
| 3098 | case set_global_int_array_request: |
| 3099 | { |
| 3100 | // replace an entire array, if it exists |
| 3101 | Chuck_Set_Global_Int_Array_Request * request = |
| 3102 | message.setIntArrayRequest; |
| 3103 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3104 | { |
| 3105 | // we know that array's name. does it exist yet? is it an int array? |
| 3106 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3107 | if( array != NULL && |
| 3108 | m_global_arrays[request->name]->array_type == te_globalInt ) |
| 3109 | { |
| 3110 | // it exists! get existing array, new size |
| 3111 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3112 | t_CKUINT newSize = request->arrayValues.size(); |
| 3113 | |
| 3114 | // resize and copy in |
| 3115 | intArray->set_size( newSize ); |
| 3116 | for( int i = 0; i < newSize; i++ ) |
| 3117 | { |
| 3118 | intArray->set( i, request->arrayValues[i] ); |
| 3119 | } |
| 3120 | } |
| 3121 | } |
| 3122 | else |
| 3123 | { |
| 3124 | // name does not exist. possible that object just doesn't exist yet. |
| 3125 | if( message.retries > 0 ) |
| 3126 | { |
| 3127 | should_retry_this_request = true; |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | // clean up request storage |
| 3133 | if( should_retry_this_request ) |
| 3134 | { |
| 3135 | m_global_request_retry_queue.put( message ); |
| 3136 | } |
| 3137 | else |
| 3138 | { |
| 3139 | CK_SAFE_DELETE( message.setIntArrayRequest ); |
| 3140 | } |
| 3141 | break; |
| 3142 | |
| 3143 | case get_global_int_array_request: |
| 3144 | { |
| 3145 | // fetch an entire array, if it exists |
| 3146 | Chuck_Get_Global_Int_Array_Request * request = |
| 3147 | message.getIntArrayRequest; |
| 3148 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3149 | { |
| 3150 | // we know that array's name. does it exist yet; do we have a callback? |
| 3151 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3152 | if( array != NULL && |
| 3153 | m_global_arrays[request->name]->array_type == te_globalInt && |
| 3154 | request->cb != NULL ) |
| 3155 | { |
| 3156 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3157 | // TODO: why is m_vector a vector of unsigned ints...?? |
| 3158 | switch( request->cb_type ) |
| 3159 | { |
| 3160 | case ck_get_plain: |
| 3161 | request->cb( |
| 3162 | (t_CKINT *) &(intArray->m_vector[0]), |
| 3163 | intArray->size() |
| 3164 | ); |
| 3165 | break; |
| 3166 | case ck_get_name: |
| 3167 | request->cb_with_name( |
| 3168 | request->name.c_str(), |
| 3169 | (t_CKINT *) &(intArray->m_vector[0]), |
| 3170 | intArray->size() |
| 3171 | ); |
| 3172 | break; |
| 3173 | case ck_get_id: |
| 3174 | request->cb_with_id( |
| 3175 | request->id, |
| 3176 | (t_CKINT *) &(intArray->m_vector[0]), |
| 3177 | intArray->size() |
| 3178 | ); |
| 3179 | break; |
| 3180 | } |
| 3181 | } |
| 3182 | } |
| 3183 | else |
| 3184 | { |
| 3185 | // name does not exist. possible that object just doesn't exist yet. |
| 3186 | if( message.retries > 0 ) |
| 3187 | { |
| 3188 | should_retry_this_request = true; |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | // clean up request storage |
| 3194 | if( should_retry_this_request ) |
| 3195 | { |
| 3196 | m_global_request_retry_queue.put( message ); |
| 3197 | } |
| 3198 | else |
| 3199 | { |
| 3200 | CK_SAFE_DELETE( message.getIntArrayRequest ); |
| 3201 | } |
| 3202 | break; |
| 3203 | |
| 3204 | case set_global_int_array_value_request: |
| 3205 | { |
| 3206 | // set a single value, if array exists and index in range |
| 3207 | Chuck_Set_Global_Int_Array_Value_Request * request = |
| 3208 | message.setIntArrayValueRequest; |
| 3209 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3210 | { |
| 3211 | // we know that array's name. does it exist yet? is it an int array? |
| 3212 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3213 | if( array != NULL && |
| 3214 | m_global_arrays[request->name]->array_type == te_globalInt ) |
| 3215 | { |
| 3216 | // it exists! get existing array |
| 3217 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3218 | // set! (if index within range) |
| 3219 | if( intArray->size() > request->index ) |
| 3220 | { |
| 3221 | intArray->set( request->index, request->value ); |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | else |
| 3226 | { |
| 3227 | // name does not exist. possible that object just doesn't exist yet. |
| 3228 | if( message.retries > 0 ) |
| 3229 | { |
| 3230 | should_retry_this_request = true; |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | // clean up request storage |
| 3236 | if( should_retry_this_request ) |
| 3237 | { |
| 3238 | m_global_request_retry_queue.put( message ); |
| 3239 | } |
| 3240 | else |
| 3241 | { |
| 3242 | CK_SAFE_DELETE( message.setIntArrayValueRequest ); |
| 3243 | } |
| 3244 | break; |
| 3245 | |
| 3246 | case get_global_int_array_value_request: |
| 3247 | { |
| 3248 | // get a single value, if array exists and index in range |
| 3249 | Chuck_Get_Global_Int_Array_Value_Request * request = |
| 3250 | message.getIntArrayValueRequest; |
| 3251 | // fetch an entire array, if it exists |
| 3252 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3253 | { |
| 3254 | // we know that array's name. does it exist yet; do we have a callback? (union) |
| 3255 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3256 | if( array != NULL && |
| 3257 | m_global_arrays[request->name]->array_type == te_globalInt && |
| 3258 | request->cb != NULL ) |
| 3259 | { |
| 3260 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3261 | // TODO why is it unsigned int storage? |
| 3262 | t_CKUINT result = 0; |
| 3263 | intArray->get( request->index, &result ); |
| 3264 | switch( request->cb_type ) |
| 3265 | { |
| 3266 | case ck_get_plain: |
| 3267 | request->cb( result ); |
| 3268 | break; |
| 3269 | case ck_get_name: |
| 3270 | request->cb_with_name( request->name.c_str(), result ); |
| 3271 | break; |
| 3272 | case ck_get_id: |
| 3273 | request->cb_with_id( request->id, result ); |
| 3274 | break; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | else |
| 3279 | { |
| 3280 | // name does not exist. possible that object just doesn't exist yet. |
| 3281 | if( message.retries > 0 ) |
| 3282 | { |
| 3283 | should_retry_this_request = true; |
| 3284 | } |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | // clean up request storage |
| 3289 | if( should_retry_this_request ) |
| 3290 | { |
| 3291 | m_global_request_retry_queue.put( message ); |
| 3292 | } |
| 3293 | else |
| 3294 | { |
| 3295 | CK_SAFE_DELETE( message.getIntArrayValueRequest ); |
| 3296 | } |
| 3297 | break; |
| 3298 | |
| 3299 | case set_global_associative_int_array_value_request: |
| 3300 | { |
| 3301 | // set a single value by key, if array exists |
| 3302 | Chuck_Set_Global_Associative_Int_Array_Value_Request * request = |
| 3303 | message.setAssociativeIntArrayValueRequest; |
| 3304 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3305 | { |
| 3306 | // we know that array's name. does it exist yet? is it an int array? |
| 3307 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3308 | if( array != NULL && |
| 3309 | m_global_arrays[request->name]->array_type == te_globalInt ) |
| 3310 | { |
| 3311 | // it exists! get existing array |
| 3312 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3313 | // set! |
| 3314 | intArray->set( request->key, request->value ); |
| 3315 | } |
| 3316 | } |
| 3317 | else |
| 3318 | { |
| 3319 | // name does not exist. possible that object just doesn't exist yet. |
| 3320 | if( message.retries > 0 ) |
| 3321 | { |
| 3322 | should_retry_this_request = true; |
| 3323 | } |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | // clean up request storage |
| 3328 | if( should_retry_this_request ) |
| 3329 | { |
| 3330 | m_global_request_retry_queue.put( message ); |
| 3331 | } |
| 3332 | else |
| 3333 | { |
| 3334 | CK_SAFE_DELETE( message.setAssociativeIntArrayValueRequest ); |
| 3335 | } |
| 3336 | break; |
| 3337 | |
| 3338 | case get_global_associative_int_array_value_request: |
| 3339 | { |
| 3340 | // get a single value by key, if array exists and key in map |
| 3341 | Chuck_Get_Global_Associative_Int_Array_Value_Request * request = |
| 3342 | message.getAssociativeIntArrayValueRequest; |
| 3343 | // fetch an entire array, if it exists |
| 3344 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3345 | { |
| 3346 | // we know that array's name. does it exist yet; do we have a callback? (union) |
| 3347 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3348 | if( array != NULL && |
| 3349 | m_global_arrays[request->name]->array_type == te_globalInt && |
| 3350 | request->cb != NULL ) |
| 3351 | { |
| 3352 | Chuck_ArrayInt * intArray = (Chuck_ArrayInt *) array; |
| 3353 | // TODO why is it unsigned int storage? |
| 3354 | t_CKUINT result = 0; |
| 3355 | intArray->get( request->key, &result ); |
| 3356 | switch( request->cb_type ) |
| 3357 | { |
| 3358 | case ck_get_plain: |
| 3359 | request->cb( result ); |
| 3360 | break; |
| 3361 | case ck_get_name: |
| 3362 | request->cb_with_name( request->name.c_str(), result ); |
| 3363 | break; |
| 3364 | case ck_get_id: |
| 3365 | request->cb_with_id( request->id, result ); |
| 3366 | break; |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | else |
| 3371 | { |
| 3372 | // name does not exist. possible that object just doesn't exist yet. |
| 3373 | if( message.retries > 0 ) |
| 3374 | { |
| 3375 | should_retry_this_request = true; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | // clean up request storage |
| 3381 | if( should_retry_this_request ) |
| 3382 | { |
| 3383 | m_global_request_retry_queue.put( message ); |
| 3384 | } |
| 3385 | else |
| 3386 | { |
| 3387 | CK_SAFE_DELETE( message.getAssociativeIntArrayValueRequest ); |
| 3388 | } |
| 3389 | break; |
| 3390 | |
| 3391 | case set_global_float_array_request: |
| 3392 | { |
| 3393 | // replace an entire array, if it exists |
| 3394 | Chuck_Set_Global_Float_Array_Request * request = |
| 3395 | message.setFloatArrayRequest; |
| 3396 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3397 | { |
| 3398 | // we know that array's name. does it exist yet? is it a float array? |
| 3399 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3400 | if( array != NULL && |
| 3401 | m_global_arrays[request->name]->array_type == te_globalFloat ) |
| 3402 | { |
| 3403 | // it exists! get existing array, new size |
| 3404 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3405 | t_CKUINT newSize = request->arrayValues.size(); |
| 3406 | |
| 3407 | // resize and copy in |
| 3408 | floatArray->set_size( newSize ); |
| 3409 | for( int i = 0; i < newSize; i++ ) |
| 3410 | { |
| 3411 | floatArray->set( i, request->arrayValues[i] ); |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | else |
| 3416 | { |
| 3417 | // name does not exist. possible that object just doesn't exist yet. |
| 3418 | if( message.retries > 0 ) |
| 3419 | { |
| 3420 | should_retry_this_request = true; |
| 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | // clean up request storage |
| 3426 | if( should_retry_this_request ) |
| 3427 | { |
| 3428 | m_global_request_retry_queue.put( message ); |
| 3429 | } |
| 3430 | else |
| 3431 | { |
| 3432 | CK_SAFE_DELETE( message.setFloatArrayRequest ); |
| 3433 | } |
| 3434 | break; |
| 3435 | |
| 3436 | case get_global_float_array_request: |
| 3437 | { |
| 3438 | // fetch an entire array, if it exists |
| 3439 | Chuck_Get_Global_Float_Array_Request * request = |
| 3440 | message.getFloatArrayRequest; |
| 3441 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3442 | { |
| 3443 | // we know that array's name. does it exist yet; do we have a callback? (union) |
| 3444 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3445 | if( array != NULL && |
| 3446 | m_global_arrays[request->name]->array_type == te_globalFloat && |
| 3447 | request->cb != NULL ) |
| 3448 | { |
| 3449 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3450 | switch( request->cb_type ) |
| 3451 | { |
| 3452 | case ck_get_plain: |
| 3453 | request->cb( |
| 3454 | &(floatArray->m_vector[0]), |
| 3455 | floatArray->size() |
| 3456 | ); |
| 3457 | break; |
| 3458 | case ck_get_name: |
| 3459 | request->cb_with_name( |
| 3460 | request->name.c_str(), |
| 3461 | &(floatArray->m_vector[0]), |
| 3462 | floatArray->size() |
| 3463 | ); |
| 3464 | break; |
| 3465 | case ck_get_id: |
| 3466 | request->cb_with_id( |
| 3467 | request->id, |
| 3468 | &(floatArray->m_vector[0]), |
| 3469 | floatArray->size() |
| 3470 | ); |
| 3471 | break; |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | else |
| 3476 | { |
| 3477 | // name does not exist. possible that object just doesn't exist yet. |
| 3478 | if( message.retries > 0 ) |
| 3479 | { |
| 3480 | should_retry_this_request = true; |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | // clean up request storage |
| 3486 | if( should_retry_this_request ) |
| 3487 | { |
| 3488 | m_global_request_retry_queue.put( message ); |
| 3489 | } |
| 3490 | else |
| 3491 | { |
| 3492 | CK_SAFE_DELETE( message.getFloatArrayRequest ); |
| 3493 | } |
| 3494 | break; |
| 3495 | |
| 3496 | case set_global_float_array_value_request: |
| 3497 | { |
| 3498 | // set a single value, if array exists and index in range |
| 3499 | Chuck_Set_Global_Float_Array_Value_Request * request = |
| 3500 | message.setFloatArrayValueRequest; |
| 3501 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3502 | { |
| 3503 | // we know that array's name. does it exist yet? is it a float array? |
| 3504 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3505 | if( array != NULL && |
| 3506 | m_global_arrays[request->name]->array_type == te_globalFloat ) |
| 3507 | { |
| 3508 | // it exists! get existing array |
| 3509 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3510 | // set! (if index within range) |
| 3511 | if( floatArray->size() > request->index ) |
| 3512 | { |
| 3513 | floatArray->set( request->index, request->value ); |
| 3514 | } |
| 3515 | } |
| 3516 | } |
| 3517 | else |
| 3518 | { |
| 3519 | // name does not exist. possible that object just doesn't exist yet. |
| 3520 | if( message.retries > 0 ) |
| 3521 | { |
| 3522 | should_retry_this_request = true; |
| 3523 | } |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | // clean up request storage |
| 3528 | if( should_retry_this_request ) |
| 3529 | { |
| 3530 | m_global_request_retry_queue.put( message ); |
| 3531 | } |
| 3532 | else |
| 3533 | { |
| 3534 | CK_SAFE_DELETE( message.setFloatArrayValueRequest ); |
| 3535 | } |
| 3536 | break; |
| 3537 | |
| 3538 | case get_global_float_array_value_request: |
| 3539 | { |
| 3540 | // get a single value, if array exists and index in range |
| 3541 | Chuck_Get_Global_Float_Array_Value_Request * request = |
| 3542 | message.getFloatArrayValueRequest; |
| 3543 | // fetch an entire array, if it exists |
| 3544 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3545 | { |
| 3546 | // we know that array's name. does it exist yet; do we have a callback? (union) |
| 3547 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3548 | if( array != NULL && |
| 3549 | m_global_arrays[request->name]->array_type == te_globalFloat && |
| 3550 | request->cb != NULL ) |
| 3551 | { |
| 3552 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3553 | t_CKFLOAT result = 0; |
| 3554 | floatArray->get( request->index, &result ); |
| 3555 | switch( request->cb_type ) |
| 3556 | { |
| 3557 | case ck_get_plain: |
| 3558 | request->cb( result ); |
| 3559 | break; |
| 3560 | case ck_get_name: |
| 3561 | request->cb_with_name( request->name.c_str(), result ); |
| 3562 | break; |
| 3563 | case ck_get_id: |
| 3564 | request->cb_with_id( request->id, result ); |
| 3565 | break; |
| 3566 | } |
| 3567 | } |
| 3568 | } |
| 3569 | else |
| 3570 | { |
| 3571 | // name does not exist. possible that object just doesn't exist yet. |
| 3572 | if( message.retries > 0 ) |
| 3573 | { |
| 3574 | should_retry_this_request = true; |
| 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | // clean up request storage |
| 3580 | if( should_retry_this_request ) |
| 3581 | { |
| 3582 | m_global_request_retry_queue.put( message ); |
| 3583 | } |
| 3584 | else |
| 3585 | { |
| 3586 | CK_SAFE_DELETE( message.getFloatArrayValueRequest ); |
| 3587 | } |
| 3588 | break; |
| 3589 | |
| 3590 | case set_global_associative_float_array_value_request: |
| 3591 | { |
| 3592 | // set a single value by key, if array exists |
| 3593 | Chuck_Set_Global_Associative_Float_Array_Value_Request * request = |
| 3594 | message.setAssociativeFloatArrayValueRequest; |
| 3595 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3596 | { |
| 3597 | // we know that array's name. does it exist yet? is it a float array? |
| 3598 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3599 | if( array != NULL && |
| 3600 | m_global_arrays[request->name]->array_type == te_globalFloat ) |
| 3601 | { |
| 3602 | // it exists! get existing array |
| 3603 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3604 | // set! |
| 3605 | floatArray->set( request->key, request->value ); |
| 3606 | } |
| 3607 | } |
| 3608 | else |
| 3609 | { |
| 3610 | // name does not exist. possible that object just doesn't exist yet. |
| 3611 | if( message.retries > 0 ) |
| 3612 | { |
| 3613 | should_retry_this_request = true; |
| 3614 | } |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | // clean up request storage |
| 3619 | if( should_retry_this_request ) |
| 3620 | { |
| 3621 | m_global_request_retry_queue.put( message ); |
| 3622 | } |
| 3623 | else |
| 3624 | { |
| 3625 | CK_SAFE_DELETE( message.setAssociativeFloatArrayValueRequest ); |
| 3626 | } |
| 3627 | break; |
| 3628 | |
| 3629 | case get_global_associative_float_array_value_request: |
| 3630 | { |
| 3631 | // get a single value by key, if array exists and key in map |
| 3632 | Chuck_Get_Global_Associative_Float_Array_Value_Request * request = |
| 3633 | message.getAssociativeFloatArrayValueRequest; |
| 3634 | // fetch an entire array, if it exists |
| 3635 | if( m_global_arrays.count( request->name ) > 0 ) |
| 3636 | { |
| 3637 | // we know that array's name. does it exist yet; do we have a callback? (union) |
| 3638 | Chuck_Object * array = m_global_arrays[request->name]->array; |
| 3639 | if( array != NULL && |
| 3640 | m_global_arrays[request->name]->array_type == te_globalFloat && |
| 3641 | request->cb != NULL ) |
| 3642 | { |
| 3643 | Chuck_ArrayFloat * floatArray = (Chuck_ArrayFloat *) array; |
| 3644 | t_CKFLOAT result = 0; |
| 3645 | floatArray->get( request->key, &result ); |
| 3646 | switch( request->cb_type ) |
| 3647 | { |
| 3648 | case ck_get_plain: |
| 3649 | request->cb( result ); |
| 3650 | break; |
| 3651 | case ck_get_name: |
| 3652 | request->cb_with_name( request->name.c_str(), result ); |
| 3653 | break; |
| 3654 | case ck_get_id: |
| 3655 | request->cb_with_id( request->id, result ); |
| 3656 | break; |
| 3657 | } |
| 3658 | } |
| 3659 | } |
| 3660 | else |
| 3661 | { |
| 3662 | // name does not exist. possible that object just doesn't exist yet. |
| 3663 | if( message.retries > 0 ) |
| 3664 | { |
| 3665 | should_retry_this_request = true; |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | // clean up request storage |
| 3671 | if( should_retry_this_request ) |
| 3672 | { |
| 3673 | m_global_request_retry_queue.put( message ); |
| 3674 | } |
| 3675 | else |
| 3676 | { |
| 3677 | CK_SAFE_DELETE( message.getAssociativeFloatArrayValueRequest ); |
| 3678 | } |
| 3679 | break; |
| 3680 | } |
| 3681 | } |
| 3682 | else |
| 3683 | { |
| 3684 | // get failed. problem? |
| 3685 | break; |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | // process retries |
| 3690 | while( m_global_request_retry_queue.more() ) |
| 3691 | { |
| 3692 | // get the request |
| 3693 | Chuck_Global_Request message; |
| 3694 | if( m_global_request_retry_queue.get( &message ) ) |
| 3695 | { |
| 3696 | // make sure it should be retried |
| 3697 | if( message.retries == 0 ) |
| 3698 | { |
| 3699 | // trying to retry something that shouldn't be. |
| 3700 | continue; |
| 3701 | } |
| 3702 | |
| 3703 | // decrement the number of retries |
| 3704 | message.retries--; |
| 3705 | // add it back to the queue for next time |
| 3706 | m_global_request_queue.put( message ); |
| 3707 | } |
| 3708 | else |
| 3709 | { |
| 3710 | // get failed. problem? |
| 3711 | break; |
| 3712 | } |
| 3713 | } |
| 3714 | } |