WoMax Documentation OMax Logo

OMax.read.c

Go to the documentation of this file.
00001 /*----------------------------------
00002  * OMax.learn - OMax.read.c
00003  * Created on 13/03/09 by BenCello
00004  *----------------------------------*/
00005 
00007 
00009 
00010 
00011 #ifndef __OMax_read_MAX_API_
00012 #define __OMax_read_MAX_API_
00013 
00014 #include "Oracle_classes.hpp"
00015 #include "OMax.oracle.h"
00016 
00017 extern "C"
00018 {
00019         
00020 #include "ext.h"                                // standard Max include, always required
00021 #include "ext_obex.h"                   // required for new style Max object
00022 #include "jpatcher_api.h"               // required for the color
00023 #include "jgraphics.h"                  // required for the color
00024 
00029         typedef struct _OMax_read 
00030         {
00031                 t_object        ob;                     
00032                 t_symbol*       oname;          
00033                 bool            obound;         
00034                 O_oracle*       oracle;         
00035                 void*           out_statenb;
00036                 void*           out_trans;      
00037                 void*           out_suff;       
00038                 void*           out_rsuff;      
00039                 void*           out_bang;       
00040         } t_OMax_read;
00041         
00043         
00045         // Prototypes //
00047         
00048         // Standard Max5 methodes
00049         void *OMax_read_new(t_symbol *s, long argc, t_atom *argv);
00050         void OMax_read_free(t_OMax_read *x);
00051         void OMax_read_assist(t_OMax_read *x, void *b, long io, long index, char *s);
00052         
00053         // Input/ouput routines
00054         void OMax_read_read(t_OMax_read *x, long statnb);
00055         void OMax_read_setname(t_OMax_read *x, t_symbol *newname);
00056         
00057         // Internal routines
00058         bool OMax_read_bind(t_OMax_read *x);
00059         
00060         // Global class pointer variable
00061         t_class *OMax_read_class;
00062         
00064         // Functions //
00066         
00067         int main(void)
00068         {       
00069                 t_class *c;
00070                 
00071                 c = class_new("OMax.read", (method)OMax_read_new, (method)OMax_read_free, (long)sizeof(t_OMax_read), 
00072                                           0L /* leave NULL!! */, A_GIMME, 0);
00073                 
00074                 // assistance
00075                 class_addmethod(c, (method)OMax_read_assist,"assist",A_CANT, 0); 
00076                 
00077                 // input methods
00078                 class_addmethod(c, (method)OMax_read_read,"int", A_LONG, 0);
00079                 class_addmethod(c, (method)OMax_read_setname, "set", A_DEFSYM, 0);
00080                 
00081                 class_register(CLASS_BOX, c); /* CLASS_NOBOX */
00082                 OMax_read_class = c;
00083                 
00084                 return 0;
00085         }
00086         
00088 
00089         
00092         void *OMax_read_new(t_symbol *s, long argc, t_atom *argv)
00093         {
00094                 t_OMax_read *x = NULL;
00095                 
00096                 if (x = (t_OMax_read *)object_alloc(OMax_read_class))
00097                 {
00098                         // outlet init
00099                         x->out_statenb = intout(x); // rightmost
00100                         x->out_trans = listout(x);
00101                         x->out_suff = listout(x);
00102                         x->out_rsuff = listout(x);
00103                         x->out_bang = bangout(x);
00104                         
00106                         x->obound = FALSE;
00107                         if (argc == 0)
00108                                 object_error((t_object *)x,"Missing name of the Oracle 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_read_free(t_OMax_read *x)
00130         {
00131                 ;
00132         }
00133         
00136         void OMax_read_assist(t_OMax_read *x, void *b, long io, long index, char *s)
00137         {
00138                 switch (io)
00139                 {
00140             case 1: // inlets
00141                                 switch (index)
00142                         {
00143                                 case 0: // leftmost
00144                                         sprintf(s, "state to read, set [symbol] changes FO to read");
00145                                         break;
00146                         }
00147                 break;
00148             case 2: // outlets
00149                                 switch (index)
00150                         {
00151                                 case 0:
00152                                         sprintf(s, "bang when done");
00153                                         break;
00154                                 case 1:
00155                                         sprintf(s, "reversed suffixes");
00156                                         break;
00157                                 case 2: // leftmost
00158                                         sprintf(s, "suffix & lrs");
00159                                         break;
00160                                 case 3:
00161                                         sprintf(s, "list of transitions");
00162                                         break;
00163                                 case 4:
00164                                         sprintf(s, "state number");
00165                                         break;
00166                         }
00167                                 break;
00168                 }
00169         }
00170         
00172         
00174 
00175         
00178         bool OMax_read_bind(t_OMax_read *x)
00179         {
00181                 if (x->obound == FALSE)
00182                 {
00184                         if ((x->oname->s_thing) && (ob_sym(x->oname->s_thing) == gensym("OMax.oracle")))
00185                         {
00186                                 x->oracle = &(((t_OMax_oracle*)(x->oname->s_thing))->oracle);
00187                                 // If binding is ok, then don't do it next time.
00188                                 x->obound = TRUE;
00189                         }
00190                         else
00191                         {
00192                                 object_error((t_object *)x,"No oracle %s declared", x->oname->s_name);
00193                         }
00194                 }
00195                 return x->obound;
00196         }
00197         
00199         
00201 
00202         
00206         void OMax_read_setname(t_OMax_read *x, t_symbol *s)
00207         {
00208                 x->oname = s;
00209                 x->obound = FALSE;
00210                 OMax_read_bind(x);
00211         }
00212         
00216         void OMax_read_read(t_OMax_read *x, long statnb)
00217         {
00218                 if (OMax_read_bind(x))
00219                 {
00220                         ATOMIC_INCREMENT(&(((t_OMax_oracle *)(x->oname->s_thing))->readcount));
00221                         if(!(((t_OMax_oracle *)(x->oname->s_thing))->wflag))
00222                         {
00223                                 if ((statnb>=0) && (statnb<(long)(x->oracle->get_size())))
00224                                 {
00225                                         O_state state = *(*x->oracle)[statnb];
00226                                         ATOMIC_DECREMENT(&(((t_OMax_oracle *)(x->oname->s_thing))->readcount));
00227                                         
00228                                         // state number
00229                                         outlet_int(x->out_statenb, state.get_statenb()); // output
00230                                         
00231                                         // transitions
00232                                         char err;
00233                                         long i = 0;
00234                                         list<O_state*> trans = state.get_trans();
00235                                         list<O_state*>::iterator it;
00236                                         long vals[trans.size()];
00237                                         t_atom * trans_list = NULL;
00238                                         for (it = trans.begin(); it!=trans.end(); it++)
00239                                         {
00240                                                 vals[i]=(*it)->get_statenb();
00241                                                 i++;
00242                                         }
00243                                         atom_alloc_array((long)trans.size(), &i, &trans_list, &err);
00244                                         if(err)
00245                                         {
00246                                                 atom_setlong_array((long)trans.size(),trans_list,(long)trans.size(),vals);
00247                                                 outlet_list(x->out_trans, NULL, (short)trans.size(), trans_list); // output
00248                                         }
00249                                         
00250                                         // suffix
00251                                         t_atom * suff = NULL;
00252                                         atom_alloc_array(2, &i, &suff, &err);
00253                                         if((state.get_suffix().first!=NULL) && (i==2) && err)
00254                                         {
00255                                                         atom_setlong(suff, (state.get_suffix().first)->get_statenb());
00256                                                         atom_setlong(suff+1, state.get_suffix().second);
00257                                                         outlet_list(x->out_suff, NULL, 2, suff); // output
00258                                         }
00259                                         
00260                                         // reversed suffixes
00261                                         list<pair<O_state*,int> > rsuff = state.get_rsuff();
00262                                         list<pair<O_state*,int> >::iterator rsit = rsuff.begin();
00263                                         long rsvals[2];
00264                                         t_atom * rsuff_out = NULL;
00265                                         atom_alloc_array(2, &i, &rsuff_out, &err);
00266                                         if(err && i==2)
00267                                         {
00268                                                 for (rsit=rsuff.begin();rsit!=rsuff.end();rsit++)
00269                                                 {
00270                                                         rsvals[0]=(*rsit).first->get_statenb();
00271                                                         rsvals[1]=(*rsit).second;
00272                                                         atom_setlong_array(2,rsuff_out,2,rsvals);
00273                                                         outlet_list(x->out_rsuff, NULL, 2, rsuff_out); // output
00274                                                 }
00275                                         }
00276                                         
00277                                         // done
00278                                         outlet_bang(x->out_bang);
00279                                         
00280                                 }
00281                                 else
00282                                 {
00283                                         ATOMIC_DECREMENT(&(((t_OMax_oracle *)(x->oname->s_thing))->readcount));
00284                                         //object_post((t_object *)x,"State %ld outside Oracle %s bounds", statnb, x->oname->s_name);
00285                                 }
00286                         }
00287                         else
00288                         {
00289                                 ATOMIC_DECREMENT(&(((t_OMax_oracle *)(x->oname->s_thing))->readcount));
00290                                 object_error((t_object *)x,"Oracle %s busy",x->oname->s_name);
00291                         }
00292                 }
00293         }
00294         
00296         
00297 }
00298 
00299 #endif

Generated on 16 Mar 2010 for Benjamin Lévy by  doxygen 1.6.1