|
From: | Riccardo Mottola |
Subject: | Re: data type polymorphism |
Date: | Sat, 21 Mar 2015 10:19:21 +0100 |
User-agent: | Mozilla/5.0 (X11; FreeBSD i386; rv:35.0) Gecko/20100101 Firefox/35.0 SeaMonkey/2.32 |
Hi Scott, Scott Christley wrote:
Hello,This is more a generic Objective-C question versus GNUstep but maybe some experts here have a suggestion.
I think this is a more a "C" question since it involves pointer sizes and such.
I have a bunch of code that looks l like this: if([encodeisEqual: [BioSwarmModelfloatEncode]]) { // interpret as float matrix float (*grid)[height][width] = matrix; for (i = 0;i < height; ++i) for (j = 0;j < width; ++j) (*grid)[i][j] = 0.0; } elseif([encodeisEqual: [BioSwarmModeldoubleEncode]]) { // interpret as double matrix double (*grid)[height][width] = matrix; for (i = 0;i < height; ++i) for (j = 0;j < width; ++j) (*grid)[i][j] = 0.0; }where I have a generic pointer void *matrix to some data, that I need to interpret as a specific data type, generally either int, float or double. The part I dont like is that the operation is essentially identical regardless of the data type, but I have to duplicate code in order to handle it. In this example, the code is just zeroing out the data. This can be a pain for more complicated operations as I have to make sure I do the correct changes to each code piece. What I would like is just to write the code once and have the compiler or whatever handle the data type for me:
I have a very similar problem I suppose. To reply to max: no, I can't just use double, the pointer size changes. I want to read out both 8 and 16 bit images.
http://price.cvs.sourceforge.net/viewvc/price/PRICE-osx/PRInvert.m?revision=1.13&view=markupHere I have the same code twice, actually even the same loop, what changes is that I declared the image data pointers once as unsigned char, once as unsigned short. I want to support this at runtime, so no #ifdef compile-time trickery.
This stopped me from making all filters support 16 bit images because it would mean rewriting half of the application this way.
Riccardo
[Prev in Thread] | Current Thread | [Next in Thread] |