discuss-gnustep
[Top][All Lists]
Advanced

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

Re: data type polymorphism


From: Ivan Vučica
Subject: Re: data type polymorphism
Date: Tue, 24 Mar 2015 22:32:26 +0000

If you have a couple hundred lines of code and want to stay in the world of C, you could consider splitting these lines into a .h file, and then #define MYTYPE float, include header, #undef MYTYPE, #define MYTYPE char, include header, etc.

If you are not doing something too computationally intensive, you would use Objective-C classes to wrap your data types. But you are, and so you're dropping to C level to do computation, hence nicest thing to do is to do relatively clean preprocessor tricks.

If you are willing to dilute your code with C++, I like David's approach. :-)


On Tue, Mar 24, 2015, 21:49 Riccardo Mottola <riccardo.mottola@libero.it> wrote:
Hi Scott,

Scott Christley wrote:
> Hi Riccardo,
>
> Maxthon is not suggesting to pick a type during compile time, but instead use a macro to generate both cases in your code.  For your app it would be something like this where the macro has the code, and the C preprocessor is used to insert the code.  In reality you still have duplicate code.  I was aware of this solution but was interested to see if there was any alternatives.
>
>
> #define PROCESS_IMAGE(DATATYPE) \
>             DATATYPE    *srcData; \
>             DATATYPE    *destData; \
>             DATATYPE    *p1; \
>             DATATYPE    *p2; \
>             // etc.
>
>
>    if ([srcImageRep bitsPerSample] == 8)
>      {
>       PROCESS_IMAGE(unsigned char)
>      }
>    else if ([srcImageRep bitsPerSample] == 16)
>      {
>       PROCESS_IMAGE(unsigned short)
>      }
>
>
Oh thank you for explaining it.  This is feasible for short pieces of
code, but if you have a hundred of lines, either you split it up in
smaller makro-pieces or it is a mess.

Thanks anyway.

Riccardo

_______________________________________________
Discuss-gnustep mailing list
Discuss-gnustep@gnu.org
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

reply via email to

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