github.com/256dpi/max-go@v0.7.0/lib/max/ext_atomarray.h (about)

     1  
     2  #ifndef _EXT_ATOMARRAY_H_
     3  #define _EXT_ATOMARRAY_H_
     4  
     5  #include "max_types.h"
     6  #include "ext_prefix.h"
     7  #include "ext_mess.h"
     8  
     9  #if C74_PRAGMA_STRUCT_PACKPUSH
    10      #pragma pack(push, 2)
    11  #elif C74_PRAGMA_STRUCT_PACK
    12      #pragma pack(2)
    13  #endif
    14  
    15  /** The atomarray flags. Currently the only flag is ATOMARRAY_FLAG_FREECHILDREN. 
    16       If set via atomarray_flags() the atomarray will free any contained A_OBJ atoms when the
    17  	 atomarray is freed.
    18  
    19  	@ingroup atomarray
    20  */
    21  
    22  enum t_atomarray_flags {
    23  	ATOMARRAY_FLAG_FREECHILDREN = 1
    24  };
    25  
    26  #ifndef C74_X64
    27  
    28  union word64			
    29  {
    30  	t_int64 w_long;
    31  	double w_float;
    32  	struct symbol *w_sym;
    33  	struct object *w_obj;
    34  };
    35  
    36  typedef struct atom64	
    37  {
    38  	short a_type;		
    39  	union word64 a_w;
    40  } t_atom64;
    41  
    42  #else
    43  
    44  typedef t_atom t_atom64;
    45  
    46  #endif
    47  
    48  
    49  /** The atomarray object. This struct is provided for debugging convenience, 
    50   but should be considered opaque and is subject to change without notice. 
    51   
    52   @ingroup atomarray
    53   */
    54  typedef struct _atomarray
    55  {
    56  	t_object	ob;
    57  	long		ac;
    58  	t_atom		*av;
    59  	long		flags; 
    60  #ifndef C74_X64
    61  	t_atom64	*av64;
    62  #endif
    63  } t_atomarray;
    64  
    65  
    66  #if C74_PRAGMA_STRUCT_PACKPUSH
    67      #pragma pack(pop)
    68  #elif C74_PRAGMA_STRUCT_PACK
    69      #pragma pack()
    70  #endif
    71  
    72  BEGIN_USING_C_LINKAGE
    73  
    74  /** 
    75  	Create a new atomarray object.
    76  	Note that atoms provided to this function will be <em>copied</em>. The copies stored internally to the atomarray instance.
    77  	You can free the atomarray by calling object_free().
    78  
    79  	@ingroup	atomarray	
    80  	@param	ac	The number of atoms to be initially contained in the atomarray.
    81  	@param	av	A pointer to the first of an array of atoms to initially copy into the atomarray.
    82  	@return		Pointer to the new atomarray object.
    83  	
    84  	@remark		Note that due to the unusual prototype of this method that you cannot instantiate this object using the
    85  				object_new_typed() function.  If you wish to use the dynamically bound creator to instantiate the object,
    86  				you should instead should use object_new() as demonstrated below.  The primary reason that you might choose
    87  				to instantiate an atomarray using object_new() instead of atomarray_new() is for using the atomarray object
    88  				in code that is also intended to run in Max 4.
    89  	@code
    90  	object_new(CLASS_NOBOX, gensym("atomarray"), argc, argv);
    91  	@endcode
    92  	
    93  	@see		atomarray_duplicate()
    94  */
    95  t_atomarray *atomarray_new(long ac, t_atom *av);
    96  
    97  /** 
    98  	Set the atomarray flags.  
    99  
   100  	@ingroup	atomarray
   101  	
   102  	@param	x		The atomarray instance.
   103  	@param  flags	The new value for the flags.
   104  */
   105  void atomarray_flags(t_atomarray *x, long flags); 
   106  
   107  /** 
   108  	Get the atomarray flags.  
   109  
   110  	@ingroup	atomarray
   111  	
   112  	@param	x	The atomarray instance.
   113  	@return		The current value of the atomarray flags. 	
   114  */
   115  long atomarray_getflags(t_atomarray *x); 
   116  
   117  /** 
   118  	Replace the existing array contents with a new set of atoms
   119  	Note that atoms provided to this function will be <em>copied</em>.  The copies stored internally to the atomarray instance.
   120  
   121  	@ingroup	atomarray
   122  	
   123  	@param	x	The atomarray instance.
   124  	@param	ac	The number of atoms to be initially contained in the atomarray.
   125  	@param	av	A pointer to the first of an array of atoms to initially copy into the atomarray.
   126  	@return		A Max error code.
   127  */
   128  t_max_err atomarray_setatoms(t_atomarray *x, long ac, t_atom *av);
   129  
   130  
   131  /** 
   132  	Retrieve a pointer to the first atom in the internal array of atoms.
   133  	This method does not copy the atoms, btu simply provides access to them.
   134  	To retrieve a copy of the atoms use atomarray_copyatoms().
   135  
   136  	@ingroup	atomarray
   137  	
   138  	@param	x	The atomarray instance.
   139  	@param	ac	The address of a long where the number of atoms will be set.
   140  	@param	av	The address of a #t_atom pointer where the address of the first atom of the array will be set.
   141  	@return		A Max error code.
   142  	
   143  	@see		atomarray_copyatoms()
   144  */
   145  t_max_err atomarray_getatoms(t_atomarray *x, long *ac, t_atom **av);
   146  
   147  
   148  /** 
   149  	Retrieve a copy of the atoms in the array.
   150  	To retrieve a pointer to the contained atoms use atomarray_getatoms().
   151  
   152  	@ingroup	atomarray
   153  	
   154  	@param	x	The atomarray instance.
   155  	@param	ac	The address of a long where the number of atoms will be set.
   156  	@param	av	The address of a #t_atom pointer where the atoms will be allocated and copied.
   157  	@return		A Max error code.
   158  	
   159  	@remark		You are responsible for freeing memory allocated for the copy of the atoms returned.
   160  	@code
   161  	long	ac = 0;
   162  	t_atom *av = NULL;
   163  	
   164  	atomarray_copyatoms(anAtomarray, &ac, &av);
   165  	if(ac && av){
   166  		// do something with ac and av here...
   167  		sysmem_freeptr(av);
   168  	}	
   169  	@endcode
   170  
   171  	@see		atomarray_getatoms()
   172  */
   173  t_max_err atomarray_copyatoms(t_atomarray *x, long *ac, t_atom **av);
   174  
   175  
   176  /** 
   177  	Return the number of atoms in the array.
   178  
   179  	@ingroup	atomarray
   180  	@param	x	The atomarray instance.
   181  	@return		The number of atoms in the array.
   182  */
   183  t_atom_long atomarray_getsize(t_atomarray *x);
   184  
   185  
   186  /** 
   187  	Copy an a specific atom from the array.
   188  
   189  	@ingroup		atomarray
   190  	@param	x		The atomarray instance.
   191  	@param	index	The zero-based index into the array from which to retrieve an atom pointer.
   192  	@param	av		The address of an atom to contain the copy.
   193  	@return			A Max error code.
   194  	
   195  	@remark			Example:
   196  	@code
   197  	{
   198  		t_atom a;
   199  
   200  		// fetch a copy of the second atom in a previously existing array
   201  		atomarray_getindex(anAtomarray, 1, &a);
   202  		// do something with the atom here...
   203  	}
   204  	@endcode
   205  */
   206  t_max_err atomarray_getindex(t_atomarray *x, long index, t_atom *av);
   207  
   208  
   209  // not exported yet
   210  t_max_err atomarray_setindex(t_atomarray *x, long index, t_atom *av);
   211  
   212  
   213  /** 
   214  	Create a new atomarray object which is a copy of another atomarray object.
   215  
   216  	@ingroup		atomarray
   217  	@param	x		The atomarray instance which is to be copied.
   218  	@return			A new atomarray which is copied from x.
   219  		
   220  	@see	atomarray_new()
   221  */
   222  void *atomarray_duplicate(t_atomarray *x);
   223  
   224  
   225  /**
   226  	Create a new atomarray object which is a full clone of another atomarray object.
   227   
   228  	@ingroup		atomarray
   229  	@param	x		The atomarray instance which is to be copied.
   230  	@return			A new atomarray which is copied from x.
   231   
   232  	@see	atomarray_new()
   233   */
   234  void *atomarray_clone(t_atomarray *x);
   235  
   236  /** 
   237  	Copy a new atom onto the end of the array.
   238  
   239  	@ingroup		atomarray
   240  	@param	x		The atomarray instance.
   241  	@param	a		A pointer to the new atom to append to the end of the array.
   242  		
   243  	@see	atomarray_appendatoms()
   244  	@see	atomarray_setatoms()
   245  */
   246  void atomarray_appendatom(t_atomarray *x, t_atom *a);
   247  
   248  
   249  /** 
   250  	Copy multiple new atoms onto the end of the array.
   251  
   252  	@ingroup		atomarray
   253  	@param	x		The atomarray instance.
   254  	@param	ac		The number of new atoms to be appended to the array.
   255  	@param	av		A pointer to the first of the new atoms to append to the end of the array.
   256  		
   257  	@see	atomarray_appendatom()
   258  	@see	atomarray_setatoms()
   259  */
   260  void atomarray_appendatoms(t_atomarray *x, long ac, t_atom *av);
   261  
   262  
   263  /** 
   264  	Remove an atom from any location within the array.
   265  	The array will be resized and collapsed to fill in the gap.
   266  
   267  	@ingroup		atomarray
   268  	@param	x		The atomarray instance.
   269  	@param	index	The zero-based index of the atom to remove from the array.
   270  */
   271  void atomarray_chuckindex(t_atomarray *x, long index);
   272  
   273  
   274  /** 
   275  	Clear the array.  Frees all of the atoms and sets the size to zero.
   276  	This function does not perform a 'deep' free, meaning that any #A_OBJ atoms will not have their object's freed.  
   277  	Only the references to those objects contained in the atomarray will be freed.
   278  
   279  	@ingroup	atomarray
   280  	@param	x	The atomarray instance.
   281  	@return		The number of atoms in the array.
   282  */
   283  void atomarray_clear(t_atomarray *x);
   284  
   285  
   286  /**
   287  	Call the specified function for every item in the atom array.  
   288  
   289  	@ingroup atomarray
   290  	@param	x		The atomarray instance.
   291  	@param	fun		The function to call, specified as function pointer cast to a Max #method.
   292  	@param	arg		An argument that you would like to pass to the function being called.
   293  	@return			A max error code.
   294  	
   295  	@remark			The atomarray_funall() method will call your function for every item in the list.
   296  					It will pass both a pointer to the item in the list, and any argument that you
   297  					provide.  The following example shows a function that could be called by hashtab_funall().
   298  	@code
   299  	void myFun(t_atom *a, void *myArg)
   300  	{
   301  		// do something with a and myArg here
   302  		// a is the atom in the atom array
   303  	}
   304  	@endcode
   305  	
   306  	@see			linklist_funall()
   307  	@see			hashtab_funall()
   308  */
   309  void atomarray_funall(t_atomarray *x, method fun, void *arg);
   310  
   311  #ifndef C74_X64
   312  #define atom64_setfloat(x, f) ((x)->a_type = A_FLOAT, (x)->a_w.w_float = (f))
   313  #define atom64_getfloat(x) (((t_atom64*)x)->a_w.w_float);
   314  #else
   315  #define atom64_setfloat(x, f) atom_setfloat(x, f)
   316  #define atom64_getfloat(x) atom_getfloat(x)
   317  #endif
   318  
   319  END_USING_C_LINKAGE
   320  
   321  #endif // #ifndef _EXT_ATOMARRAY_H_