discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: data type polymorphism


From: Maxthon Chan
Subject: Re: data type polymorphism
Date: Tue, 24 Mar 2015 12:39:43 +0800

I think the thing you are looking for here is C++ templates.

If you need to keep your code strictly C you can emulate this by (ab)using 
#include and #define and #ifdef like this:

// foo.c

#define DATA_TYPE float

#include “foo.i”

#undef DATA_TYPE
#define DATA_TYPE double

#include “foo.i”

#undef DATA_TYPE

// foo.i

#define CONCAT(x, y) x ## y

DATA_TYPE *CONCAT(process_,DATA_TYPE)(DATA_TYPE **data)
{
// …
}

> On Mar 21, 2015, at 17:19, Riccardo Mottola <address@hidden> wrote:
> 
> 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 don稚 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 zero段ng 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=markup
> 
> Here 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
> 
> 
> 
> _______________________________________________
> Discuss-gnustep mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/discuss-gnustep




reply via email to

[Prev in Thread] Current Thread [Next in Thread]