[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Portable Code (am I dreaming? :))
From:       Nicolas Goutte <nicog () snafu ! de>
Date:       2002-01-23 12:52:03
[Download RAW message or body]

On Wednesday 23 January 2002 03:39, shaheed wrote:
> Clarence,
>
> > The problem is this: the mswrite import filter is sort of separate from
> > the MSWriteLib library and this modularity enables me to write other
> > filters for mswrite, instead of just KWord (e.g. mswrite->HTML). 
> > MSWriteLib is written purely in standard C++ and I don't want to
> > "contaminate" it with QT.  Does anyone have a way around this?
>
> Apart from my ignorance of QDatastream, this was also the original design
> goal of the word stuff. The way i would do it now is as follows:
>
> 1. Write all your code in terms of Q_{U,}INT{8,16,32} to maximise
> portability on all Qt supported platforms. In your header file, do
> something like this:
>
> #ifndef Q_UINT8
>
> ...as many platform-specific defs enclose in platform #ifdef guards
>
> #endif
>
> to maximise portability on non Qt supported platforms. Or vice versa.

I do not think that you need it.

The MS Write import filter does define its base types (BYTE, WORD, DWORD...) 
correctly (apart perhaps from the comment I made about signess).

QT defines its base types quite the same way. So you do not need to depend on 
QT, because it does not bring you something new or better (not even 
additional portability.)

And if you read the values byte by byte, you do not really care about the 
real size (as long as it is not smaller).

>
> 2. Use functions (and/or macros) as Nicolas suggested for reading 8, 16, 32
> bit signed/unsigneds.
>
> 3. Don't worry about the direction of bit shifts for the reason Nicolas
> gave, and don't worry about the performance of bit shifts since the
> compiler will almost certainly put the original value in a register. The
> current msword code does some of this, and a typical routine looks like
> this:
>
> unsigned MsWordGenerated::read(const U8 *in, __UNAL SHD *out, unsigned
> count) {
>     U32 shifterU32;
>     U16 shifterU16;
>     U8 shifterU8;
>     U8 *ptr;
>     unsigned bytes = 0;
>
>     ptr = (U8 *)out;
>     shifterU32 = shifterU16 = shifterU8 = 0;
>     for (unsigned i = 0; i < count; i++)
>     {
>         bytes += read(in + bytes, &shifterU16);
>         out->icoFore = shifterU16;
>         shifterU16 >>= 5;
>         out->icoBack = shifterU16;
>         shifterU16 >>= 5;
>         out->ipat = shifterU16;
>         shifterU16 >>= 6;
>         out++;
>     }
>     return bytes;
> } // SHD

I must admit that I have some problems to understand this function. I 
especially do not understand why you use shifts not divisible by 4 and why 
you do not use AND ( & ).

>
> Note that the _UNAL will disappear as soon as I get round to eliminating a

I suppose that _UNAL is something like __unaligned, isn't it?

> few sizeof()s used by the current code. In the example, I have an
> overloaded read() function for the different primitive types.

Just a comment on what I told previously: instead of struct you can also use 
class and have a method (named for example readFromFile) for each structure 
instead of an independant function for each structure.

You could even extend the idea to have BYTE, WORD, DWORD... as classes.

>
> Finally, if you can, I heartily recommend a code generator...that way, if
> you get the implementation wrong, you can at least change it.
>
> Thanks, Shaheed

Have a nice day/evening/night!
_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic