[Gridflow-cvs] [svn] commit: r6106 - in /trunk/src: grid.cxx gridflow.cxx gridflow.hxx source_filter.rb

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Sun Aug 15 01:03:29 EDT 2010


Author: matju
Date: Sun Aug 15 01:03:27 2010
New Revision: 6106

Log:
do not use string("_0_foo") at runtime, replacing it by insel(0,gensym("foo")), which is a pair<int,t_symbol *>
however, C++ function names retain the old syntax, as does much of source_filter

Modified:
    trunk/src/grid.cxx
    trunk/src/gridflow.cxx
    trunk/src/gridflow.hxx
    trunk/src/source_filter.rb

Modified: trunk/src/grid.cxx
==============================================================================
--- trunk/src/grid.cxx (original)
+++ trunk/src/grid.cxx Sun Aug 15 01:03:27 2010
@@ -124,7 +124,7 @@
 #endif
 }
 
-#define CATCH_IT catch (Barf &slimy) {slimy.error(parent->bself,-1,"grid");}
+#define CATCH_IT catch (Barf &slimy) {slimy.error(parent->bself,-1,s_grid);}
 
 template <class T> void GridInlet::flow(long n, T *data) {
 	CHECK_BUSY(inlet); CHECK_TYPE(*data,sender->nt); CHECK_ALIGN(data,sender->nt);

Modified: trunk/src/gridflow.cxx
==============================================================================
--- trunk/src/gridflow.cxx (original)
+++ trunk/src/gridflow.cxx Sun Aug 15 01:03:27 2010
@@ -67,26 +67,15 @@
 #undef FOO
 
 Barf::Barf(const char *s, ...) {
-    ostringstream os;
-    va_list ap;
-    va_start(ap,s);
-    voprintf(os,s,ap);
-    va_end(ap);
-    text = os.str();
-}
+    ostringstream os; va_list ap; va_start(ap,s  ); voprintf(os,s  ,ap);
+    va_end(ap); text = os.str();}
 Barf::Barf(const char *file, int line, const char *func, const char *fmt, ...) {
-    ostringstream os;
-    va_list ap;
-    va_start(ap,fmt);
-    voprintf(os,fmt,ap);
-    //oprintf(os,"\n%s:%d:in `%s'",file,line,func);
-    va_end(ap);
-    text = os.str();
-}
-
-void Barf::error(BFObject *bself, int winlet, const char *selector) {
-	if (!bself) ::error("[???] inlet %d method %s: %s"                              ,winlet,selector?selector:"(???)",text.data());
-	else    pd_error(bself,"%s inlet %d method %s: %s",bself->binbuf_string().data(),winlet,selector?selector:"(???)",text.data());
+    ostringstream os; va_list ap; va_start(ap,fmt); voprintf(os,fmt,ap); //oprintf(os,"\n%s:%d:in `%s'",file,line,func);
+    va_end(ap); text = os.str();}
+
+void Barf::error(BFObject *bself, int winlet, t_symbol *sel) {
+	if (!bself) ::error("[???] inlet %d method %s: %s"                              ,winlet,sel?sel->s_name:"(???)",text.data());
+	else    pd_error(bself,"%s inlet %d method %s: %s",bself->binbuf_string().data(),winlet,sel?sel->s_name:"(???)",text.data());
 }
 void Barf::error(t_symbol *s, int argc, t_atom *argv) {
 	ostringstream os; os << s->s_name;
@@ -530,16 +519,21 @@
 #define pd_class(x) (*(t_pd *)x)
 #define pd_classname(x) (fclasses_pd[pd_class(x)]->name.data())
 
-static FMethod method_lookup (FClass *fclass, const char *sel) {
-	string s = sel;
+// when winlet==0  : the message came to the receiver of the object (by left inlet or by receive-symbol)
+// when winlet>0   : the message came through a proxy (non-left inlet)
+// when winlet==-1 : \decl n ... for multi-inlet methods
+// when winlet==-2 : \decl void anything : matches anything in all inlets (as a fallback)
+//		     \decl ___get : attribute lister defined by source_filter
+//                   other : attribute readers
+static FMethod method_lookup (FClass *fclass, int winlet, t_symbol *sel) {
 	typeof(fclass->methods) &m = fclass->methods;
-	typeof(m.begin()) it = m.find(s);
+	typeof(m.begin()) it = m.find(insel(winlet,sel));
 	if (it!=m.end()) return it->second;
-	if (fclass->super) return method_lookup(fclass->super,sel);
+	if (fclass->super) return method_lookup(fclass->super,winlet,sel);
 	return 0;
 }
-static FMethod method_lookup (BFObject *bself, const char *sel) {
-	return method_lookup(fclasses_pd[pd_class(bself)],sel);
+static FMethod method_lookup (BFObject *bself, int winlet, t_symbol *sel) {
+	return method_lookup(fclasses_pd[pd_class(bself)],winlet,sel);
 }
 
 void call_super(int argc, t_atom *argv) {/* unimplemented */}
@@ -554,28 +548,25 @@
 };
 
 static t_class *BFProxy_class;
+static t_symbol *s_loadbang;
 
 static void BFObject_loadbang (BFObject *bself) {
-    try {
-	FMethod m = method_lookup(bself,"_0_loadbang");
-	m(bself->self,0,0);
-    } catch (Barf &oozy) {oozy.error(bself,0,"loadbang");}
-}
-
-static void BFObject_anything (BFObject *bself, int winlet, t_symbol *selector, int ac, t_atom2 *at) {
+    try {FMethod m = method_lookup(bself,0,s_loadbang); m(bself->self,0,0);}
+    catch (Barf &oozy) {oozy.error(bself,0,s_loadbang);}
+}
+
+static void BFObject_anything (BFObject *bself, int winlet, t_symbol *s, int ac, t_atom2 *at) {
     #ifdef DES_BUGS
 	post("BFObject_anything bself=%08lx magic=%08lx self=%08lx",long(bself),bself->magic,long(bself->self));
     #endif
-    const char *s = selector->s_name;
     try {
 	t_atom2 argv[ac+2]; for (int i=0; i<ac; i++) argv[i+2] = at[i];
 	int argc = handle_braces(ac,argv+2);
 	FMethod m;
-	char buf[64], bof[64];
-	sprintf(buf,"_%d_%s",winlet,s); m=method_lookup(bself,buf);                 if (m) {m(bself->self,argc+0,argv+2); return;}
-	sprintf(bof,"_n_%s",        s); m=method_lookup(bself,bof); argv[1]=winlet; if (m) {m(bself->self,argc+1,argv+1); return;}
-	m = method_lookup(bself,"anything");      if (m) {argv[0]=winlet; argv[1]=selector; m(bself->self,argc+2,argv+0); return;}
-	pd_error((t_pd *)bself, "method '%s' not found for inlet %d in class '%s'",s,winlet,pd_classname(bself));
+	m=method_lookup(bself,winlet,s);                                    if (m) {m(bself->self,argc+0,argv+2); return;}
+	m=method_lookup(bself,-1,s); argv[1]=winlet;                        if (m) {m(bself->self,argc+1,argv+1); return;}
+	m = method_lookup(bself,-2,&s_anything); if (m) {argv[0]=winlet; argv[1]=s; m(bself->self,argc+2,argv+0); return;}
+	pd_error((t_pd *)bself, "method '%s' not found for inlet %d in class '%s'",s->s_name,winlet,pd_classname(bself));
     } catch (Barf &oozy) {oozy.error(bself,winlet,s);}
 }
 static void BFObject_anything0 (BFObject *self, t_symbol *s, int argc, t_atom2 *argv) {
@@ -640,7 +631,7 @@
 	try {
 	     delete bself->self;
 	     bself->self = (FObject *)0xdeadbeef;
-	} catch (Barf &oozy) {oozy.error(bself,-1,"destructor");}
+	} catch (Barf &oozy) {oozy.error(bself,-1,gensym("destructor"));}
 }
 
 //****************************************************************
@@ -786,9 +777,9 @@
 	fclasses_pd[fclass->bfclass] = fclass;
 	t_class *b = fclass->bfclass;
 	class_addanything(b,t_method(BFObject_anything0));
-	FMethod m = method_lookup(fclass,"_0_loadbang");
+	FMethod m = method_lookup(fclass,0,s_loadbang);
 	//post("class %s loadbang %08x",name,long(m));
-	if (m) class_addmethod(fclass->bfclass,t_method(BFObject_loadbang),gensym("loadbang"),A_NULL);
+	if (m) class_addmethod(fclass->bfclass,t_method(BFObject_loadbang),s_loadbang,A_NULL);
 }
 
 //static inline const t_atom &convert(const t_atom &x, const t_atom *foo) {return x;}
@@ -851,11 +842,11 @@
 	FClass *fc = fclasses_pd[pd_class(bself)];
 	if (!s) {
 		foreach(attr,fc->attrs) {
-			t_atom2 a[1] = {gensym((char *)attr->second->name.data())};
+			t_atom2 a[1] = {gensym((char *)attr->second->name->s_name)};
 			pd_typedmess((t_pd *)bself,gensym("get"),1,a);
 		}
 	} else {
-		FMethod m = method_lookup(bself,"___get");
+		FMethod m = method_lookup(bself,-2,gensym("___get"));
 		if (!m) RAISE("missing ___get");
 		t_atom2 a[1] = {s}; m(this,1,a);
 	}
@@ -863,10 +854,10 @@
 \def 0 help () {
 	FClass *fc = fclasses_pd[pd_class(bself)];
 	post("attributes (");
-	foreach(attr,fc->attrs) post("    %s %s;",attr->second->type.data(),attr->second->name.data());
+	foreach(attr,fc->attrs) post("    %s %s;",attr->second->type.data(),attr->second->name->s_name);
 	post(")");
 	post("methods (");
-	foreach(meth,fc->methods) post("    %s",meth->first.data());
+	foreach(meth,fc->methods) post("    %d %s",meth->first.first,meth->first.second->s_name);
 	post(")");
 }
 \classinfo {}
@@ -1018,6 +1009,7 @@
 // (segfaults), in addition to libraries not being canvases ;-)
 // AND ALSO, CONTRARY TO WHAT m_pd.h SAYS, open_via_path()'s args are reversed!!!
 extern "C" void gridflow_setup () {
+    s_loadbang = gensym("loadbang");
     post("GridFlow " GF_VERSION ", Copyright (c) 2001-2010 Mathieu Bouchard");
     post("GridFlow was compiled on "__DATE__", "__TIME__);
     //std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
@@ -1100,7 +1092,7 @@
 		);
 	delete[] dirresult;
 	delete[] dirname;
-    } catch (Barf &oozy) {oozy.error(0,-1,(char *)0);}
+    } catch (Barf &oozy) {oozy.error(0,-3,(t_symbol *)0);}
     signal(SIGSEGV,SIG_DFL);
     signal(SIGABRT,SIG_DFL);
     signal(SIGILL,SIG_DFL);

Modified: trunk/src/gridflow.hxx
==============================================================================
--- trunk/src/gridflow.hxx (original)
+++ trunk/src/gridflow.hxx Sun Aug 15 01:03:27 2010
@@ -50,6 +50,8 @@
 using std::vector;
 using std::ostream;
 using std::ostringstream;
+using std::pair;
+typedef pair<int,t_symbol *> insel; // inlet-selector compound
 
 #ifndef a_float
 #define a_float    a_w.w_float
@@ -192,8 +194,8 @@
 struct Barf {
   string text;
   Barf(const char *s, ...);
-  Barf(const char *file, int line, const char *func, const char *s, ...);
-  void error(BFObject *self, int winlet, const char *selector);
+  Barf(const char *file, int line, const char *func, const char *fmt, ...);
+  void error(BFObject *self, int winlet, t_symbol *selector);
   void error(t_symbol *s, int argc, t_atom *argv);
   ~Barf() {}
 };
@@ -729,9 +731,9 @@
 // you shouldn't use MethodDecl directly (used by source_filter.rb)
 struct MethodDecl {const char *selector; FMethod method;};
 struct AttrDecl {
-	string name;
+	t_symbol *name;
 	string type;
-	AttrDecl(string name_, string type_) {name=name_; type=type_;}
+	AttrDecl(t_symbol *name_, string type_) {name=name_; type=type_;}
 };
 typedef FObject *(*t_allocator)(BFObject *,MESSAGE3);
 struct FClass {
@@ -742,8 +744,8 @@
 	int noutlets;
 	t_class *bfclass;
 	string name;
-	map<string,FMethod> methods;
-	map<string,AttrDecl *> attrs;
+	map<pair<int,t_symbol *>,FMethod> methods; // (inlet,selector) -> method
+	map<t_symbol *,AttrDecl *> attrs;
 };
 
 void fclass_install(FClass *fc, FClass *super);

Modified: trunk/src/source_filter.rb
==============================================================================
--- trunk/src/source_filter.rb (original)
+++ trunk/src/source_filter.rb Sun Aug 15 01:03:27 2010
@@ -25,7 +25,7 @@
 $exit = 0
 
 ClassDecl = Struct.new(:name,:supername,:methods,:grins,:attrs,:info)
-MethodDecl = Struct.new(:rettype,:selector,:args,:minargs,:maxargs,:where,:continue)
+MethodDecl = Struct.new(:rettype,:inlet,:selector,:args,:minargs,:maxargs,:where,:continue)
 Arg  = Struct.new(:type,:name,:default)
 Attr = Struct.new(:type,:name,:default,:virtual)
 
@@ -41,6 +41,7 @@
 		return true
 	end
 	attr_accessor :done
+	def selector2; if inlet==-2 then selector elsif inlet==-1 then "_n_#{selector}" else "_#{inlet}_#{selector}" end end
 end
 
 class Arg
@@ -75,12 +76,10 @@
 	/^(\w+(?:\s*\*)?)\s+(\w+)\s*\(([^\)]*)\)\s*(#{term})/.match line or
 		raise "syntax error #{where} #{line}"
 	rettype,selector,args,continue = $1,$2,$3,$4,$6
-	if /^\d+$/ =~ rettype then
-		selector = "_"+rettype+"_"+selector
-		rettype = "void"
-	end
+	if /^\d+$/ =~ rettype then inlet = rettype; rettype = "void"
+	elsif    rettype=="n" then inlet = -1 else inlet = -2 end
 	args,minargs,maxargs = parse_args args
-	MethodDecl.new(rettype,selector,args,minargs,maxargs,where,continue)
+	MethodDecl.new(rettype,inlet,selector,args,minargs,maxargs,where,continue)
 end
 
 def parse_args(args)
@@ -132,14 +131,14 @@
 	raise "missing \\class #{where}" if not qlass or not ClassDecl===qlass
 	classname = qlass.name
 	m = parse_methoddecl(line,"(;\s*|\\{?.*)$")
-	qlass.methods[m.selector] = m
+	qlass.methods[m.selector2] = m
 	if /^\{/ =~ m.continue
 	  handle_def(line,true)
 	else
-	  Out.print "#{m.rettype} #{m.selector}("
+	  Out.print "#{m.rettype} #{m.selector2}("
 	  Out.print "VA" if m.maxargs<0
 	  Out.print unparse_args m.args if m.args.length>0
-	  Out.print "); static void #{m.selector}_wrap(#{classname} *self, VA);"
+	  Out.print "); static void #{m.selector2}_wrap(#{classname} *self, VA);"
 	end
 end
 
@@ -166,8 +165,8 @@
 	raise "missing \\class #{where}" if not qlass or not ClassDecl===qlass
 	classname = qlass.name
 	n = m
-	if qlass.methods[m.selector]
-		m = qlass.methods[m.selector]
+	if qlass.methods[m.selector2]
+		m = qlass.methods[m.selector2]
 		if !m===n then
 			STDERR.puts "ERROR: def does not match decl:"
 			STDERR.puts "#{m.where}: \\decl #{m.inspect}"
@@ -175,24 +174,24 @@
 			$exit = 1
 		end
 	else
-		qlass.methods[m.selector] = m
+		qlass.methods[m.selector2] = m
 	end
 	if in_class_block then Out.print "static void " else Out.print "void #{classname}::" end
-	Out.print "#{m.selector}_wrap(#{classname} *self, VA) {"
-	if /^_(\d+)_(\w+)$/ =~ m.selector then context = "inlet #{$1} method #{$2}" else context = m.selector end
-	Out.print "DEF_IN(\"method #{m.selector}\");"
+	Out.print "#{m.selector2}_wrap(#{classname} *self, VA) {"
+	if /^_(\d+)_(\w+)$/ =~ m.selector2 then context = "inlet #{$1} method #{$2}" else context = m.selector2 end
+	Out.print "DEF_IN(\"method #{m.selector2}\");"
 	Out.print "#{m.rettype} foo;" if m.rettype!="void"
 	check_argc m
 	Out.print "foo = " if m.rettype!="void"
-	Out.print " self->#{m.selector}("
+	Out.print " self->#{m.selector2}("
 	pass_args m
 	Out.print m.rettype+" "
-	Out.print "#{classname}::" unless in_class_block
-	Out.print m.selector+"("
+	Out.print classname+"::" unless in_class_block
+	Out.print m.selector2+"("
 	Out.print "VA" if m.maxargs<0
 	Out.print unparse_args(n.args,false) if m.args.length>0
-	Out.print ")#{term} "
-	qlass.methods[m.selector].done=true
+	Out.print ")"+term+" "
+	qlass.methods[m.selector2].done=true
 end
 
 def handle_constructor(line)
@@ -229,8 +228,9 @@
 	get << "RAISE(\"unknown attr %s\",s->s_name); outlet_anything(out[noutlets-1],s,1,a);}"
 	handle_def get if frame.attrs.size>0
 	Out.print "void #{frame.name}_startup (FClass *fclass) {"
-	frame.methods.each {|name,method| Out.print "fclass->methods[\"#{name}\"] = FMethod(#{frame.name}::#{method.selector}_wrap);" }
-	frame.attrs.each   {|name,attr|   Out.print "fclass->  attrs[\"#{name}\"] = new AttrDecl(\"#{name}\",\"#{attr.type}\");" }
+	frame.methods.each {|name,method|
+	 Out.print "fclass->methods[insel(#{method.inlet},gensym(\"#{method.selector}\"))] = FMethod(#{frame.name}::#{method.selector2}_wrap);" }
+	frame.attrs.each   {|name,attr|   Out.print "fclass->  attrs[gensym(\"#{name}\")] = new AttrDecl(gensym(\"#{name}\"),\"#{attr.type}\");" }
 	Out.print line.chomp
 end
 



More information about the Gridflow-cvs mailing list