Boost logo

Boost :

From: Marco Manfredini (marco_at_[hidden])
Date: 2000-03-10 18:19:23


> -----Original Message-----
> From: Borgerding, Mark A. [mailto:MarkAB_at_[hidden]]
> Sent: Friday, March 10, 2000 11:28 PM
> To: boost_at_[hidden]
> Subject: [boost] Re: endian handling classes
>
>
> > -----Original Message-----
> > From: Miki Jovanovic [mailto:miki_at_[hidden]]
> > Sent: Friday, March 10, 2000 5:04 PM
> > To: boost_at_[hidden]
> > Subject: [boost] Re: endian handling classes
> >
> >
> > "borgerding, mark a." <marka-_at_[hidden]> wrote:
> > > I had an idea today for a way to more easily write portable
> > networking code.
> >
> > Hi Mark,
> >
> > Good idea all around. If I remember correctly, someone else
> > suggested a
> > very similar approach some time in the past. I can't seem
> to find that
> > post now. It would be nice to find it to compare notes and to
> > recognize
> > effort.
>
> Marco, is this what you referred to?
> I will look through the archives and see what I can find.

I found a post from me in deja from'99:
http://x24.deja.com/getdoc.xp?AN=502703845.2&CONTEXT=952727818.2053177344&hi
tnum=0

But I'm not so vain to claim that I have code in Visual Source Safe that was
checked in November 1996 with this trick :-) But perhaps I should claim a
patent and get enormously rich. (door slam, footsteps, engines start,
silence...)
Btw: I there anybody around that did *not* invent the method to do and undo
screen-updates (I mean move spaceships around..) with xor as a spotty
teenager who hacks machine code into a vic-20?

I've included what wrote way back then, but it's essentially what you
wrote - less portable however.

While we are at sharing delightful ideas. Here are two "smart" pointer
classes I occasionaly make up to access data structures in memory mapped
files (I retype them from memory, because I never made them really useful,
i.e. with good conversions&portability. They are ad-hoc thingies)

template<class T, long c>
struct long_offset_ptr
{
        unsigned long offset;
        T *operator *() { return T* ((char*)this)+(offset+c); }
        T *operator->() { return T* ((char*)this)+(offset+c); }
};

This is useful, if you have, say, a file header which has relative offsets
to data:

long tag;
long copyright; // offset into copyright string (from here), zero terminated
long bitmap; // offset to a bitmap info (from here)

so you can declare the header:

struct file_header
{
        long tag;
        long_offset_ptr<char> copyright;
        long_offset_ptr<bitmap_header> bitmap;
};

then you cast the start of your memory mapped file to a "header" and access
like frenzy:

file_header *fh=(file_header*)map_file_ptr;
cout << fh->copyright;

The second is similar and deals with absolute offsets from the start of the
file:

template<class T, void *base>
struct based_ptr
{
        unsigned long offset;
        T *operator *() { return T* ((char*)base)+offset; }
        T *operator->() { return T* ((char*)base)+offset; }
};

You instantiate the template with a global void * that points to the start
of the memory mapped file. That's also its weakness - you need one global
for each file, not very flexible, but simplifies things alot sometimes.

-------

Is that of interest? There are some problems attached to these ideas, mainly
converting them to and from normal pointers had certain safety commisions
which I'm not aware anymore.

Cheers

Marco




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk