[Gridflow-cvs] [svn] commit: r6321 - in /trunk/src: classes1.cxx classes3.cxx grid.cxx gridflow.hxx opencv.cxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Tue Sep 21 20:49:06 EDT 2010


Author: matju
Date: Tue Sep 21 20:49:05 2010
New Revision: 6321

Log:
move PtrGrid::constraint() to P::but(); remove PtrGrid (replace by P<Grid>)

Modified:
    trunk/src/classes1.cxx
    trunk/src/classes3.cxx
    trunk/src/grid.cxx
    trunk/src/gridflow.hxx
    trunk/src/opencv.cxx

Modified: trunk/src/classes1.cxx
==============================================================================
--- trunk/src/classes1.cxx (original)
+++ trunk/src/classes1.cxx Tue Sep 21 20:49:05 2010
@@ -79,10 +79,10 @@
 	\attr NumberTypeE cast;
 	/*\attr*/ Dim dim; // size of grids to send
 	\attr bool per_message;
-	PtrGrid dim_grid;
+	P<Grid> dim_grid;
 	\constructor (...) {
 		per_message = true;
-		dim_grid.constrain(expect_dim_dim_list);
+		dim_grid.but(expect_dim_dim_list);
 		this->cast = argc>=2 ? NumberTypeE_find(argv[1]) : int32_e;
 		if (argc>2) RAISE("too many arguments");
 		if (argc>0 && argv[0]!=gensym("per_message")) {
@@ -335,8 +335,8 @@
 // in1: whatever nt
 // out0: same nt as in1
 \class GridStore : FObject {
-	PtrGrid r; // can't be \attr (why ?)
-	PtrGrid put_at; // can't be //\attr (why ?)
+	P<Grid> r; // can't be \attr (why ?)
+	P<Grid> put_at; // can't be //\attr (why ?)
 	\attr Numop *op;
 	int32 *wdex ; // temporary buffer, copy of put_at
 	int32 *fromb;
@@ -345,7 +345,7 @@
 	int d; // goes with wdex
 	long cs; // chunksize used in put_at
 	\constructor (Grid *r=0) {
-		put_at.constrain(expect_max_one_dim);
+		put_at.but(expect_max_one_dim);
 		this->r = r?r:new Grid(Dim(),int32_e,true);
 		op = op_put;
 		wdex  = NEWBUF(int32,Dim::MAX_DIM); // temporary buffer, copy of put_at
@@ -375,7 +375,7 @@
 // takes the backstore of a grid and puts it back into place. a backstore
 // is a grid that is filled while the grid it would replace has not
 // finished being used.
-static void snap_backstore (PtrGrid &r) {if (r && r->next) {P<Grid> tmp=r->next; r=tmp;}}
+static void snap_backstore (P<Grid> &r) {if (r && r->next) {P<Grid> tmp=r->next; r=tmp;}}
 
 template <class T> void GridStore::compute_indices(T *v, long nc, long nd) {
 	for (int i=0; i<nc; i++) {
@@ -523,7 +523,7 @@
 //{ Dim[*As]<T>,Dim[*Bs]<T> -> Dim[*As]<T> }
 \class GridOp : FObject {
 	\attr Numop *op;
-	PtrGrid r;
+	P<Grid> r;
 	\constructor (Numop *op, Grid *r=0) {this->op=op; this->r=r?r:new Grid(Dim(),int32_e,true);}
 	\grin 0
 	\grin 1
@@ -568,7 +568,7 @@
 //****************************************************************
 \class GridFold : FObject {
 	\attr Numop *op;
-	\attr PtrGrid seed;
+	\attr P<Grid> seed;
 	\constructor (Numop *op) {this->op=op;}
 	\grin 0
 };
@@ -613,7 +613,7 @@
 
 \class GridScan : FObject {
 	\attr Numop *op;
-	\attr PtrGrid seed;
+	\attr P<Grid> seed;
 	\constructor (Numop *op) {this->op = op;}
 	\grin 0
 };
@@ -657,9 +657,9 @@
 \class GridInner : FObject {
 	\attr Numop *op;
 	\attr Numop *fold;
-	\attr PtrGrid seed;
-	PtrGrid r;
-	PtrGrid r2; // temporary
+	\attr P<Grid> seed;
+	P<Grid> r;
+	P<Grid> r2; // temporary
 	bool use_dot;
 	\constructor (Grid *r=0) {
 		this->op = op_mul;
@@ -778,7 +778,7 @@
 /*{ Dim[*As]<T>,Dim[*Bs]<T> -> Dim[*As,*Bs]<T> }*/
 \class GridOuter : FObject {
 	\attr Numop *op;
-	PtrGrid r;
+	P<Grid> r;
 	\constructor (Numop *op, Grid *r=0) {
 		this->op = op;
 		this->r = r ? r : new Grid(Dim(),int32_e,true);
@@ -836,13 +836,13 @@
 //{ Dim[]<T>,Dim[]<T>,Dim[]<T> -> Dim[A]<T> } or
 //{ Dim[B]<T>,Dim[B]<T>,Dim[B]<T> -> Dim[*As,B]<T> }
 \class GridFor : FObject {
-	\attr PtrGrid from;
-	\attr PtrGrid to  ;
-	\attr PtrGrid step;
+	\attr P<Grid> from;
+	\attr P<Grid> to  ;
+	\attr P<Grid> step;
 	\constructor (Grid *from, Grid *to, Grid *step=0) {
-		this->from.constrain(expect_max_one_dim);
-		this->to  .constrain(expect_max_one_dim);
-		this->step.constrain(expect_max_one_dim);
+		this->from.but(expect_max_one_dim);
+		this->to  .but(expect_max_one_dim);
+		this->step.but(expect_max_one_dim);
 		this->from=from;
 		this->to  =to  ;
 		this->step=step;
@@ -933,11 +933,11 @@
 //{ Dim[*As]<T>,Dim[B] -> Dim[*Cs]<T> }
 \class GridRedim : FObject {
 	/*\attr*/ Dim dim;
-	PtrGrid dim_grid;
-	PtrGrid temp; // temp->dim is not of the same shape as dim
+	P<Grid> dim_grid;
+	P<Grid> temp; // temp->dim is not of the same shape as dim
 	~GridRedim() {}
 	\constructor (Grid *d) {
-		dim_grid.constrain(expect_dim_dim_list);
+		dim_grid.but(expect_dim_dim_list);
 		dim_grid=d;
 		dim = dim_grid->to_dim();
 	//	if (!dim.prod()) RAISE("target grid size must not be zero");

Modified: trunk/src/classes3.cxx
==============================================================================
--- trunk/src/classes3.cxx (original)
+++ trunk/src/classes3.cxx Tue Sep 21 20:49:05 2010
@@ -32,7 +32,7 @@
 
 //****************************************************************
 \class GridToTilde : FObject {
-	PtrGrid blah;
+	P<Grid> blah;
 	t_outlet **sigout;
 	int chans; /* number of channels */
 	int start;
@@ -87,7 +87,7 @@
 }
 //****************************************************************
 \class GridFromTilde : FObject {
-	PtrGrid blah;
+	P<Grid> blah;
 	t_inlet **sigin;
 	int chans; /* number of channels */
 	t_sample **sam;
@@ -129,7 +129,7 @@
 //****************************************************************
 \class GridJoin : FObject {
 	\attr int which_dim;
-	PtrGrid r;
+	P<Grid> r;
 	\grin 0
 	\grin 1
 	\constructor (int which_dim=-1, Grid *r=0) {
@@ -352,7 +352,7 @@
 
 \class GridMoment : FObject {
 	\constructor (int order=1) {
-		offset.constrain(expect_pair);
+		offset.but(expect_pair);
 		t_atom2 a[2] = {t_atom2(0),t_atom2(0)};
 		offset=new Grid(2,a,int32_e);
 		if (order!=1 && order!=2) RAISE("supports only orders 1 and 2 for now");
@@ -361,7 +361,7 @@
 	\grin 0 int
 	\grin 1 int
 	\attr int order; // order
-	\attr PtrGrid offset;
+	\attr P<Grid> offset;
 	int64 sumy,sumxy,sumx,sum,y; // temporaries
 };
 
@@ -534,8 +534,8 @@
 \class GridBorder : FObject {
 	/*\attr */ Dim diml;
 	/*\attr */ Dim dimr;
-	PtrGrid diml_grid;
-	PtrGrid dimr_grid;
+	P<Grid> diml_grid;
+	P<Grid> dimr_grid;
 	\grin 0
 	\grin 1 int
 	\grin 2 int
@@ -598,15 +598,15 @@
 	};
 	\attr Numop *op;
 	\attr Numop *fold;
-	\attr PtrGrid seed;
-	\attr PtrGrid b;
+	\attr P<Grid> seed;
+	\attr P<Grid> b;
 	\attr bool wrap;
 	\attr bool anti;
-	PtrGrid a;
+	P<Grid> a;
 	vector<PlanEntry> plan;
 	int margx,margy; // margins
 	\constructor (Grid *r=0) {
-		b.constrain(expect_convolution_matrix);
+		b.but(expect_convolution_matrix);
 		this->op = op_mul;
 		this->fold = op_add;
 		this->seed = new Grid(Dim(),int32_e,true);
@@ -718,11 +718,11 @@
 }
 
 \class GridScaleBy : FObject {
-	\attr PtrGrid scale; // integer scale factor
+	\attr P<Grid> scale; // integer scale factor
 	int scaley;
 	int scalex;
 	\constructor (Grid *factor=0) {
-		scale.constrain(expect_scale_factor);
+		scale.but(expect_scale_factor);
 		t_atom2 a[1] = {2};
 		scale = factor?factor:new Grid(1,a);
 		prepare_scale_factor();
@@ -768,13 +768,13 @@
 // ----------------------------------------------------------------
 //{ Dim[A,B,3]<T> -> Dim[C,D,3]<T> }
 \class GridDownscaleBy : FObject {
-	\attr PtrGrid scale;
+	\attr P<Grid> scale;
 	\attr bool smoothly;
 	int scaley;
 	int scalex;
-	PtrGrid temp;
+	P<Grid> temp;
 	\constructor (Grid *factor=0, t_symbol *option=0) {
-		scale.constrain(expect_scale_factor);
+		scale.but(expect_scale_factor);
 		t_atom2 a[1] = {2};
 		scale = factor?factor:new Grid(1,a,int32_e);
 		prepare_scale_factor();
@@ -854,8 +854,8 @@
 
 //****************************************************************
 \class GridLayer : FObject {
-	PtrGrid r;
-	\constructor () {r.constrain(expect_rgb_picture); r=new Grid(Dim(1,1,3),int32_e,true);}
+	P<Grid> r;
+	\constructor () {r.but(expect_rgb_picture); r=new Grid(Dim(1,1,3),int32_e,true);}
 	\grin 0 int
 	\grin 1 int
 };
@@ -910,19 +910,19 @@
 }
 \class DrawPolygon : FObject {
 	\attr Numop *op;
-	\attr PtrGrid color;
-	\attr PtrGrid polygon;
+	\attr P<Grid> color;
+	\attr P<Grid> polygon;
 	\attr DrawMode draw;
 	\attr OmitMode omit;
-	PtrGrid color2;
-	PtrGrid lines;
+	P<Grid> color2;
+	P<Grid> lines;
 	int lines_start;
 	int lines_stop;
 	\constructor (Numop *op=op_put, Grid *color=0, Grid *polygon=0) {
 		draw=DRAW_FILL;
 		omit=OMIT_NONE;
-		this->color.constrain(expect_one_dim);
-		this->polygon.constrain(expect_polygon);
+		this->color  .but(expect_one_dim);
+		this->polygon.but(expect_polygon);
 		this->op = op;
 		this->color   = color   ? color   : new Grid(Dim(1)  ,int32_e,true);
 		this->polygon = polygon ? polygon : new Grid(Dim(0,2),int32_e,true);
@@ -1051,15 +1051,15 @@
 
 \class DrawImage : FObject {
 	\attr Numop *op;
-	\attr PtrGrid image;
-	\attr PtrGrid position;
+	\attr P<Grid> image;
+	\attr P<Grid> position;
 	\attr bool alpha;
 	\attr bool tile;
 	\constructor (Numop *op=op_put, Grid *image=0, Grid *position=0) {
 		alpha=false; tile=false;
 		this->op = op;
-		this->position.constrain(expect_position);
-		this->image   .constrain(expect_picture);
+		this->position.but(expect_position);
+		this->image   .but(expect_picture);
 		this->image    = image    ? image    : new Grid(Dim(1,1,3),int32_e,true);
 		this->position = position ? position : new Grid(Dim(2),int32_e,true);
 	}
@@ -1156,8 +1156,8 @@
 /* NOT FINISHED */
 \class GridDrawPoints : FObject {
 	\attr Numop *op;
-	\attr PtrGrid color;
-	\attr PtrGrid points;
+	\attr P<Grid> color;
+	\attr P<Grid> points;
 	\grin 0
 	\grin 1 int32
 	\grin 2 int32
@@ -1223,7 +1223,7 @@
 
 \class GridPack : FObject {
 	int n;
-	PtrGrid a;
+	P<Grid> a;
 	\attr NumberTypeE cast;
 	\constructor (t_atom2 d=2, NumberTypeE nt=int32_e) {
 		Dim dim;
@@ -1302,12 +1302,12 @@
 #define OP(x) op_dict[string(#x)]
 \class GridClusterAvg : FObject {
 	\attr int numClusters;
-	\attr PtrGrid r;
-	\attr PtrGrid sums;
-	\attr PtrGrid counts;
+	\attr P<Grid> r;
+	\attr P<Grid> sums;
+	\attr P<Grid> counts;
 	\constructor (int v) {
 		_1_float(v);
-		r.constrain(expect_min_one_dim);
+		r.but(expect_min_one_dim);
 		r = new Grid(Dim(0));
 	}
 	\decl 1 float (int v) {numClusters = v;}
@@ -1354,10 +1354,10 @@
 \class GridLopSpace : FObject {
 	\attr bool reverse;
 	\attr int which_dim;
-	\attr PtrGrid r;
+	\attr P<Grid> r;
 	\grin 0
 	\grin 1
-	PtrGrid r2;
+	P<Grid> r2;
 	\constructor (int which_dim=0, Grid *r=0) {this->which_dim = which_dim; this->r=r; reverse=false;}
 };
 template <class T> inline T       shr8r (T       a) {return (a+128)>>8;}
@@ -1435,7 +1435,7 @@
 
 \class GridTabwrite : FObject {
 	t_symbol *t;
-	PtrGrid r;
+	P<Grid> r;
 	\constructor (t_symbol *table=&s_) {
 		t = table;
 	}
@@ -1471,9 +1471,9 @@
 //****************************************************************
 \class GridCompress : FObject {
 	void *tmp;
-	PtrGrid r;
+	P<Grid> r;
 	\constructor (Grid *r=0) {
-		this->r.constrain(expect_one_dim);
+		this->r.but(expect_one_dim);
 		this->r=r?r:new Grid(Dim(0),int32_e,true);
 	}
 	\grin 0

Modified: trunk/src/grid.cxx
==============================================================================
--- trunk/src/grid.cxx (original)
+++ trunk/src/grid.cxx Tue Sep 21 20:49:05 2010
@@ -38,6 +38,8 @@
 #define CHECK_BUSY(s)  if (!this->sender) RAISE(#s " not busy (wanting to write %ld values)",(long)n);
 #define CHECK_ALIGN(d,nt) {int bytes = number_type_table[nt].size/8; int align = ((uintptr_t)(void*)d)%bytes; \
 	if (align) {post("(%s): Alignment Warning: %p is not %d-aligned: %d", __PRETTY_FUNCTION__, (void*)d,bytes,align);}}
+
+CONSTRAINT(expect_any) {}
 
 // **************** Grid ******************************************
 

Modified: trunk/src/gridflow.hxx
==============================================================================
--- trunk/src/gridflow.hxx (original)
+++ trunk/src/gridflow.hxx Tue Sep 21 20:49:05 2010
@@ -211,6 +211,8 @@
 #define sigjmp_buf jmp_buf
 #define siglongjmp longjmp
 #endif
+
+extern char *short_backtrace (int start=3, int end=4);
 
 //#define _L_ post("%s:%d in %s",__FILE__,__LINE__,__PRETTY_FUNCTION__);
 #define _L_ fprintf(stderr,"%s:%d in %s\n",__FILE__,__LINE__,__PRETTY_FUNCTION__);
@@ -295,37 +297,6 @@
 	return ((t_symbol *)*xx)->s_name;
 }
 
-//****************************************************************
-
-//template <class T> class P : T * {};
-//a reference counting pointer class
-//note: T <= CObject
-//used mostly as P<Grid>, P<BitPacking>
-extern char *short_backtrace (int start=3, int end=4);
-template <class T> struct P {
-public:
-	//#define INCR if (p) {p->refcount++; if (p->refcount>1) post("refcount++ to %d at %s",p->refcount,short_backtrace());}
-	//#define DECR if (p) {if (p->refcount>1) post("refcount-- from %d at %s",p->refcount,short_backtrace()); p->refcount--; if (!p->refcount) delete p;}
-	#define INCR if (p) {p->refcount++;}
-	#define DECR if (p) {p->refcount--; if (!p->refcount) delete p;}
-	T *p;
-	P()               {p=0;}
-	P(T *_p)          {p=_p  ; INCR;}
-	P(const P<T> &_p) {p=_p.p; INCR;}
-	P<T> &operator = (T *  _p) {DECR; p=_p;   INCR; return *this;}
-	P<T> &operator = (P<T> _p) {DECR; p=_p.p; INCR; return *this;}
-	bool operator == (P<T> _p) {return p==_p.p;}
-	bool operator != (P<T> _p) {return p!=_p.p;}
-	~P() {DECR;}
-	bool operator !() {return  !p;}
-	operator bool()   {return !!p;}
-	T &operator *()   {return  *p;}
-	T *operator ->()  {return   p;}
-	operator T *() {return p;}
-	//#undef INCR
-	//#undef DECR
-};
-
 void gfmemcopy(uint8 *out, const uint8 *in, long n);
 template <class T> inline void COPY  (T *dest, const T *src, long n=1) {gfmemcopy((uint8*)dest,(const uint8*)src,n*sizeof(T));}
 template <class T> inline void CLEAR (T *dest,               long n=1) {memset(dest,0,n*sizeof(T));}
@@ -340,6 +311,74 @@
 };
 
 #undef check
+
+//****************************************************************
+//NumberTypeE is a very small int identifying the type of the (smallest) elements of a grid
+
+#define NT_UNSIGNED (1<<0)
+#define NT_FLOAT    (1<<1)
+#define NT_UNIMPL   (1<<2)
+#define NUMBER_TYPE_LIMITS(T,a,b,c) \
+	inline T nt_smallest(T *bogus) {return a;} \
+	inline T nt_greatest(T *bogus) {return b;} \
+	inline T nt_all_ones(T *bogus) {return c;}
+
+NUMBER_TYPE_LIMITS(  uint8,0,255,255)
+NUMBER_TYPE_LIMITS(  int16,-0x8000,0x7fff,-1)
+NUMBER_TYPE_LIMITS(  int32,-0x80000000,0x7fffffff,-1)
+NUMBER_TYPE_LIMITS(  int64,-0x8000000000000000LL,0x7fffffffffffffffLL,-1)
+NUMBER_TYPE_LIMITS(float32,-HUGE_VAL,+HUGE_VAL,(RAISE("all_ones"),0))
+NUMBER_TYPE_LIMITS(float64,-HUGE_VAL,+HUGE_VAL,(RAISE("all_ones"),0))
+
+#ifdef HAVE_LITE
+#define NT_NOTLITE NT_UNIMPL
+#define NONLITE(x...)
+#else
+#define NT_NOTLITE 0
+#define NONLITE(x...) x
+#endif
+#define NUMBER_TYPES(MACRO) \
+	MACRO(b,uint8, 8,NT_UNSIGNED) \
+	MACRO(s,int16,16,0          ) \
+	MACRO(i,int32,32,0          ) \
+	MACRO(l,int64,64,NT_NOTLITE ) \
+	MACRO(f,float32,32,NT_FLOAT ) \
+	MACRO(d,float64,64,NT_NOTLITE|NT_FLOAT)
+
+enum NumberTypeE {
+#define FOO(ABBR,SYM,SIZE,FLAGS) SYM##_e,
+NUMBER_TYPES(FOO)
+#undef FOO
+number_type_table_end
+};
+
+#define FOO(_type_) \
+inline NumberTypeE NumberTypeE_type_of(_type_ *x) {return _type_##_e;}
+EACH_NUMBER_TYPE(FOO)
+#undef FOO
+
+struct NumberType {
+	const char *alias;
+	const char *name;
+	int size;
+	int flags;
+	NumberTypeE index;
+	NumberType(const char *alias_, const char *name_, int size_, int flags_) :
+		alias(alias_), name(name_), size(size_), flags(flags_) {}
+};
+
+NumberTypeE NumberTypeE_find (string sym);
+NumberTypeE NumberTypeE_find (const t_atom &sym);
+
+#define TYPESWITCH(T,C,E) switch (T) { \
+  case uint8_e:   C(uint8)   break;         case   int16_e: C(int16)   break; \
+  case int32_e:   C(int32)   break; NONLITE(case   int64_e: C(int64)   break;) \
+  case float32_e: C(float32) break; NONLITE(case float64_e: C(float64) break;) \
+  default: E; RAISE("type '%s' not available here",number_type_table[T].name);}
+#define TYPESWITCH_JUSTINT(T,C,E) switch (T) { \
+  case uint8_e: C(uint8) break;           case int16_e: C(int16) break; \
+  case int32_e: C(int32) break;   NONLITE(case int64_e: C(int64) break;) \
+  default: E; RAISE("type '%s' not available here",number_type_table[T].name);}
 
 //****************************************************************
 // a Dim is a list of dimensions that describe the shape of a grid
@@ -383,72 +422,41 @@
 };
 
 //****************************************************************
-//NumberTypeE is a very small int identifying the type of the (smallest) elements of a grid
-
-#define NT_UNSIGNED (1<<0)
-#define NT_FLOAT    (1<<1)
-#define NT_UNIMPL   (1<<2)
-#define NUMBER_TYPE_LIMITS(T,a,b,c) \
-	inline T nt_smallest(T *bogus) {return a;} \
-	inline T nt_greatest(T *bogus) {return b;} \
-	inline T nt_all_ones(T *bogus) {return c;}
-
-NUMBER_TYPE_LIMITS(  uint8,0,255,255)
-NUMBER_TYPE_LIMITS(  int16,-0x8000,0x7fff,-1)
-NUMBER_TYPE_LIMITS(  int32,-0x80000000,0x7fffffff,-1)
-NUMBER_TYPE_LIMITS(  int64,-0x8000000000000000LL,0x7fffffffffffffffLL,-1)
-NUMBER_TYPE_LIMITS(float32,-HUGE_VAL,+HUGE_VAL,(RAISE("all_ones"),0))
-NUMBER_TYPE_LIMITS(float64,-HUGE_VAL,+HUGE_VAL,(RAISE("all_ones"),0))
-
-#ifdef HAVE_LITE
-#define NT_NOTLITE NT_UNIMPL
-#define NONLITE(x...)
-#else
-#define NT_NOTLITE 0
-#define NONLITE(x...) x
-#endif
-#define NUMBER_TYPES(MACRO) \
-	MACRO(b,uint8, 8,NT_UNSIGNED) \
-	MACRO(s,int16,16,0          ) \
-	MACRO(i,int32,32,0          ) \
-	MACRO(l,int64,64,NT_NOTLITE ) \
-	MACRO(f,float32,32,NT_FLOAT ) \
-	MACRO(d,float64,64,NT_NOTLITE|NT_FLOAT)
-
-enum NumberTypeE {
-#define FOO(ABBR,SYM,SIZE,FLAGS) SYM##_e,
-NUMBER_TYPES(FOO)
-#undef FOO
-number_type_table_end
-};
-
-#define FOO(_type_) \
-inline NumberTypeE NumberTypeE_type_of(_type_ *x) {return _type_##_e;}
-EACH_NUMBER_TYPE(FOO)
-#undef FOO
-
-struct NumberType {
-	const char *alias;
-	const char *name;
-	int size;
-	int flags;
-	NumberTypeE index;
-	NumberType(const char *alias_, const char *name_, int size_, int flags_) :
-		alias(alias_), name(name_), size(size_), flags(flags_) {}
-};
-
-NumberTypeE NumberTypeE_find (string sym);
-NumberTypeE NumberTypeE_find (const t_atom &sym);
-
-#define TYPESWITCH(T,C,E) switch (T) { \
-  case uint8_e:   C(uint8)   break;         case   int16_e: C(int16)   break; \
-  case int32_e:   C(int32)   break; NONLITE(case   int64_e: C(int64)   break;) \
-  case float32_e: C(float32) break; NONLITE(case float64_e: C(float64) break;) \
-  default: E; RAISE("type '%s' not available here",number_type_table[T].name);}
-#define TYPESWITCH_JUSTINT(T,C,E) switch (T) { \
-  case uint8_e: C(uint8) break;           case int16_e: C(int16) break; \
-  case int32_e: C(int32) break;   NONLITE(case int64_e: C(int64) break;) \
-  default: E; RAISE("type '%s' not available here",number_type_table[T].name);}
+// GridConstraint interface: (only a GRID_BEGIN time constraint)
+// return if d and nt are acceptable, else RAISE with proper descriptive message
+#define CONSTRAINT(NAME) void NAME (const Dim &d, NumberTypeE nt)
+typedef CONSTRAINT((*GridConstraint));
+CONSTRAINT(expect_any);
+
+//template <class T> class P : T * {};
+//a reference counting pointer class
+//note: T <= CObject
+//used mostly as P<Grid>, P<BitPacking>
+template <class T> struct P {
+public:
+	//#define INCR if (p) {p->refcount++; if (p->refcount>1) post("refcount++ to %d at %s",p->refcount,short_backtrace());}
+	//#define DECR if (p) {if (p->refcount>1) post("refcount-- from %d at %s",p->refcount,short_backtrace()); p->refcount--; if (!p->refcount) delete p;}
+	#define INCR if (p) {p->refcount++;}
+	#define DECR if (p) {p->refcount--; if (!p->refcount) delete p;}
+	T *p;
+	GridConstraint dc; // is not really related to the general P template, but it saves some trouble later
+	void but (GridConstraint dc) {this->dc=dc;}
+	P()               {dc=0; p=0;}
+	P(T *_p)          {dc=0; p=_p  ; INCR;}
+	P(const P<T> &_p) {dc=0; p=_p.p; INCR;}
+	P<T> &operator = (T *  _p) {DECR; p=_p;   INCR; return *this;}
+	P<T> &operator = (P<T> _p) {DECR; p=_p.p; INCR; return *this;}
+	bool operator == (P<T> _p) {return p==_p.p;}
+	bool operator != (P<T> _p) {return p!=_p.p;}
+	~P() {DECR;}
+	bool operator !() {return  !p;}
+	operator bool()   {return !!p;}
+	T &operator *()   {return  *p;}
+	T *operator ->()  {return   p;}
+	operator T *() {return p;}
+	#undef INCR
+	#undef DECR
+};
 
 //****************************************************************
 //BitPacking objects encapsulate optimised loops of bitfield conversion (mostly for I/O)
@@ -627,31 +635,13 @@
 };
 static inline Grid *convert (const t_atom &r, Grid **bogus) {return new Grid(r);}
 
-// GridConstraint interface: (only a GRID_BEGIN time constraint)
-// return if d and nt are acceptable, else RAISE with proper descriptive message
-#define CONSTRAINT(NAME) void NAME (const Dim &d, NumberTypeE nt)
-typedef CONSTRAINT((*GridConstraint));
-
-struct PtrGrid : public P<Grid> {
-	GridConstraint dc;
-	void constrain(GridConstraint dc_) {dc=dc_;}
-	PtrGrid()                  : P<Grid>(), dc(0) {}
-	PtrGrid(const PtrGrid &_p) : P<Grid>(), dc(0) {dc=_p.dc; p=_p.p; INCR;}
-	PtrGrid(         Grid *_p) : P<Grid>(), dc(0) {            p=_p; INCR;}
-	PtrGrid &operator =(  Grid *_p) {if(dc&&_p)dc(_p->dim,_p->nt); DECR; p=_p;   INCR; return *this;}
-	PtrGrid &operator =(P<Grid> _p) {if(dc&&_p)dc(_p->dim,_p->nt); DECR; p=_p.p; INCR; return *this;}
-	PtrGrid &operator =(PtrGrid _p) {if(dc&&_p)dc(_p->dim,_p->nt); DECR; p=_p.p; INCR; return *this;}
-};
-#undef INCR
-#undef DECR
-
 static inline Dim convert(const t_atom &x, Dim *foo) {
 	Grid *d = convert(x,(Grid **)0);
 	if (!d) RAISE("urgh");
 	if (d->dim.n!=1) RAISE("dimension list must have only one dimension itself");
 	return Dim(d->dim[0],(int32 *)d->data);
 }
-static inline PtrGrid convert(const t_atom &x, PtrGrid *foo) {return PtrGrid(convert(x,(Grid **)0));}
+static inline P<Grid> convert(const t_atom &x, P<Grid> *foo) {return P<Grid>(convert(x,(Grid **)0));}
 
 //****************************************************************
 // GridInlet represents a grid-aware inlet
@@ -668,7 +658,7 @@
 #define GRID_END }
 
 /* macros for defining a gridinlet's behaviour as just storage (without and with backstore, respectively) */
-// V is a PtrGrid instance-var
+// V is a P<Grid> instance-var
 #define GRID_INPUT(I,V) \
 	GRID_INLET(I) {V=new Grid(in.dim,NumberTypeE_type_of(data));} GRID_FLOW {COPY((T *)*(V)+in.dex,data,n);} GRID_FINISH
 #define GRID_INPUT2(I,V) GRID_INLET(I) {V->next = new Grid(in.dim,NumberTypeE_type_of(data));} \
@@ -689,7 +679,7 @@
 	// set once per transmission
 	GridOut *sender; Dim dim; NumberTypeE nt; /* nt shouldn't need to exist */
 	// modified continually
-	long dex; PtrGrid buf; /* factor-chunk buffer */ long bufi; /* buffer index: how much of buf is filled */
+	long dex; P<Grid> buf; /* factor-chunk buffer */ long bufi; /* buffer index: how much of buf is filled */
 
 	GridInlet(FObject *parent_, const GridHandler *gh_) :
 		parent(parent_), gh(gh_), sender(0), dim(0), nt(int32_e), dex(0), bufi(0) {}

Modified: trunk/src/opencv.cxx
==============================================================================
--- trunk/src/opencv.cxx (original)
+++ trunk/src/opencv.cxx Tue Sep 21 20:49:05 2010
@@ -120,7 +120,7 @@
 	set_atom(a,b);
 }
 
-CvArr *cvGrid(PtrGrid g, CvMode mode, int reqdims=-1) {
+CvArr *cvGrid(P<Grid> g, CvMode mode, int reqdims=-1) {
 	int channels=1;
 	int dims=g->dim.n;
 	//post("mode=%d",(int)mode);
@@ -145,7 +145,7 @@
 	//return 0;
 }
 
-IplImage *cvImageGrid(PtrGrid g /*, CvMode mode */) {
+IplImage *cvImageGrid(P<Grid> g /*, CvMode mode */) {
 	Dim &d = g->dim;
 	if (d.n!=3) RAISE("expected 3 dimensions, got %s",d.to_s());
 	int channels=g->dim[2];
@@ -201,11 +201,11 @@
 };
 \end class {}
 
-// from flow_objects.c
-static void snap_backstore (PtrGrid &r) {if (r && r->next) {r=r->next.p; r->next=0;}}
+// from classes1.cxx
+static void snap_backstore (P<Grid> &r) {if (r && r->next) {P<Grid> tmp=r->next.p; r=tmp;}}
 
 \class CvOp2 : CvOp1 {
-	PtrGrid r;
+	P<Grid> r;
 	\constructor (Grid *r=0) {this->r = r?r:new Grid(Dim(),int32_e,true);}
 	virtual void func(CvArr *l, CvArr *r, CvArr *o) {/* rien */}
 	\grin 0
@@ -217,8 +217,8 @@
 	if (in.dim != r->dim) RAISE("dimension mismatch: left:%s right:%s",in.dim.to_s(),r->dim.to_s());
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,(T *)data);
-	PtrGrid o = new Grid(in.dim,in.nt);
+	P<Grid> l = new Grid(in.dim,(T *)data);
+	P<Grid> o = new Grid(in.dim,in.nt);
 	CvArr *a = cvGrid(l,mode);
 	CvArr *b = cvGrid(r,mode);
 	CvArr *c = cvGrid(o,mode);
@@ -260,8 +260,8 @@
 	in.set_chunk(0);
 } GRID_FLOW {
 	//post("l=%p, r=%p", &*l, &*r);
-	PtrGrid l = new Grid(in.dim,(T *)data);
-	PtrGrid o = new Grid(in.dim,in.nt);
+	P<Grid> l = new Grid(in.dim,(T *)data);
+	P<Grid> o = new Grid(in.dim,in.nt);
 	CvArr *a = cvGrid(l,mode);
 	CvArr *c = cvGrid(o,mode);
 	//post("a=%p, b=%p", a, b);
@@ -282,10 +282,10 @@
 	if (in.dim[0] != in.dim[1]) RAISE("matrix should be square");
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,(T *)data);
-	PtrGrid o0 = new Grid(in.dim,in.nt);
-	PtrGrid o1 = new Grid(in.dim,in.nt);
-	PtrGrid o2 = new Grid(in.dim,in.nt);
+	P<Grid> l  = new Grid(in.dim,(T *)data);
+	P<Grid> o0 = new Grid(in.dim,in.nt);
+	P<Grid> o1 = new Grid(in.dim,in.nt);
+	P<Grid> o2 = new Grid(in.dim,in.nt);
 	CvArr *a = cvGrid(l,mode);
 	CvArr *c0 = cvGrid(o0,mode);
 	CvArr *c1 = cvGrid(o1,mode);
@@ -320,7 +320,7 @@
 GRID_INLET(0) {
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,in.nt); COPY((T *)*l,data,in.dim.prod());
+	P<Grid> l = new Grid(in.dim,in.nt); COPY((T *)*l,data,in.dim.prod());
 	IplImage *img = cvImageGrid(l);
 	cvEllipse(img,center,axes,angle,start_angle,end_angle,color,thickness,line_type,shift);
 	cvReleaseImageHeader(&img);
@@ -339,7 +339,7 @@
 GRID_INLET(0) {
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,(T *)data); CvArr *a = cvGrid(l,mode);
+	P<Grid> l = new Grid(in.dim,(T *)data); CvArr *a = cvGrid(l,mode);
 	CvSeq *seq = cvApproxPoly(a,sizeof(CvMat),storage,CV_POLY_APPROX_DP,accuracy,closed);
 	seq=seq; //blah
 } GRID_END
@@ -440,7 +440,7 @@
 GRID_INLET(0) {
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,(T *)data);
+	P<Grid> l = new Grid(in.dim,(T *)data);
 	IplImage *img = cvImageGrid(l);
 	CvSeq *ret = cvHaarDetectObjects(img,cascade,storage,scale_factor,min_neighbors,flags);
 	int n = ret ? ret->total : 0;
@@ -471,10 +471,8 @@
 	in.set_chunk(0);
 } GRID_FLOW {
 	int32 v[] = {in.dim.prod(0)/in.dim.prod(-1),in.dim.prod(-1)};
-	PtrGrid l = new Grid(Dim(2,v),(T *)data);
-	CvArr *a = (CvMat *)cvGrid(l,mode,2);
-	PtrGrid o = new Grid(Dim(1,v),int32_e);
-	CvArr *c = (CvMat *)cvGrid(o,mode);
+	P<Grid> l = new Grid(Dim(2,v),(T *)data); CvArr *a = (CvMat *)cvGrid(l,mode,2);
+	P<Grid> o = new Grid(Dim(1,v),int32_e);   CvArr *c = (CvMat *)cvGrid(o,mode);
 	cvKMeans2(a,numClusters,c,termcrit);
 	int w[in.dim.n];
 	COPY(w,in.dim.v,in.dim.n);
@@ -502,10 +500,8 @@
 GRID_INLET(0) {
 	in.set_chunk(0);
 } GRID_FLOW {
-	PtrGrid l = new Grid(in.dim,(T *)data);
-	CvArr *a = (CvMat *)cvGrid(l,mode,2);
-	PtrGrid o = new Grid(in.dim,float32_e);
-	CvArr *c = (CvMat *)cvGrid(o,mode);
+	P<Grid> l = new Grid(in.dim,(T *)data);	CvArr *a = (CvMat *)cvGrid(l,mode,2);
+	P<Grid> o = new Grid(in.dim,float32_e);	CvArr *c = (CvMat *)cvGrid(o,mode);
 	cvCornerHarris(a,c,block_size,aperture_size,k);
 	cvRelease(&a);
 	cvRelease(&c);



More information about the Gridflow-cvs mailing list