WoMax Documentation

# PC/OMax.state2date/OMax.state2date.c

    00001 /*--------------------------------------
    00002  * OMax.state2date - OMax.state2date.c
    00003  * Created on 26/03/09 by BenCello
    00004  *--------------------------------------*/
    00005
    00007
    00009
    00010
    00011 #ifndef __OMax_state2date_MAX_API_
    00012 #define __OMax_state2date_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_state2date
    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_enddate;
    00039                 void*           out_begindate;
    00040         } t_OMax_state2date;
    00041
    00043
    00045         // Prototypes //
    00047
    00048         // Standard Max5 methodes
    00049         void *OMax_state2date_new(t_symbol *s, long argc, t_atom *argv);
    00050         void OMax_state2date_free(t_OMax_state2date *x);
    00051         void OMax_state2date_assist(t_OMax_state2date *x, void *b, long io, long index, char *s);
    00052
    00053         // Input/ouput routines
    00054         void OMax_state2date_do(t_OMax_state2date *x, long statnb);
    00055         void OMax_state2date_setname(t_OMax_state2date *x, t_symbol *newname);
    00056
    00057         // Internal routines
    00058         t_symbol * OMax_state2date_name(t_symbol * oname);
    00059         bool OMax_state2date_bind(t_OMax_state2date *x);
    00060
    00061         // Global class pointer variable
    00062         t_class* OMax_state2date_class;
    00063
    00065         // Functions //
    00067
    00068         int main(void)
    00069         {
    00070                 // object initialization, NEW STYLE
    00071                 t_class *c;
    00072
    00073                 c = class_new("OMax.state2date", (method)OMax_state2date_new, (method)OMax_state2date_free, (long)sizeof(t_OMax_state2date),
    00074                                           0L /* leave NULL!! */, A_GIMME, 0);
    00075
    00076                 // assistance
    00077                 class_addmethod(c, (method)OMax_state2date_assist,"assist",A_CANT, 0);
    00078
    00079                 // input methods
    00080                 class_addmethod(c, (method)OMax_state2date_do, "int", 0);
    00081                 class_addmethod(c, (method)OMax_state2date_setname, "set", A_DEFSYM, 0);
    00082
    00083                 class_register(CLASS_BOX, c); /* CLASS_NOBOX */
    00084                 OMax_state2date_class = c;
    00085
    00086                 return 0;
    00087         }
    00088
    00090
    00091
    00094         void *OMax_state2date_new(t_symbol *s, long argc, t_atom *argv)
    00095         {
    00096                 t_OMax_state2date *x = NULL;
    00097
    00098                 // object instantiation, NEW STYLE
    00099                 if (x = (t_OMax_state2date *)object_alloc(OMax_state2date_class))
    00100                 {
    00101                         // outlet init
    00102                         x->out_begindate = intout(x);
    00103                         x->out_enddate = intout(x);
    00104
    00106                         x->obound = FALSE;
    00107                         if (argc == 0)
    00108                                 object_error((t_object *)x,"Missing name of the Oracle data to read");
    00109                         else
    00110                         {
    00111                                 if (argv->a_type != A_SYM)
    00112                                         object_error((t_object *)x,"First argument must be a symbol (name of an existing Oracle)");
    00113                                 else
    00114                                         x->oname = atom_getsym(argv);
    00115                         }
    00116
    00117                         // color
    00118                         t_object *box;
    00119                         t_jrgba colorvals;
    00120                         jrgba_set(&colorvals, 0.30, 1.0, 0.15, 1.0);
    00121                         object_obex_lookup((t_object *)x, gensym("#B"), &box);
    00122                         jbox_set_color(box, &colorvals);
    00123                 }
    00124                 return (x);
    00125         }
    00126
    00129         void OMax_state2date_free(t_OMax_state2date *x)
    00130         {
    00131                 ;
    00132         }
    00133
    00136         void OMax_state2date_assist(t_OMax_state2date *x, void *b, long io, long index, char *s)
    00137         {
    00138                 switch (io)
    00139                 {
    00140                         case 1: // inlet
    00141                                 sprintf(s, "state number, set [symbol] changes sequence to read");
    00142                                 break;
    00143                         case 2: // outlet
    00144                                 switch(index)
    00145                         {
    00146                                 case 0: // leftmost
    00147                                         sprintf(s, "end date");
    00148                                         break;
    00149                                 case 1:
    00150                                         sprintf(s, "begin date");
    00151                                         break;
    00152                         }
    00153                 }
    00154         }
    00155
    00157
    00159
    00160
    00163         t_symbol * OMax_state2date_name(t_symbol * oname)
    00164         {
    00165                 char dataname[128];
    00166                 strcpy(dataname,oname->s_name);
    00168                 strcat(dataname,"_data");
    00169                 return gensym(dataname);
    00170         }
    00171
    00174         bool OMax_state2date_bind(t_OMax_state2date *x)
    00175         {
    00177                 if (x->obound == FALSE)
    00178                 {
    00180                         x->dataname = OMax_state2date_name(x->oname);
    00181                         if ((x->dataname->s_thing) && (ob_sym(x->dataname->s_thing) == gensym("OMax.data")))
    00182                         {
    00183                                 x->data = &(((t_OMax_data*)(x->dataname->s_thing))->data);
    00184                                 // If binding is ok, then don't do it next time.
    00185                                 x->obound = TRUE;
    00187                                 x->datatype = ((t_OMax_data*)(x->dataname->s_thing))->datatype;
    00188                         }
    00189                         else
    00190                         {
    00191                                 object_error((t_object *)x,"No data for Oracle %s declared", x->oname->s_name);
    00192                         }
    00193                 }
    00194                 return x->obound;
    00195         }
    00196
    00198
    00200
    00201
    00205         void OMax_state2date_do(t_OMax_state2date *x, long statnb)
    00206         {
    00207                 if (OMax_state2date_bind(x))
    00208                 {
    00209                         ATOMIC_INCREMENT(&(((t_OMax_data *)(x->dataname->s_thing))->readcount));
    00210                         if(((t_OMax_data *)(x->dataname->s_thing))->wflag == FALSE)
    00211                         {
    00212                                 if ((statnb>=0) && (statnb<(long)(x->data->get_size())))
    00213                                 {
    00214                                         switch(x->datatype)
    00215                                         {
    00216                                                 case LETTERS:
    00217                                                         outlet_int(x->out_begindate,statnb-1); // output begin
    00218                                                         outlet_int(x->out_enddate,statnb); // output end
    00219                                                         break;
    00220                                                 default:
    00221                                                         int date = (*x->data).get_date(statnb);
    00222                                                         outlet_int(x->out_begindate,date); // output begin
    00223                                                         outlet_int(x->out_enddate,date+(*x->data)[statnb]->get_duration()); // output end
    00224                                                         break;
    00225                                         }
    00226                                 }
    00227                         }
    00228                         else
    00229                                 object_error((t_object *)x,"Data of Oracle %s busy",x->oname->s_name);
    00230                         ATOMIC_DECREMENT(&(((t_OMax_data *)(x->dataname->s_thing))->readcount));
    00231                 }
    00232         }
    00233
    00237         void OMax_state2date_setname(t_OMax_state2date *x, t_symbol *s)
    00238         {
    00239                 x->oname = s;
    00240                 x->obound = FALSE;
    00241                 OMax_state2date_bind(x);
    00242         }
    00243
    00245 }
    00246
    00247 #endif