Max 5 API Reference
00001 /** 00002 00003 @defgroup attr Attributes 00004 00005 An attribute of an object is a setting or property that tells the object how to do its job. 00006 For example, the metro object has an interval attribute that tells it how fast to run. 00007 00008 Attributes are similar to methods, except that the attributes have a state. 00009 Attributes are themselves objects, and they share a common interface for getting and setting values. 00010 00011 An attribute is most typically added to the class definition of another object during it's class 00012 initialization or main() function. 00013 Most typically, this attribute's value will be stored in an instance's struct, 00014 and thus it will serve as a property of that instance of the object. 00015 00016 Attributes can, however, be declared as 'class static'. 00017 This means that the property is shared by all instances of the class, 00018 and the value is stored as a shared (static) variable. 00019 00020 Additionally, Max 5 has introduced the notion of 'instance attributes' (also called 'object attributes'). 00021 Instance attributes are the creation of an attribute object, and then adding it to one specific 00022 instance of another class. 00023 00024 Finally, because attributes themselves are Max objects they too can possess attributes. 00025 These 'attributes of attributes' are used in Max to do things like specify a range of values for an attribute, 00026 give an attribute human friendly caption, 00027 or determine to what category an attribute should belong in the inspector. 00028 00029 The easiest and most common way of working with attributes is to use the provided macros. 00030 These macros simplify the process of creating a new attribute object, setting any attributes of the attribute, 00031 and binding it to an object class or an object instance. 00032 00033 00034 @section attribute_accessors Setting and Getting Attribute Values 00035 00036 By default, Max provides standard attribute accessors. 00037 These are the functions the get or set the attribute value in the object's struct. 00038 If you need to define a custom accessor, you can specify this information using the #CLASS_ATTR_ACCESSORS macro. 00039 00040 00041 @subsection attribute_accessors_getter Writing a custom Attribute Getter 00042 00043 If you need to define a custom accessor, it should have a prototype and form comparable to the following custom getter: 00044 00045 @code 00046 t_max_err foo_myval_get(t_foo *x, void *attr, long *ac, t_atom **av) 00047 { 00048 if ((*ac)&&(*av)) { 00049 //memory passed in, use it 00050 } else { 00051 //otherwise allocate memory 00052 *ac = 1; 00053 if (!(*av = getbytes(sizeof(t_atom)*(*ac)))) { 00054 *ac = 0; 00055 return MAX_ERR_OUT_OF_MEM; 00056 } 00057 } 00058 atom_setfloat(*av,x->myval); 00059 00060 return MAX_ERR_NONE; 00061 } 00062 @endcode 00063 00064 Note that getters require memory to be allocated, if there is not memory passed into the getter. 00065 Also the attr argument is the class' attribute object and can be queried using 00066 object_method for things like the attribute flags, names, filters, etc.. 00067 00068 00069 @subsection attribute_accessors_getter Writing a custom Attribute Setter 00070 00071 If you need to define a custom accessor, it should have a prototype and form comparable to the following custom setter: 00072 00073 @code 00074 t_max_err foo_myval_set(t_foo *x, void *attr, long ac, t_atom *av) 00075 { 00076 if (ac&&av) { 00077 x->myval = atom_getfloat(av); 00078 } else { 00079 // no args, set to zero 00080 x->myval = 0; 00081 } 00082 return MAX_ERR_NONE; 00083 } 00084 @endcode 00085 00086 00087 @section attribute_change_notification Attribute Notificaton 00088 00089 Although the subject of object registration and notification is covered elsewhere, 00090 it bears noting that attributes of all types will, if registered, 00091 automatically send notifications to all attached client objects each time the attribute's value is set. 00092 00093 */
Copyright © 2008, Cycling '74