[Gridflow-cvs] [svn] commit: r3563 - in /trunk/base: grid.h source_filter.rb

gridflow-cvs at artengine.ca gridflow-cvs at artengine.ca
Wed Apr 16 16:33:50 EDT 2008


Author: matju
Date: Wed Apr 16 16:33:50 2008
New Revision: 3563

Log:
introducing type t_atom2 and macros MESSAGE and MESSAGE2

Modified:
    trunk/base/grid.h
    trunk/base/source_filter.rb

Modified: trunk/base/grid.h
==============================================================================
--- trunk/base/grid.h (original)
+++ trunk/base/grid.h Wed Apr 16 16:33:50 2008
@@ -25,7 +25,7 @@
 #define __GF_GRID_H
 #include "../gridflow2.h"
 
-#define GF_VERSION "0.9.1"
+#define GF_VERSION "0.9.2"
 
 #include <stdio.h>
 extern "C" void *gfmalloc(size_t n);
@@ -269,6 +269,12 @@
 static inline R    cmp(R a, R b) {return R::value(rb_funcall(a.r,SI(<=>),1,b.r));}
 #endif // USE_RUBY
 
+// trick to be able to define methods in t_atom
+struct t_atom2 : t_atom {
+	bool operator == (t_symbol *b) {return this->a_type==A_SYMBOL && this->a_w.w_symbol==b;}
+	bool operator != (t_symbol *b) {return !(*this==b);}
+};
+
 //****************************************************************
 
 //template <class T> class P : T * {};
@@ -422,6 +428,7 @@
 };
 
 NumberTypeE NumberTypeE_find (string sym);
+NumberTypeE NumberTypeE_find (const t_atom &sym);
 #ifdef USE_RUBY
 NumberTypeE NumberTypeE_find (Ruby sym);
 #endif
@@ -560,6 +567,9 @@
 #ifdef USE_RUBY
 static inline NumberTypeE convert(Ruby x, NumberTypeE *bogus) {return NumberTypeE_find(x);}
 #endif
+static inline NumberTypeE convert(const t_atom &x, NumberTypeE *bogus) {
+	if (x.a_type!=A_SYMBOL) RAISE("expected number-type"); return NumberTypeE_find(string(x.a_w.w_symbol->s_name));}
+
 
 #ifdef USE_RUBY
 static Numop *convert(Ruby x, Numop **bogus) {
@@ -584,8 +594,10 @@
 		init(dim,nt);
 		if (clear) {long size = bytes(); CLEAR((char *)data,size);}
 	}
-	Grid(Ruby x) { state=1; init_from_ruby(x); }
-	Grid(int n, Ruby *a, NumberTypeE nt_=int32_e) {state=1; init_from_ruby_list(n,a,nt_);}
+	Grid(Ruby          x) {state=1; init_from_ruby(x);}
+	Grid(const t_atom &x) {state=1; init_from_atom(x);}
+	Grid(int n, Ruby   *a, NumberTypeE nt_=int32_e) {state=1; init_from_ruby_list(n,a,nt_);}
+	Grid(int n, t_atom *a, NumberTypeE nt_=int32_e) {state=1; init_from_list(n,a,nt_);}
 	template <class T> Grid(P<Dim> dim, T *data) {
 		state=0; this->dim=dim;
 		this->nt=NumberTypeE_type_of((T *)0);
@@ -615,7 +627,9 @@
 		//fprintf(stderr,"rdata=%p data=%p align=%d\n",rdata,data,align);
 	}
 	void init_from_ruby(Ruby x);
-	void init_from_ruby_list(int n, Ruby *a, NumberTypeE nt=int32_e);
+	void init_from_atom(const t_atom &x);
+	void init_from_ruby_list(int n, Ruby   *a, NumberTypeE nt=int32_e);
+	void init_from_list(     int n, t_atom *a, NumberTypeE nt=int32_e);
 };
 
 static inline Grid *convert (Ruby r, Grid **bogus) {return r?new Grid(r):0;}
@@ -748,8 +762,10 @@
 #else
 #define GRIN(TB,TS,TI,TL,TF,TD) {TB,TS,TI,TF}
 #endif // HAVE_LITE
+#define MESSAGE t_symbol *sel, int argc, t_atom2 *argv
+#define MESSAGE2 sel,argc,argv
 struct FClass {
-	void *(*allocator)(); // returns a new C++ object
+	void *(*allocator)(MESSAGE); // returns a new C++ object
 	void (*startup)(Ruby rself); // initializer for the Ruby class
 	const char *name; // C++/Ruby name (not PD name)
 	int methodsn; MethodDecl *methods; // C++ -> Ruby methods
@@ -827,7 +843,7 @@
 // represents objects that have inlets/outlets
 \class FObject : CObject {
 	BFObject *bself; // point to PD peer
-	FObject() : bself(0) {}
+	FObject(MESSAGE) : bself(0) {}
 	template <class T> void send_out(int outlet, int argc, T *argv) {
 		t_atom foo[argc];
 		for (int i=0; i<argc; i++) SETFLOAT(&foo[i],argv[i]);
@@ -842,7 +858,7 @@
 	// Make sure you distinguish #close/#delete, and C++'s delete. The first
 	// two are quite equivalent and should never make an object "crashable".
 	// C++'s delete is called by Ruby's garbage collector or by PureData's delete.
-	GridObject() {}
+	GridObject(MESSAGE) : FObject(MESSAGE2) {}
 	~GridObject() {}
 	bool is_busy_except(P<GridInlet> gin) {
 		for (uint32 i=0; i<in.size(); i++)
@@ -897,7 +913,7 @@
 	FILE *f;
 	NumberTypeE cast;
 	long frame;
-	Format() : mode(0), fd(-1), f(0), cast(int32_e), frame(0) {}
+	Format(MESSAGE) : GridObject(MESSAGE2), mode(0), fd(-1), f(0), cast(int32_e), frame(0) {}
 	\decl void initialize (t_symbol *mode, ...);
 	\decl 0 open (t_symbol *mode, string filename);
 	\decl 0 close ();

Modified: trunk/base/source_filter.rb
==============================================================================
--- trunk/base/source_filter.rb (original)
+++ trunk/base/source_filter.rb Wed Apr 16 16:33:50 2008
@@ -28,13 +28,13 @@
 $exit = 0
 
 ClassDecl = Struct.new(:name,:supername,:methods,:grins,:attrs,:info)
-MethodDecl = Struct.new(:rettype,:selector,:arglist,:minargs,:maxargs,:where,:static)
+MethodDecl = Struct.new(:rettype,:selector,:arglist,:minargs,:maxargs,:where)
 Arg  = Struct.new(:type,:name,:default)
 Attr = Struct.new(:type,:name,:default,:virtual)
 
 class MethodDecl
 	def ==(o)
-		return false unless rettype==o.rettype && static==o.static &&
+		return false unless rettype==o.rettype &&
 			maxargs==o.maxargs
 		arglist.each_index{|i| arglist[i] == o.arglist[i] or return false }
 		return true
@@ -72,15 +72,15 @@
 end
 
 def parse_methoddecl(line,term)
-	/^(static\s)?\s*(\w+)\s+(\w+)\s*\(([^\)]*)\)\s*#{term}/.match line or
+	/^(\w+)\s+(\w+)\s*\(([^\)]*)\)\s*#{term}/.match line or
 		raise "syntax error #{where} #{line}"
-	static,rettype,selector,arglist = $1,$2,$3,$4
+	rettype,selector,arglist = $1,$2,$3,$4
 	if /^\d+$/ =~ rettype then
 		selector = "_"+rettype+"_"+selector
 		rettype = "void"
 	end
 	arglist,minargs,maxargs = parse_arglist arglist
-	MethodDecl.new(rettype,selector,arglist,minargs,maxargs,where,static)
+	MethodDecl.new(rettype,selector,arglist,minargs,maxargs,where)
 end
 
 def parse_arglist(arglist)
@@ -133,12 +133,10 @@
 
 def handle_decl(line)
 	frame = $stack[-1]
-	raise "missing \\class #{where}" if
-		not frame or not ClassDecl===frame
+	raise "missing \\class #{where}" if not frame or not ClassDecl===frame
 	classname = frame.name
 	m = parse_methoddecl(line,";\s*$")
 	frame.methods[m.selector] = m
-	Out.print "static " if m.static
 	Out.print "#{m.rettype} #{m.selector}(VA"
 	Out.print ", #{unparse_arglist m.arglist}" if m.arglist.length>0
 	Out.print ");"
@@ -170,10 +168,7 @@
 	Out.print "if (argc<#{m.minargs}"
 	Out.print "||argc>#{m.maxargs}" if m.maxargs!=-1
 	Out.print ") RAISE(\"got %d args instead of %d..%d in %s\",argc,#{m.minargs},#{m.maxargs},methodspec);"
-	error = proc {|x,y|
-		"RAISE(\"got %s instead of #{x} in %s\","+
-		"rb_str_ptr(rb_inspect(rb_obj_class(#{y}))),methodspec)"
-	}
+	#error = proc {|x,y| "RAISE(\"got %s instead of #{x} in %s\",rb_str_ptr(rb_inspect(rb_obj_class(#{y}))),methodspec)" }
 	Out.print "foo = " if m.rettype!="void"
 	Out.print " self->#{m.selector}(argc,argv"
 	m.arglist.each_with_index{|arg,i|
@@ -183,15 +178,9 @@
 			Out.print ",convert(argv[#{i}],(#{arg.type}*)0)"
 		end
 	}
-        if m.rettype=="R"
-	  Out.print ").r;"
-	else
-	  Out.print ");"
-	end
-	Out.print "} catch (Barf *oozy) {rb_raise(rb_eArgError,\"%s\",oozy->text);}"
+	Out.print ");} catch (Barf *oozy) {rb_raise(rb_eArgError,\"%s\",oozy->text);}"
 	case m.rettype
 	when "void"; Out.print "return Qnil;"
-	when "Ruby"; Out.print "return foo;"
 	else         Out.print "return R(foo).r;"
 	end
 	Out.print "} #{m.rettype} #{classname}::#{m.selector}(VA"
@@ -200,12 +189,38 @@
 	qlass.methods[m.selector].done=true
 end
 
+def handle_constructor(line)
+	frame = $stack[-1]
+	raise "missing \\class #{where}" if not frame or not ClassDecl===frame
+	m = parse_methoddecl("void constructor"+line,"(.*)$")
+	Out.print "#{frame.name}(MESSAGE) : #{frame.supername}(MESSAGE2) {"
+	Out.print "static const char *methodspec = \"#{frame.name}::#{m.selector}(#{unparse_arglist m.arglist,false})\";"
+
+	Out.print "try {"
+	Out.print "if (argc<#{m.minargs}"
+	Out.print "||argc>#{m.maxargs}" if m.maxargs!=-1
+	Out.print ") RAISE(\"got %d args instead of %d..%d in %s\",argc,#{m.minargs},#{m.maxargs},methodspec);"
+	#error = proc {|x,y| "RAISE(\"got %s instead of #{x} in %s\",rb_str_ptr(rb_inspect(rb_obj_class(#{y}))),methodspec)" }
+	Out.print "#{m.selector}(sel,argc,argv"
+	m.arglist.each_with_index{|arg,i|
+		if arg.default then
+			Out.print ",argc<#{i+1}?#{arg.default}:convert(argv[#{i}],(#{arg.type}*)0)"
+		else
+			Out.print ",convert(argv[#{i}],(#{arg.type}*)0)"
+		end
+	}
+	Out.print ");} catch (Barf *oozy) {rb_raise(rb_eArgError,\"%s\",oozy->text);}}"
+	Out.print "#{m.rettype} #{m.selector}(MESSAGE"
+	Out.print ", #{unparse_arglist m.arglist}" if m.arglist.length>0
+	Out.print ") "+line[/\{.*/]
+end
+
 def handle_classinfo(line)
 	frame = $stack[-1]
 	cl = frame.name
 	line="{}" if /^\s*$/ =~ line
 	Out.print "static void #{cl}_startup (Ruby rself);"
-	Out.print "static void *#{cl}_allocator () {return new #{cl};}"
+	Out.print "static void *#{cl}_allocator (MESSAGE) {return new #{cl}(sel,argc,argv);}"
 	Out.print "static MethodDecl #{cl}_methods[] = {"
 	Out.print frame.methods.map {|foo,method|
 		c,s = frame.name,method.selector



More information about the Gridflow-cvs mailing list