github.com/256dpi/max-go@v0.7.0/lib/max/ext_dictionary.h (about) 1 // ext_dictionary.h -- copyright 2012 Cycling '74 -- All rights reserved 2 3 #ifndef __EXT_DICTIONARY_H__ 4 #define __EXT_DICTIONARY_H__ 5 6 #include "ext_prefix.h" 7 8 #include "max_types.h" 9 #include "ext_hashtab.h" 10 #include "ext_mess.h" 11 12 BEGIN_USING_C_LINKAGE 13 14 #if C74_PRAGMA_STRUCT_PACKPUSH 15 #pragma pack(push, 2) 16 #elif C74_PRAGMA_STRUCT_PACK 17 #pragma pack(2) 18 #endif 19 20 21 /** A dictionary entry. This struct is provided for debugging convenience, 22 but should be considered opaque and is subject to change without notice. 23 24 @ingroup dictionary 25 @see t_dictionary 26 */ 27 typedef struct _dictionary_entry { 28 t_object e_obj; 29 t_symbol *e_key; // redundant with hashtab, but allows getting key during linklist traversal 30 t_atom e_value; 31 long e_flags; 32 } t_dictionary_entry; 33 34 35 /** The dictionary object. This struct is provided for debugging convenience, 36 but should be considered opaque and is subject to change without notice. 37 38 @ingroup dictionary 39 @see t_dictionary 40 */ 41 typedef struct _dictionary 42 { 43 t_object d_obj; 44 t_hashtab *d_hashtab; 45 t_linklist *d_linklist; 46 void *d_transaction_mutex; // currently having header inclusion issues this is a t_systhread_mutex 47 char d_use_transaction; // always lock transaction mutex with low level dictionary calls 48 } t_dictionary; 49 50 #if C74_PRAGMA_STRUCT_PACKPUSH 51 #pragma pack(pop) 52 #elif C74_PRAGMA_STRUCT_PACK 53 #pragma pack() 54 #endif 55 56 57 /** 58 Create a new dictionary object. 59 You can free the dictionary by calling object_free(). 60 However, you should keep in mind the guidelines provided in @ref when_to_free_a_dictionary. 61 62 @ingroup dictionary 63 @return Pointer to the new dictionary object. 64 65 @see object_free() 66 */ 67 t_dictionary* dictionary_new(void); 68 69 /** 70 Add a long integer value to the dictionary. 71 72 @ingroup dictionary 73 @param d The dictionary instance. 74 @param key The name of the key used to index the new value. 75 All keys must be unique. If the key name already exists, 76 then the existing value associated with the key will be freed prior to the new value's assignment. 77 @param value The new value to append to the dictionary. 78 @return A Max error code. 79 */ 80 t_max_err dictionary_appendlong(t_dictionary *d, t_symbol *key, t_atom_long value); 81 82 83 /** 84 Add a double-precision float value to the dictionary. 85 86 @ingroup dictionary 87 @param d The dictionary instance. 88 @param key The name of the key used to index the new value. 89 All keys must be unique. If the key name already exists, 90 then the existing value associated with the key will be freed prior to the new value's assignment. 91 @param value The new value to append to the dictionary. 92 @return A Max error code. 93 */ 94 t_max_err dictionary_appendfloat(t_dictionary *d, t_symbol *key, double value); 95 96 97 /** 98 Add a #t_symbol* value to the dictionary. 99 100 @ingroup dictionary 101 @param d The dictionary instance. 102 @param key The name of the key used to index the new value. 103 All keys must be unique. If the key name already exists, 104 then the existing value associated with the key will be freed prior to the new value's assignment. 105 @param value The new value to append to the dictionary. 106 @return A Max error code. 107 */ 108 t_max_err dictionary_appendsym(t_dictionary *d, t_symbol *key, t_symbol *value); 109 110 111 /** 112 Add a #t_atom* value to the dictionary. 113 114 @ingroup dictionary 115 @param d The dictionary instance. 116 @param key The name of the key used to index the new value. 117 All keys must be unique. If the key name already exists, 118 then the existing value associated with the key will be freed prior to the new value's assignment. 119 @param value The new value to append to the dictionary. 120 @return A Max error code. 121 */ 122 t_max_err dictionary_appendatom(t_dictionary *d, t_symbol *key, t_atom *value); 123 124 125 // private 126 t_max_err dictionary_appendattribute(t_dictionary *d, t_symbol *key, t_symbol *attrname, t_object *obj); 127 128 129 /** 130 Add a C-string to the dictionary. Internally this uses the #t_string object. 131 It is useful to use the #t_string in dictionaries rather than the #t_symbol 132 to avoid bloating Max's symbol table unnecessarily. 133 134 @ingroup dictionary 135 @param d The dictionary instance. 136 @param key The name of the key used to index the new value. 137 All keys must be unique. If the key name already exists, 138 then the existing value associated with the key will be freed prior to the new value's assignment. 139 @param value The new value to append to the dictionary. 140 @return A Max error code. 141 */ 142 t_max_err dictionary_appendstring(t_dictionary *d, t_symbol *key, const char *value); 143 144 145 /** 146 Add an array of atoms to the dictionary. 147 Internally these atoms will be copied into a #t_atomarray object, which will be appended to the dictionary 148 with the given key. 149 150 @ingroup dictionary 151 @param d The dictionary instance. 152 @param key The name of the key used to index the new value. 153 All keys must be unique. If the key name already exists, 154 then the existing value associated with the key will be freed prior to the new value's assignment. 155 @param argc The number of atoms to append to the dictionary. 156 @param argv The address of the first atom in the array to append to the dictionary. 157 @return A Max error code. 158 */ 159 t_max_err dictionary_appendatoms(t_dictionary *d, t_symbol *key, long argc, t_atom *argv); 160 161 // internal use only 162 t_max_err dictionary_appendatoms_flags(t_dictionary *d, t_symbol *key, long argc, t_atom *argv, long flags); 163 164 165 /** 166 Add an @ref atomarray object to the dictionary. 167 Note that from this point on that you should not free the #t_atomarray*, because the atomarray is now owned by 168 the dictionary, and freeing the dictionary will free the atomarray as discussed in @ref when_to_free_a_dictionary. 169 170 @ingroup dictionary 171 @param d The dictionary instance. 172 @param key The name of the key used to index the new value. 173 All keys must be unique. If the key name already exists, 174 then the existing value associated with the key will be freed prior to the new value's assignment. 175 @param value The new value to append to the dictionary. 176 @return A Max error code. 177 */ 178 t_max_err dictionary_appendatomarray(t_dictionary *d, t_symbol *key, t_object *value); 179 180 181 /** 182 Add a dictionary object to the dictionary. 183 Note that from this point on that you should not free the #t_dictionary* that is being added, 184 because the newly-added dictionary is now owned by the dictionary to which it has been added, 185 as discussed in @ref when_to_free_a_dictionary. 186 187 @ingroup dictionary 188 @param d The dictionary instance. 189 @param key The name of the key used to index the new value. 190 All keys must be unique. If the key name already exists, 191 then the existing value associated with the key will be freed prior to the new value's assignment. 192 @param value The new value to append to the dictionary. 193 @return A Max error code. 194 */ 195 t_max_err dictionary_appenddictionary(t_dictionary *d, t_symbol *key, t_object *value); 196 197 198 /** 199 Add an object to the dictionary. 200 Note that from this point on that you should not free the #t_object* that is being added, 201 because the newly-added object is now owned by the dictionary to which it has been added, 202 as discussed in @ref when_to_free_a_dictionary. 203 204 @ingroup dictionary 205 @param d The dictionary instance. 206 @param key The name of the key used to index the new value. 207 All keys must be unique. If the key name already exists, 208 then the existing value associated with the key will be freed prior to the new value's assignment. 209 @param value The new value to append to the dictionary. 210 @return A Max error code. 211 */ 212 t_max_err dictionary_appendobject(t_dictionary *d, t_symbol *key, t_object *value); 213 214 215 t_max_err dictionary_appendobject_flags(t_dictionary *d, t_symbol *key, t_object *value, long flags); 216 217 218 t_max_err dictionary_appendbinbuf(t_dictionary *d, t_symbol *key, void *value); 219 220 221 /** 222 Retrieve a long integer from the dictionary. 223 224 @ingroup dictionary 225 @param d The dictionary instance. 226 @param key The key associated with the value to lookup. 227 @param value The address of variable to hold the value associated with the key. 228 @return A Max error code. 229 */ 230 t_max_err dictionary_getlong(C74_CONST t_dictionary *d, t_symbol *key, t_atom_long *value); 231 232 233 /** 234 Retrieve a double-precision float from the dictionary. 235 236 @ingroup dictionary 237 @param d The dictionary instance. 238 @param key The key associated with the value to lookup. 239 @param value The address of variable to hold the value associated with the key. 240 @return A Max error code. 241 */ 242 t_max_err dictionary_getfloat(C74_CONST t_dictionary *d, t_symbol *key, double *value); 243 244 245 /** 246 Retrieve a #t_symbol* from the dictionary. 247 248 @ingroup dictionary 249 @param d The dictionary instance. 250 @param key The key associated with the value to lookup. 251 @param value The address of variable to hold the value associated with the key. 252 @return A Max error code. 253 */ 254 t_max_err dictionary_getsym(C74_CONST t_dictionary *d, t_symbol *key, t_symbol **value); 255 256 257 /** 258 Copy a #t_atom from the dictionary. 259 260 @ingroup dictionary 261 @param d The dictionary instance. 262 @param key The key associated with the value to lookup. 263 @param value The address of variable to hold the value associated with the key. 264 @return A Max error code. 265 */ 266 t_max_err dictionary_getatom(C74_CONST t_dictionary *d, t_symbol *key, t_atom *value); 267 268 269 // private 270 t_max_err dictionary_getattribute(const t_dictionary *d, t_symbol *key, t_symbol *attrname, t_object *obj); 271 272 273 /** 274 Retrieve a C-string pointer from the dictionary. 275 The retrieved pointer references the string in the dictionary, it is not a copy. 276 277 @ingroup dictionary 278 @param d The dictionary instance. 279 @param key The key associated with the value to lookup. 280 @param value The address of variable to hold the value associated with the key. 281 @return A Max error code. 282 */ 283 t_max_err dictionary_getstring(C74_CONST t_dictionary *d, t_symbol *key, const char **value); 284 285 286 /** 287 Retrieve the address of a #t_atom array of in the dictionary. 288 The retrieved pointer references the t_atoms in the dictionary. 289 To fetch a copy of the t_atoms from the dictionary, use dictionary_copyatoms(). 290 291 @ingroup dictionary 292 @param d The dictionary instance. 293 @param key The key associated with the value to lookup. 294 @param argc The address of a variable to hold the number of atoms in the array. 295 @param argv The address of a variable to hold a pointer to the first atom in the array. 296 @return A Max error code. 297 298 @see dictionary_copyatoms() 299 @see dictionary_getatoms_ext() 300 */ 301 t_max_err dictionary_getatoms(C74_CONST t_dictionary *d, t_symbol *key, long *argc, t_atom **argv); 302 303 /** 304 Retrieve the address of a #t_atom array in the dictionary. 305 The retrieved pointer references the t_atoms in the dictionary. 306 Optionally convert strings to symbols. 307 To fetch a copy of the t_atoms from the dictionary, use dictionary_copyatoms(). 308 309 @ingroup dictionary 310 @param d The dictionary instance. 311 @param key The key associated with the value to lookup. 312 @param stringstosymbols The flag to convert strings to symbols (true,false). 313 @param argc The address of a variable to hold the number of atoms in the array. 314 @param argv The address of a variable to hold a pointer to the first atom in the array. 315 @return A Max error code. 316 317 @see dictionary_copyatoms() 318 @see dictionary_getatoms() 319 */ 320 t_max_err dictionary_getatoms_ext(const t_dictionary *d, t_symbol *key, long stringstosymbols, long *argc, t_atom **argv); 321 322 /** 323 Retrieve copies of a #t_atom array in the dictionary. 324 The retrieved pointer of t_atoms in the dictionary has memory allocated and copied to it from within the function. 325 You are responsible for freeing it with sysmem_freeptr(). 326 327 @ingroup dictionary 328 @param d The dictionary instance. 329 @param key The key associated with the value to lookup. 330 @param argc The address of a variable to hold the number of atoms in the array. 331 @param argv The address of a variable to hold a pointer to the first atom in the array. 332 You should initialize this pointer to NULL prior to passing it to dictionary_copyatoms(). 333 @return A Max error code. 334 335 @see dictionary_getatoms() 336 */ 337 t_max_err dictionary_copyatoms(C74_CONST t_dictionary *d, t_symbol *key, long *argc, t_atom **argv); 338 339 340 /** 341 Retrieve a #t_atomarray pointer from the dictionary. 342 343 @ingroup dictionary 344 @param d The dictionary instance. 345 @param key The key associated with the value to lookup. 346 @param value The address of variable to hold the value associated with the key. 347 @return A Max error code. 348 */ 349 t_max_err dictionary_getatomarray(C74_CONST t_dictionary *d, t_symbol *key, t_object **value); 350 351 352 /** 353 Retrieve a #t_dictionary pointer from the dictionary. 354 355 @ingroup dictionary 356 @param d The dictionary instance. 357 @param key The key associated with the value to lookup. 358 @param value The address of variable to hold the value associated with the key. 359 @return A Max error code. 360 */ 361 t_max_err dictionary_getdictionary(C74_CONST t_dictionary *d, t_symbol *key, t_object **value); 362 363 /** 364 Retrieve the address of a #t_atom array of in the dictionary within nested dictionaries. 365 The address can index into nested dictionaries using the '::' operator. For example, 366 the key "field::subfield" will look for the value at key "field" and then look for the 367 value "subfield" in the value found at "field". 368 369 @ingroup dictionary 370 @param d The dictionary instance. 371 @param key The key associated with the value to lookup. 372 @param ac The number of return values 373 @param av The return values 374 @param errstr An error message if an error code was returned. Optional, pass NULL if you don't want it. 375 @return A Max error code. 376 */ 377 t_max_err dictionary_get_ex(t_dictionary *d, t_symbol *key, long *ac, t_atom **av, char *errstr); 378 379 380 /** 381 Retrieve a #t_object pointer from the dictionary. 382 383 @ingroup dictionary 384 @param d The dictionary instance. 385 @param key The key associated with the value to lookup. 386 @param value The address of variable to hold the value associated with the key. 387 @return A Max error code. 388 */ 389 t_max_err dictionary_getobject(C74_CONST t_dictionary *d, t_symbol *key, t_object **value); 390 391 392 /** 393 Test a key to set if the data stored with that key contains a #t_string object. 394 395 @ingroup dictionary 396 @param d The dictionary instance. 397 @param key The key associated with the value to test. 398 @return Returns true if the key contains a #t_string, otherwise returns false. 399 */ 400 long dictionary_entryisstring(C74_CONST t_dictionary *d, t_symbol *key); 401 402 403 /** 404 Test a key to set if the data stored with that key contains a #t_atomarray object. 405 406 @ingroup dictionary 407 @param d The dictionary instance. 408 @param key The key associated with the value to test. 409 @return Returns true if the key contains a #t_atomarray, otherwise returns false. 410 */ 411 long dictionary_entryisatomarray(C74_CONST t_dictionary *d, t_symbol *key); 412 413 414 /** 415 Test a key to set if the data stored with that key contains a #t_dictionary object. 416 417 @ingroup dictionary 418 @param d The dictionary instance. 419 @param key The key associated with the value to test. 420 @return Returns true if the key contains a #t_dictionary, otherwise returns false. 421 */ 422 long dictionary_entryisdictionary(C74_CONST t_dictionary *d, t_symbol *key); 423 424 425 /** 426 Test a key to set if it exists in the dictionary. 427 428 @ingroup dictionary 429 @param d The dictionary instance. 430 @param key The key associated with the value to test. 431 @return Returns true if the key exists, otherwise returns false. 432 */ 433 long dictionary_hasentry(C74_CONST t_dictionary *d, t_symbol *key); 434 435 436 /** 437 Return the number of keys in a dictionary. 438 439 @ingroup dictionary 440 @param d The dictionary instance. 441 @return The number of keys in the dictionary. 442 */ 443 t_atom_long dictionary_getentrycount(C74_CONST t_dictionary *d); 444 445 446 /** 447 Retrieve all of the key names stored in a dictionary. 448 449 The numkeys and keys parameters should be initialized to zero. 450 The dictionary_getkeys() method will allocate memory for the keys it returns. 451 You are then responsible for freeing this memory using dictionary_freekeys(). 452 <em>You must use dictionary_freekeys(), not some other method for freeing the memory.</em> 453 454 @ingroup dictionary 455 @param d The dictionary instance. 456 @param numkeys The address of a long where the number of keys retrieved will be set. 457 @param keys The address of the first of an array #t_symbol pointers where the retrieved keys will be set. 458 @return A max error code. 459 460 @remark The following example demonstrates fetching all of the keys from a dictionary named 'd' 461 in order to iterate through each item stored in the dictionary. 462 @code 463 t_symbol **keys = NULL; 464 long numkeys = 0; 465 long i; 466 t_object *anItem; 467 468 dictionary_getkeys(d, &numkeys, &keys); 469 for(i=0; i<numkeys; i++){ 470 // do something with the keys... 471 } 472 if(keys) 473 dictionary_freekeys(d, numkeys, keys); 474 @endcode 475 476 @see dictionary_freekeys() 477 */ 478 t_max_err dictionary_getkeys(C74_CONST t_dictionary *d, long *numkeys, t_symbol ***keys); 479 t_max_err dictionary_getkeys_ordered(C74_CONST t_dictionary *d, long *numkeys, t_symbol ***keys); 480 481 482 /** 483 Free memory allocated by the dictionary_getkeys() method. 484 485 @ingroup dictionary 486 @param d The dictionary instance. 487 @param numkeys The address of a long where the number of keys retrieved will be set. 488 @param keys The address of the first of an array #t_symbol pointers where the retrieved keys will be set. 489 @return A max error code. 490 491 @see dictionary_getkeys() 492 */ 493 void dictionary_freekeys(t_dictionary *d, long numkeys, t_symbol **keys); 494 495 496 /** 497 Remove a value from the dictionary. 498 This method will free the object in the dictionary. 499 If freeing the object is inappropriate or undesirable, use dictionary_chuckentry() instead. 500 501 @ingroup dictionary 502 @param d The dictionary instance. 503 @param key The key associated with the value to delete. 504 @return A max error code. 505 506 @see dictionary_chuckentry() 507 @see dictionary_clear() 508 */ 509 t_max_err dictionary_deleteentry(t_dictionary *d, t_symbol *key); 510 511 512 /** 513 Remove a value from the dictionary without freeing it. 514 515 @ingroup dictionary 516 @param d The dictionary instance. 517 @param key The key associated with the value to delete. 518 @return A max error code. 519 520 @see dictionary_deleteentry() 521 */ 522 t_max_err dictionary_chuckentry(t_dictionary *d, t_symbol *key); // remove a value from the dictionary without deleting it 523 524 525 /** 526 Delete all values from a dictionary. 527 This method will free the objects in the dictionary. 528 If freeing the objects is inappropriate or undesirable then you should iterate through 529 the dictionary and use dictionary_chuckentry() instead. 530 531 @ingroup dictionary 532 @param d The dictionary instance. 533 @return A max error code. 534 535 @see dictionary_getkeys() 536 @see dictionary_chuckentry() 537 @see dictionary_deleteentry() 538 */ 539 t_max_err dictionary_clear(t_dictionary *d); 540 541 542 543 t_dictionary *dictionary_clone(t_dictionary *d); 544 t_max_err dictionary_clone_to_existing(const t_dictionary *d, t_dictionary *dc); 545 t_max_err dictionary_copy_to_existing(const t_dictionary *d, t_dictionary *dc); 546 t_max_err dictionary_merge_to_existing(const t_dictionary *d, t_dictionary *dc); 547 t_max_err dictionary_copy_nonunique_to_existing(const t_dictionary *d, t_dictionary *dc); 548 549 550 // funall will pass the t_dictionary_entry pointer to the fun 551 // use the methods below to access the fields 552 553 /** 554 Call the specified function for every entry in the dictionary. 555 556 @ingroup dictionary 557 @param d The dictionary instance. 558 @param fun The function to call, specified as function pointer cast to a Max #method. 559 @param arg An argument that you would like to pass to the function being called. 560 561 @remark The dictionary_funall() method will call your function for every entry in the dictionary. 562 It will pass both a pointer to the #t_dictionary_entry, and any argument that you provide. 563 The following example shows a function that could be called by dictionary_funall(). 564 @code 565 void my_function(t_dictionary_entry *entry, void* my_arg) 566 { 567 t_symbol *key; 568 t_atom value; 569 570 key = dictionary_entry_getkey(entry); 571 dictionary_entry_getvalue(entry, &value); 572 573 // do something with key, value, and my_arg... 574 } 575 @endcode 576 @see dictionary_entry_getkey() 577 @see dictionary_entry_getvalue() 578 */ 579 void dictionary_funall(t_dictionary *d, method fun, void *arg); 580 581 582 /** 583 Given a #t_dictionary_entry*, return the key associated with that entry. 584 585 @ingroup dictionary 586 @param x The dictionary entry. 587 @return The key associated with the entry. 588 589 @see dictionary_entry_getvalue() 590 @see dictionary_funall() 591 */ 592 t_symbol* dictionary_entry_getkey(t_dictionary_entry *x); 593 594 595 /** 596 Given a #t_dictionary_entry*, return the value associated with that entry. 597 598 @ingroup dictionary 599 @param x The dictionary entry. 600 @param value The address of a #t_atom to which the value will be copied. 601 602 @see dictionary_entry_getkey() 603 @see dictionary_funall() 604 */ 605 void dictionary_entry_getvalue(t_dictionary_entry *x, t_atom *value); 606 607 /** 608 Given a #t_dictionary_entry*, return the values associated with that entry. 609 610 @ingroup dictionary 611 @param x The dictionary entry. 612 @param argc The length of the returned #t_atom vector. 613 @param argv The address of a #t_atom vector to which the values will be copied. 614 615 @see dictionary_entry_getkey() 616 @see dictionary_funall() 617 */ 618 void dictionary_entry_getvalues(t_dictionary_entry *x, long *argc, t_atom **argv); 619 620 621 622 /** 623 Given 2 dictionaries, copy the keys unique to one of the dictionaries to the other dictionary. 624 625 @ingroup dictionary 626 @param d A dictionary instance. This will be the destination for any values that are copied. 627 @param copyfrom A dictionary instance from which we will copy any values with unique keys. 628 @return A Max error code. 629 630 @see dictionary_copyentries() 631 */ 632 t_max_err dictionary_copyunique(t_dictionary *d, t_dictionary *copyfrom); 633 634 635 636 /** 637 Retrieve a long integer from the dictionary. 638 If the named key doesn't exist, then return a specified default value. 639 640 @ingroup dictionary 641 @param d The dictionary instance. 642 @param key The key associated with the value to lookup. 643 @param value The address of variable to hold the value associated with the key. 644 @param def The default value to return in the absence of the key existing in the dictionary. 645 @return A Max error code. 646 647 @see dictionary_getlong() 648 */ 649 t_max_err dictionary_getdeflong(const t_dictionary *d, t_symbol *key, t_atom_long *value, t_atom_long def); 650 651 652 /** 653 Retrieve a double-precision float from the dictionary. 654 If the named key doesn't exist, then return a specified default value. 655 656 @ingroup dictionary 657 @param d The dictionary instance. 658 @param key The key associated with the value to lookup. 659 @param value The address of variable to hold the value associated with the key. 660 @param def The default value to return in the absence of the key existing in the dictionary. 661 @return A Max error code. 662 663 @see dictionary_getfloat() 664 */ 665 t_max_err dictionary_getdeffloat(const t_dictionary *d, t_symbol *key, double *value, double def); 666 667 668 /** 669 Retrieve a #t_symbol* from the dictionary. 670 If the named key doesn't exist, then return a specified default value. 671 672 @ingroup dictionary 673 @param d The dictionary instance. 674 @param key The key associated with the value to lookup. 675 @param value The address of variable to hold the value associated with the key. 676 @param def The default value to return in the absence of the key existing in the dictionary. 677 @return A Max error code. 678 679 @see dictionary_getsym() 680 */ 681 t_max_err dictionary_getdefsym(const t_dictionary *d, t_symbol *key, t_symbol **value, t_symbol *def); 682 683 684 /** 685 Retrieve a #t_atom* from the dictionary. 686 If the named key doesn't exist, then return a specified default value. 687 688 @ingroup dictionary 689 @param d The dictionary instance. 690 @param key The key associated with the value to lookup. 691 @param value The address of variable to hold the value associated with the key. 692 @param def The default value to return in the absence of the key existing in the dictionary. 693 @return A Max error code. 694 695 @see dictionary_getatom() 696 */ 697 t_max_err dictionary_getdefatom(const t_dictionary *d, t_symbol *key, t_atom *value, t_atom *def); 698 699 700 /** 701 Retrieve a C-string from the dictionary. 702 If the named key doesn't exist, then return a specified default value. 703 704 @ingroup dictionary 705 @param d The dictionary instance. 706 @param key The key associated with the value to lookup. 707 @param value The address of variable to hold the value associated with the key. 708 @param def The default value to return in the absence of the key existing in the dictionary. 709 @return A Max error code. 710 711 @see dictionary_getstring() 712 */ 713 t_max_err dictionary_getdefstring(const t_dictionary *d, t_symbol *key, const char **value, char *def); 714 715 716 /** 717 Retrieve the address of a #t_atom array of in the dictionary. 718 The retrieved pointer references the t_atoms in the dictionary. 719 To fetch a copy of the t_atoms from the dictionary, use dictionary_copyatoms(). 720 If the named key doesn't exist, then return a default array of atoms, specified as a #t_atomarray*. 721 722 @ingroup dictionary 723 @param d The dictionary instance. 724 @param key The key associated with the value to lookup. 725 @param argc The address of a variable to hold the number of atoms in the array. 726 @param argv The address of a variable to hold a pointer to the first atom in the array. 727 @param def The default values specified as an instance of the #t_atomarray object. 728 @return A Max error code. 729 730 @see dictionary_getatoms() 731 @see dictionary_copydefatoms() 732 */ 733 t_max_err dictionary_getdefatoms(t_dictionary *d, t_symbol *key, long *argc, t_atom **argv, t_atom *def); 734 735 736 /** 737 Retrieve copies of a #t_atom array in the dictionary. 738 The retrieved pointer of t_atoms in the dictionary has memory allocated and copied to it from within the function. 739 You are responsible for freeing it with sysmem_freeptr(). 740 If the named key doesn't exist, then copy a default array of atoms, specified as a #t_atomarray*. 741 742 @ingroup dictionary 743 @param d The dictionary instance. 744 @param key The key associated with the value to lookup. 745 @param argc The address of a variable to hold the number of atoms in the array. 746 @param argv The address of a variable to hold a pointer to the first atom in the array. 747 You should initialize this pointer to NULL prior to passing it to dictionary_copyatoms(). 748 @param def The default values specified as an instance of the #t_atomarray object. 749 @return A Max error code. 750 751 @see dictionary_getdefatoms() 752 @see dictionary_copyatoms() 753 */ 754 t_max_err dictionary_copydefatoms(t_dictionary *d, t_symbol *key, long *argc, t_atom **argv, t_atom *def); 755 756 757 758 /** 759 Print the contents of a dictionary to the Max window. 760 761 @ingroup dictionary 762 @param d The dictionary instance. 763 @param recurse If non-zero, the dictionary will be recursively unravelled to the Max window. 764 Otherwise it will only print the top level. 765 @param console If non-zero, the dictionary will be posted to the console rather than the Max window. 766 On the Mac you can view this using Console.app. 767 On Windows you can use the free DbgView program which can be downloaded from Microsoft. 768 @return A Max error code. 769 */ 770 t_max_err dictionary_dump(t_dictionary *d, long recurse, long console); 771 772 773 /** 774 Copy specified entries from one dictionary to another. 775 776 @ingroup dictionary 777 @param src The source dictionary from which to copy entries. 778 @param dst The destination dictionary to which the entries will be copied. 779 @param keys The address of the first of an array of #t_symbol* that specifies which keys to copy. 780 @return A Max error code. 781 782 @see dictionary_copyunique() 783 */ 784 t_max_err dictionary_copyentries(t_dictionary *src, t_dictionary *dst, t_symbol **keys); 785 786 787 /** 788 Create a new dictionary populated with values using a combination of attribute and sprintf syntax. 789 790 @ingroup dictionary 791 @param fmt An sprintf-style format string specifying key-value pairs with attribute nomenclature. 792 @param ... One or more arguments which are to be substituted into the format string. 793 @return A new dictionary instance. 794 795 @remark Max attribute syntax is used to define key-value pairs. For example, 796 @code 797 "@key1 value @key2 another_value" 798 @endcode 799 800 @remark One common use of this to create dictionary that represents an element of a patcher, 801 or even an entire patcher itself. The example below creates a dictionary that can 802 be passed to a function like newobject_fromdictionary() to create a new object. 803 @code 804 t_dictionary *d; 805 char text[4]; 806 807 strncpy_zero(text, "foo", 4); 808 809 d = dictionary_sprintf("@maxclass comment @varname _name \ 810 @text \"%s\" @patching_rect %.2f %.2f %.2f %.2f \ 811 @fontsize %f @textcolor %f %f %f 1.0 \ 812 @fontname %s @bgcolor 0.001 0.001 0.001 0.", 813 text, 20.0, 20.0, 200.0, 24.0, 814 18, 0.9, 0.9, 0.9, "Arial"); 815 816 // do something with the dictionary here. 817 818 object_free(d); 819 @endcode 820 821 @see newobject_sprintf() 822 @see newobject_fromdictionary() 823 @see atom_setparse() 824 */ 825 t_dictionary *dictionary_sprintf(C74_CONST char *fmt, ...); 826 827 828 /** 829 Take a lock on a dictionary for preventing dictionary lock for transactions 830 across multiple calls, or holding on to internal dictionary element pointers 831 for complex operations. 832 833 @ingroup dictionary 834 @param d The dictionary to lock 835 @return A Max error code. 836 837 @see dictionary_transaction_unlock() 838 */ 839 long dictionary_transaction_lock(t_dictionary *d); 840 841 /** 842 Release a lock on a dictionary for preventing dictionary lock for transactions 843 across multiple calls, or holding on to internal dictionary element pointers 844 for complex operations. 845 846 @ingroup dictionary 847 @param d The dictionary to unlock 848 @return A Max error code. 849 850 @see dictionary_transaction_lock() 851 */ 852 long dictionary_transaction_unlock(t_dictionary *d); 853 854 /** Read the specified JSON file and return a #t_dictionary object. 855 You are responsible for freeing the dictionary with object_free(), 856 subject to the caveats explained in @ref when_to_free_a_dictionary. 857 @ingroup dictionary 858 @param filename The name of the file. 859 @param path The path of the file. 860 @param d The address of a #t_dictionary pointer that will be set to the newly created dictionary. 861 @return A Max error code 862 */ 863 t_max_err dictionary_read(const char *filename, short path, t_dictionary **d); 864 865 /** Serialize the specified #t_dictionary object to a JSON file. 866 @ingroup dictionary 867 @param d The dictionary to serialize into JSON format and write to disk. 868 @param filename The name of the file to write. 869 @param path The path to which the file should be written. 870 @return A Max error code. 871 */ 872 t_max_err dictionary_write(t_dictionary *d, const char *filename, short path); 873 874 END_USING_C_LINKAGE 875 876 #endif //__EXT_DICTIONARY_H__