WoMax Documentation

# OMax.date2state/OMax.date2state.c

    00001 /*--------------------------------------
    00002  * OMax.date2state - OMax.date2state.c
    00003  * Created on 29/03/09 by BenCello
    00004  *--------------------------------------*/
    00005
    00007
    00009
    00010
    00011 #ifndef __OMax_date2state_MAX_API_
    00012 #define __OMax_date2state_MAX_API_
    00013
    00014 #include "Oracle_data.hpp"
    00015
    00016 extern "C"
    00017 {
    00018
    00019 #include "ext.h"                                // standard Max include, always required
    00020 #include "ext_obex.h"                   // required for new style Max object
    00021 #include "jpatcher_api.h"               // required for the color
    00022 #include "jgraphics.h"                  // required for the color
    00023
    00024 #include "OMax.data.h"
    00025
    00030         typedef struct _OMax_date2state
    00031         {
    00032                 t_object        ob;
    00033                 t_symbol*       oname;
    00034                 t_symbol*       dataname;
    00035                 bool            obound;
    00036                 O_DataType      datatype;
    00037                 O_data*         data;
    00038                 void*           out;
    00039         } t_OMax_date2state;
    00040
    00042
    00044         // Prototypes //
    00046
    00047         // Standard Max5 methodes
    00048         void *OMax_date2state_new(t_symbol *s, long argc, t_atom *argv);
    00049         void OMax_date2state_free(t_OMax_date2state *x);
    00050         void OMax_date2state_assist(t_OMax_date2state *x, void *b, long m, long a, char *s);
    00051
    00052         // Input/ouput routines
    00053         void OMax_date2state_do(t_OMax_date2state *x, long date);
    00054         void OMax_date2state_setname(t_OMax_date2state *x, t_symbol *newname);
    00055
    00056         // Internal routines
    00057         t_symbol * OMax_date2state_name(t_symbol * oname);
    00058         bool OMax_date2state_bind(t_OMax_date2state *x);
    00059
    00060         // Global class pointer variable
    00061         t_class *OMax_date2state_class;
    00062
    00064         // Functions //
    00066
    00067         int main(void)
    00068         {
    00069                 t_class *c;
    00070
    00071                 c = class_new("OMax.date2state", (method)OMax_date2state_new, (method)OMax_date2state_free, (long)sizeof(t_OMax_date2state),
    00072                                           0L /* leave NULL!! */, A_GIMME, 0);
    00073
    00074                 // assistance
    00075                 class_addmethod(c, (method)OMax_date2state_assist,"assist",A_CANT, 0);
    00076
    00077                 // input methods
    00078                 class_addmethod(c, (method)OMax_date2state_do, "int", 0);
    00079                 class_addmethod(c, (method)OMax_date2state_setname, "set", A_DEFSYM, 0);
    00080
    00081                 class_register(CLASS_BOX, c); /* CLASS_NOBOX */
    00082                 OMax_date2state_class = c;
    00083
    00084                 return 0;
    00085         }
    00086
    00088
    00089
    00092         void *OMax_date2state_new(t_symbol *s, long argc, t_atom *argv)
    00093         {
    00094                 t_OMax_date2state *x = NULL;
    00095
    00096                 // object instantiation, NEW STYLE
    00097                 if (x = (t_OMax_date2state *)object_alloc(OMax_date2state_class))
    00098                 {
    00099                         // outlet init
    00100                         x->out = intout(x);
    00101
    00103                         x->obound = FALSE;
    00104                         if (argc == 0)
    00105                                 object_error((t_object *)x,"Missing name of the Oracle data to read");
    00106                         else
    00107                         {
    00108                                 if (argv->a_type != A_SYM)
    00109                                         object_error((t_object *)x,"First argument must be a symbol (name of an existing Oracle)");
    00110                                 else
    00111                                         x->oname = atom_getsym(argv);
    00112                         }
    00113
    00114                         // color
    00115                         t_object *box;
    00116                         t_jrgba colorvals;
    00117                         jrgba_set(&colorvals, 0.30, 1.0, 0.15, 1.0);
    00118                         object_obex_lookup((t_object *)x, gensym("#B"), &box);
    00119                         jbox_set_color(box, &colorvals);
    00120                 }
    00121                 return (x);
    00122         }
    00123
    00124
    00127         void OMax_date2state_free(t_OMax_date2state *x)
    00128         {
    00129                 ;
    00130         }
    00131
    00134         void OMax_date2state_assist(t_OMax_date2state *x, void *b, long m, long a, char *s)
    00135         {
    00136                 if (m == ASSIST_INLET) { // inlet
    00137                         sprintf(s, "date, set [symbol] changes sequence to read");
    00138                 }
    00139                 else {  // outlet
    00140                         sprintf(s, "state number");
    00141                 }
    00142         }
    00143
    00145
    00147
    00148
    00151         t_symbol * OMax_date2state_name(t_symbol * oname)
    00152         {
    00153                 char dataname[128];
    00154                 strcpy(dataname,oname->s_name);
    00156                 strcat(dataname,"_data");
    00157                 return gensym(dataname);
    00158         }
    00159
    00162         bool OMax_date2state_bind(t_OMax_date2state *x)
    00163         {
    00165                 if (x->obound == FALSE)
    00166                 {
    00168                         x->dataname = OMax_date2state_name(x->oname);
    00169                         if ((x->dataname->s_thing) && (ob_sym(x->dataname->s_thing) == gensym("OMax.data")))
    00170                         {
    00171                                 x->data = &(((t_OMax_data*)(x->dataname->s_thing))->data);
    00172                                 // If binding is ok, then don't do it next time.
    00173                                 x->obound = TRUE;
    00175                                 x->datatype = ((t_OMax_data*)(x->dataname->s_thing))->datatype;
    00176                         }
    00177                         else
    00178                         {
    00179                                 object_error((t_object *)x,"No data for Oracle %s declared", x->oname->s_name);
    00180                         }
    00181                 }
    00182                 return x->obound;
    00183         }
    00184
    00186
    00188
    00189
    00193         void OMax_date2state_do(t_OMax_date2state *x, long date)
    00194         {
    00195                 if (OMax_date2state_bind(x))
    00196                 {
    00197                         ATOMIC_INCREMENT(&(((t_OMax_data *)(x->dataname->s_thing))->readcount));
    00198                         if(!(((t_OMax_data *)(x->dataname->s_thing))->wflag))
    00199                         {
    00200                                 if (x->data->get_size()>0 && date>=0)
    00201                                 {
    00202                                         switch(x->datatype)
    00203                                         {
    00204                                                 case LETTERS:
    00205                                                         outlet_int(x->out, date+1); // output
    00206                                                         break;
    00207                                                 default:
    00208                                                         outlet_int(x->out, x->data->get_state(date)); // output
    00209                                                         break;
    00210                                         }
    00211                                 }
    00212                         }
    00213                         else
    00214                                 object_error((t_object *)x,"Data of Oracle %s busy",x->oname->s_name);
    00215                         ATOMIC_DECREMENT(&(((t_OMax_data *)(x->dataname->s_thing))->readcount));
    00216                 }
    00217         }
    00218
    00222         void OMax_date2state_setname(t_OMax_date2state *x, t_symbol *s)
    00223         {
    00224                 x->oname = s;
    00225                 x->obound = FALSE;
    00226                 OMax_date2state_bind(x);
    00227         }
    00228
    00230 }
    00231
    00232 #endif