github.com/256dpi/max-go@v0.7.0/lib/max/ext_dictobj.h (about) 1 /* 2 ext_dictobj.h 3 Copyright 2010 - Cycling '74 4 Timothy Place, tim@cycling74.com 5 */ 6 7 #ifndef __EXT_DICTOBJ_H__ 8 #define __EXT_DICTOBJ_H__ 9 10 #include "ext_prefix.h" 11 #include "ext_dictionary.h" 12 13 BEGIN_USING_C_LINKAGE 14 15 /** Register a #t_dictionary with the dictionary passing system and map it to a unique name. 16 17 @ingroup dictobj 18 @param d A valid dictionary object. 19 @param name The address of a #t_symbol pointer to the name you would like mapped to this dictionary. 20 If the t_symbol pointer has a NULL value then a unique name will be generated and filled-in 21 upon return. 22 @return The dictionary mapped to the specified name. 23 **/ 24 t_dictionary *dictobj_register(t_dictionary *d, t_symbol **name); 25 26 27 /** Unregister a #t_dictionary with the dictionary passing system. 28 Generally speaking you should not need to call this method. 29 Calling object_free() on the #t_dictionary automatically unregisters it. 30 31 @ingroup dictobj 32 @param d A valid dictionary object. 33 @return A Max error code. 34 **/ 35 t_max_err dictobj_unregister(t_dictionary *d); 36 37 38 /** Find the #t_dictionary for a given name, and return a <i>copy</i> of that dictionary 39 When you are done, do <i>not</i> call dictobj_release() on the dictionary, 40 because you are working on a copy rather than on a retained pointer. 41 42 @ingroup dictobj 43 @param name The name associated with the dictionary for which you wish to obtain a copy. 44 @return The dictionary cloned from the existing dictionary. 45 Returns NULL if no dictionary is associated with name. 46 @see #dictobj_findregistered_retain() 47 **/ 48 t_dictionary *dictobj_findregistered_clone(t_symbol *name); 49 50 51 /** Find the #t_dictionary for a given name, return a pointer to that #t_dictionary, and increment its reference count. 52 When you are done you should call dictobj_release() on the dictionary. 53 54 @ingroup dictobj 55 @param name The name associated with the dictionary for which you wish to obtain a pointer. 56 @return A pointer to the dictionary associated with name. 57 Returns NULL if no dictionary is associated with name. 58 @see #dictobj_release() 59 @see #dictobj_findregistered_clone() 60 **/ 61 t_dictionary *dictobj_findregistered_retain(t_symbol *name); 62 63 64 /** For a #t_dictionary/name that was previously retained with dictobj_findregistered_retain(), 65 release it (decrement its reference count). 66 67 @ingroup dictobj 68 @param d A valid dictionary object retained by dictobj_findregistered_retain(). 69 @return A Max error code. 70 @see #dictobj_findregistered_retain() 71 **/ 72 t_max_err dictobj_release(t_dictionary *d); 73 74 75 /** Find the name associated with a given #t_dictionary. 76 77 @ingroup dictobj 78 @param d A dictionary, whose name you wish to determine. 79 @return The symbol associated with the dictionary, or NULL if the dictionary is not registered. 80 @see #dictobj_register() 81 **/ 82 t_symbol *dictobj_namefromptr(t_dictionary *d); 83 84 85 /** Send atoms to an outlet in your Max object, handling complex datatypes that may be present in those atoms. 86 This is particularly when sending the contents of a dictionary entry out of an outlet as in the following example code. 87 88 @code 89 long ac = 0; 90 t_atom *av = NULL; 91 t_max_err err; 92 93 err = dictionary_copyatoms(d, key, &ac, &av); 94 if (!err && ac && av) { 95 // handles singles, lists, symbols, atomarrays, dictionaries, etc. 96 dictobj_outlet_atoms(x->outlets[i],ac,av); 97 } 98 99 if (av) 100 sysmem_freeptr(av); 101 @endcode 102 103 @ingroup dictobj 104 @param out The outlet through which the atoms should be sent. 105 @param argc The count of atoms in argv. 106 @param argv Pointer to the first of an array of atoms to send to the outlet. 107 **/ 108 void dictobj_outlet_atoms(void *out, long argc, t_atom *argv); 109 110 111 /** Ensure that an atom is safe for passing. 112 Atoms are allowed to be #A_LONG, #A_FLOAT, or #A_SYM, but not #A_OBJ. 113 If the atom is an #A_OBJ, it will be converted into something that will be safe to pass. 114 115 @ingroup dictobj 116 @param a An atom to check, and potentially modify, to ensure safety in the dictionary-passing system. 117 @return If the atom was changed then 1 is returned. Otherwise 0 is returned. 118 **/ 119 long dictobj_atom_safety(t_atom *a); 120 121 122 enum { 123 DICTOBJ_ATOM_FLAGS_DEFAULT = 0, ///< default 124 DICTOBJ_ATOM_FLAGS_REGISTER ///< dictionary atoms should be registered/retained 125 }; 126 127 128 /** Ensure that an atom is safe for passing. 129 Atoms are allowed to be #A_LONG, #A_FLOAT, or #A_SYM, but not #A_OBJ. 130 If the atom is an #A_OBJ, it will be converted into something that will be safe to pass. 131 132 @ingroup dictobj 133 @param a An atom to check, and potentially modify, to ensure safety in the dictionary-passing system. 134 @param flags Pass DICTOBJ_ATOM_FLAGS_REGISTER to have dictionary atoms registered/retained. 135 @return If the atom was changed then 1 is returned. Otherwise 0 is returned. 136 **/ 137 long dictobj_atom_safety_flags(t_atom *a, long flags); 138 139 140 void dictobj_atom_release(t_atom *a); 141 142 143 /** Validate the contents of a #t_dictionary against a second #t_dictionary containing a schema. 144 145 The schema dictionary contains keys and values, like any dictionary. 146 dictobj_validate() checks to make sure that all keys in the schema dictionary are present in the candidate dictionary. 147 If the keys are all present then the candidate passes and the function returns true. 148 Otherwise the the candidate fails the validation and the function returns false. 149 150 Generally speaking, the schema dictionary with contain values with the symbol "*", indicating a wildcard, 151 and thus only the key is used to validate the dictionary (all values match the wildcard). 152 However, if the schema dictionary contains non-wildcard values for any of its keys, those keys in the 153 candidate dictionary must also contain matching values in order for the candidate to successfully validate. 154 155 An example of this in action is the dict.route object in Max, which simply wraps this function. 156 157 @ingroup dictobj 158 @param schema The dictionary against which to validate candidate. 159 @param candidate A dictionary to test against the schema. 160 @return Returns true if the candidate validates against the schema, otherwise returns false. 161 @see #dictobj_dictionarytoatoms() 162 **/ 163 long dictobj_validate(const t_dictionary *schema, const t_dictionary *candidate); 164 165 166 /** Convert a C-string of @ref using_dictobj_syntax into a C-string of JSON. 167 168 @ingroup dictobj 169 @param jsonsize The address of a variable to be filled-in with the number of chars in json upon return. 170 @param json The address of a char pointer to point to the JSON C-string upon return. 171 Should be initialized to NULL. 172 You are responsible for freeing the string with sysmem_freeptr() when you are done with it. 173 @param str A NULL-terminated C-string containing @ref using_dictobj_syntax . 174 @return A Max error code. 175 @see #dictobj_dictionarytoatoms() 176 **/ 177 t_max_err dictobj_jsonfromstring(long *jsonsize, char **json, const char *str); 178 179 180 /** Create a new #t_dictionary from @ref using_dictobj_syntax which is passed in as a C-string. 181 182 @ingroup dictobj 183 @param d The address of a dictionary variable, which will hold a pointer to 184 the new dictionary upon return. Should be initialized to NULL. 185 @param str A NULL-terminated C-string containing @ref using_dictobj_syntax . 186 @param str_is_already_json 187 @param errorstring 188 @return A Max error code. 189 @see #dictobj_dictionarytoatoms() 190 **/ 191 t_max_err dictobj_dictionaryfromstring(t_dictionary **d, const char *str, int str_is_already_json, char *errorstring); 192 193 194 /** Create a new #t_dictionary from @ref using_dictobj_syntax which is passed in as an array of atoms. 195 Unlike many #t_dictionary calls to create dictionaries, this function does not take over ownership of the atoms you pass in. 196 197 @ingroup dictobj 198 @param d The address of a dictionary variable, which will hold a pointer to 199 the new dictionary upon return. Should be initialized to NULL. 200 @param argc The number of atoms in argv. 201 @param argv Pointer to the first of an array of atoms to be interpreted as 202 @ref using_dictobj_syntax . 203 @return A Max error code. 204 @see #dictobj_dictionaryfromatoms_extended() #dictobj_dictionarytoatoms() 205 **/ 206 t_max_err dictobj_dictionaryfromatoms(t_dictionary **d, const long argc, const t_atom *argv); 207 208 209 /** Create a new #t_dictionary from from an array of atoms that use Max dictionary syntax, JSON, or compressed JSON. 210 This function is the C analog to the dict.deserialize object in Max. 211 Unlike many #t_dictionary calls to create dictionaries, this function does not take over ownership of the atoms you pass in. 212 213 @ingroup dictobj 214 @param d The address of a dictionary variable, which will hold a pointer to 215 the new dictionary upon return. Should be initialized to NULL. 216 @param msg Ignored. 217 @param argc The number of atoms in argv. 218 @param argv Pointer to the first of an array of atoms to be interpreted as @ref using_dictobj_syntax , JSON, or compressed JSON. 219 @return A Max error code. 220 @see #dictobj_dictionaryfromatoms() #dictobj_dictionaryfromstring() 221 **/ 222 t_max_err dictobj_dictionaryfromatoms_extended(t_dictionary **d, const t_symbol *msg, long argc, const t_atom *argv); 223 224 225 /** Serialize the contents of a #t_dictionary into @ref using_dictobj_syntax . 226 227 @ingroup dictobj 228 @param d The dictionary to serialize. 229 @param argc The address of a variable to hold the number of atoms allocated upon return. 230 @param argv The address of a t_atom pointer which will point to the first atom 231 (of an array of argc atoms) upon return. You are responsible for freeing the pointer 232 returned with sysmem_freeptr() when you are done with it. If you pass in existing memory, 233 it must be memory allocated using sysmem_newptr() and the pointer may be resized. 234 235 @return A Max error code. 236 @see #dictobj_dictionaryfromatoms() 237 **/ 238 t_max_err dictobj_dictionarytoatoms(const t_dictionary *d, long *argc, t_atom **argv); 239 240 241 /** Given a complex key (one that includes potential heirarchy or array-member access), 242 return the actual key and the dictionary in which the key should be referenced. 243 244 @ingroup dictobj 245 @param x Your calling object. If there is an error this will be used by the internal call to object_error(). 246 @param d The dictionary you are querying. 247 @param akey The complex key specifying the query. 248 @param create If true, create the intermediate dictionaries in the hierarchy specified in akey. 249 @param targetdict Returns the t_dictionary that for the (sub)dictionary specified by akey. 250 @param targetkey Returns the name of the key in targetdict that to which akey is referencing. 251 @param index Returns the index requested if array-member access is specified. Pass NULL if you are not interested in this. 252 253 @return A Max error code. 254 */ 255 t_max_err dictobj_key_parse(t_object *x, t_dictionary *d, t_atom *akey, t_bool create, t_dictionary **targetdict, t_symbol **targetkey, t_int32 *index); 256 257 258 259 END_USING_C_LINKAGE 260 261 262 #endif // __EXT_DICTOBJ_H__