Max 5 API Reference
00001 /** 00002 @defgroup datastore Data Storage 00003 00004 Max provides a number of ways of storing and manipulating data at a high level. 00005 It is recommended to use Max's data storage mechanisms where possible, 00006 as Max's systems are designed for thread-safety and integration with the rest of Max API. 00007 */ 00008 00009 00010 00011 00012 00013 00014 /** 00015 @defgroup atomarray Atom Array 00016 @ingroup datastore 00017 00018 Max's atomarray object is a container for an array of atoms with an interface for manipulating that array. 00019 It can be useful for passing lists as a single atom, such as for the return value of an #A_GIMMEBACK method. 00020 It also used frequently in when working with Max's #t_dictionary object. 00021 00022 @see dictionary 00023 */ 00024 00025 00026 /** 00027 @defgroup database Database 00028 @ingroup datastore 00029 00030 Max's database support currently consists of a SQLite ( http://sqlite.org ) extension which is loaded 00031 dynamically by Max at launch time. 00032 Because it is loaded dynamically, all interfacing with the sqlite object relies on Max's message passing interface, 00033 using object_method() and related functions. 00034 00035 For most common database needs, a C-interface is defined in the ext_database.h header file and implemented in the 00036 ext_database.c source file. The functions defined in this interface wrap the message passing calls and provide 00037 a convenient means by which you can work with databases. 00038 ext_database.c is located in the 'common' folder inside of the 'max-includes' folder. 00039 If you use any of the functions defined ext_database.h, you will need to add ext_database.c to your project. 00040 */ 00041 00042 00043 /** 00044 @defgroup dictionary Dictionary 00045 @ingroup datastore 00046 00047 In Max 5, we have a new "dictionary" object which can be used for object prototypes, object serialization, object constructors, and other tasks. 00048 A dictionary is ultimately a collection of atom values assigned to symbolic keys. 00049 In addition to primitive #A_LONG, #A_FLOAT, and #A_SYM atom types, 00050 the #A_OBJ atom type is used for #t_atomarray (for a set of atoms assigned to a key), 00051 #t_dictionary (for hierarhical use), 00052 #t_string (for large blocks of text which we don't wish to bloat the symbol table), and potentially other object data types. 00053 Internally, the dictionary object uses a combination data structure of a hash table (for fast key lookup) 00054 and a linked-list (to maintain ordering of information within the dictionary). 00055 00056 Dictionaries are clonable entites, but note that all the member objects of a given dictionary may not be clonable. 00057 At the time of this writing, for example, the #t_string object is not clonable, though it will be made clonable in the near future. 00058 In order for prototype entities to be guaranteed their passage into the constructor, 00059 they must be clonable (currenlty a symbol conversion is in place for the t_string class). 00060 00061 00062 @section using_dictionaries Using Dictionaries 00063 00064 Dictionaries are used in many places in Max 5. They can be confusing in many respects. 00065 It is easy to produce memory leaks or bugs where objects are freed twice. 00066 It is easy to confuse what type of dictionary is used for what. 00067 This page will begin with some high level information to help understand when to free and when not to free. 00068 Then, we will offer recipies for using dictionaries to accomplish common tasks. 00069 00070 00071 @subsection understanding_dictionaries Understanding Dictionaries 00072 00073 A dictionary stores atom values under named key entries. These atoms can contain #A_OBJ values. 00074 When the dictionary is freed, any #A_OBJ values that are in the dictionary will also be freed. 00075 Thus, it is easy to mistakenly free objects twice, thus this is something to be careful about. 00076 For example, look at this code: 00077 00078 @code 00079 t_dictionary *d = dictionary_new(); 00080 t_dictionary *sd = dictionary_new(); 00081 dictionary_appenddictionary(d, gensym("subdictionary"), sd); 00082 do_something(d); 00083 object_free(d); // this will free *both* d and sd since sd is contained by d 00084 // freeing "sd" here would be bad 00085 @endcode 00086 00087 You primarily need to keep this in mind when calling dictionary_appendobject(), dictionary_appenddictionary(), or dictionary_appendatomarray(). 00088 So, what do you do if you need to free a dictionary but you also want to hang on to an object that is inside of the dictionary? 00089 In this case, chuck the entry in question first. 00090 For example, let's assume that for some reason you cannot free the "sd" dictionary in the code above. 00091 Perhaps it doesn't belong to you. But, to do some operation you need to append it to a new dictionary. 00092 Then, do this: 00093 00094 @code 00095 void function_foo(t_dictionary *sd) { 00096 t_dictionary *d = dictionary_new(); 00097 dictionary_appenddictionary(d, gensym("subdictionary"), sd); 00098 do_something(d); 00099 dictionary_chuckentry(d, gensym("subdictionary")); 00100 object_free(d); 00101 } 00102 @endcode 00103 00104 00105 @subsection when_to_free_a_dictionary When to Free a Dictionary 00106 00107 So, how do you know when you need to free a dictionary? 00108 Well, generally if you make a dictionary, you need to free it when you are done (unless you transfer ownership of the dictionary to someone else). 00109 On the other hand, if you are passed a dictionary (i.e. as a parameter of your function or method) 00110 then it is not yours to free and you should just use it. 00111 However, it is not always obvious that you made a dictionary vs just borrowed it. 00112 00113 Here are some common (and not so common) ways to make a dictionary. 00114 These functions return a new dictionary and thus the dictionary you get should be freed when you are done, 00115 unless you pass the dictionary on to someone else who will free it at an appropriate time. Here they are: 00116 <ul> 00117 <li>dictionary_new() </li> 00118 <li>dictionary_clone() </li> 00119 <li>dictionary_read() </li> 00120 <li>dictionary_sprintf() </li> 00121 <li>dictionary_vsprintf() </li> 00122 <li>jsonreader_parse() </li> 00123 <li>jpatcher_monikerforobject() </li> 00124 <li>class_cloneprototype() </li> 00125 <li>prototype_getdictionary() </li> 00126 <li>clipboard_todictionary() </li> 00127 <li>jpatchercontroller_copytodictionary() </li> 00128 <li>probably others of course</li> 00129 </ul> 00130 00131 Here are some functions that return borrowed dictionaries. 00132 These are dictionaries that you can use but you cannot free since you do not own them. 00133 Here they are: 00134 <ul> 00135 <li>dictionary_prototypefromclass() </li> 00136 <li>object_refpage_get_class_info_fromclassname() </li> 00137 <li>object_refpage_get_class_info() </li> 00138 <li>object_dictionaryarg() </li> 00139 </ul> 00140 00141 Finally, most functions that accept dictionaries as parameters will not assume ownership of the dictionary. 00142 Usually the way ownership is assumed is if you add a dictionary as a subdictionary to a dictionary that you do not own. 00143 One exception is the utility newobject_fromdictionary_delete() 00144 who's name makes it clear that the dictionary will be deleted after calling the function. 00145 00146 00147 @subsection dictionaries_common_uses Some Common Uses of Dictionaries 00148 00149 You can make a patcher by passing a dictionary to object_new_typed() when making a "jpatcher". 00150 Using atom_setparse() and attr_args_dictionary() makes this relatively easy. 00151 00152 Use newobject_sprintf() to programmatically make an object in a patch. 00153 Actually, you don't explicitly use a dictionary here! 00154 If you do want more control, so you can touch the dictionary to customize it, then see the next bullet. 00155 00156 Use dictionary_sprintf() to make a dictionary to specify a box (i.e. specify class with \@maxclass attr). 00157 Then, make another dictionary and append your box dictionary to it under the key "box" via dictionary_appenddictionary(). 00158 Finally, make your object with newobject_fromdictionary(). 00159 00160 @see linklist 00161 @see hashtab 00162 00163 @version 5.0 00164 */ 00165 00166 00167 00168 /** 00169 @defgroup hashtab Hash Table 00170 @ingroup datastore 00171 00172 Max's hashtab object implements a hash table ( http://en.wikipedia.org/wiki/Hash_table ). 00173 Any type of value may be stored in the table, indexed using a #t_symbol as the unique key. 00174 00175 @see linklist 00176 00177 */ 00178 00179 00180 00181 /** 00182 @defgroup indexmap Index Map 00183 @ingroup datastore 00184 00185 An indexmap is basically a managed array of pointers, 00186 but it allows you to derive relatively quickly the index from a pointer in the array. 00187 00188 The index is assumed to be 0-N (where N is the current size of the array). 00189 You can sort the data and retain access to an index from the data relatively quickly. 00190 There is a hashtab which holds pieces of memory that hold indices that can be referenced by the data pointer. 00191 There is also an array of data pointers -- this is in "index" order. 00192 When operations take place on the array (insert, delete, sort), the pointers in the hashtab are updated with new indices. 00193 */ 00194 00195 00196 00197 00198 00199 /** 00200 @defgroup linklist Linked List 00201 @ingroup datastore 00202 00203 Max's linklist object implements a doubly-linked-list ( http://en.wikipedia.org/wiki/Linked_list ) together 00204 with a high-level interface for manipulating and accessing values in the list. 00205 */ 00206 00207 00208 00209 /** 00210 @defgroup quickmap Quick Map 00211 @ingroup datastore 00212 00213 A quickmap implements a pair of #t_hashtab hash tables so that it is fast to look up a unique value for a unique key or vice-versa. 00214 This implies that both the keys and the values must be unique so that look-ups can be performed in both directions. 00215 */ 00216 00217 00218 00219 /** 00220 @defgroup string String Object 00221 @ingroup datastore 00222 00223 Max's string object is a simple wrapper for c-strings, useful when working with Max's #t_dictionary, #t_linklist, or #t_hashtab. 00224 00225 @see dictionary 00226 */ 00227 00228 00229 00230 00231 /** 00232 @defgroup symobject Symbol Object 00233 @ingroup datastore 00234 00235 The symobject class is a simple object that wraps a #t_symbol* together with a couple of additional fields. 00236 It is useful for storing symbols, possibly with additional flags or pointers, into a @ref hashtab or @ref linklist. 00237 00238 @version 5.0 00239 */ 00240 00241
Copyright © 2008, Cycling '74