Is there template in stl,boost or other LGPL open-source toolkit which behaves exactly like this:-
- a relative pointer with custom alignment,option to store fewer bits to reduce range.
a possible implementation to illustrate:-
template<typename T, typename OFFSET=int,
int ALIGN_SHIFT=2>
class OffsetPtr
{
OFFSET ofs;
public:
T* operator->() {
return (T*) (((((size_t)this)>>ALIGN_SHIFT)+ofs)<<ALIGN_SHIFT);
};
void operator=(T* src) {
size_t ofs_shifted = (((size_t) src)>>ALIGN_SHIFT) - (((size_t) this)>>ALIGN_SHIFT); //asserts..
ofs = (OFFSET) (ofs_shifted);
}
//...
};
Its something I would routinely create in the past (compact cache-friendly precompiled data-structures), e.g. for data broken into sub 128k chunks OFFSET=short
Another variation I'd use in ancient C #defines would use offsets from a header, where the alignments would be more useful.
I've seen something about an 'interprocess library' in boost having an 'offset_ptr', that looks very similar, so it seems likely there's an existing implementation including this exact pattern somewhere. It's quick to write but there might be benefits to an existing implementation like a suite of associated stl compliant data structures built around the same concept - a 'near vector' with 16bit offset pointer & 16bit count for example
If you're using Visual C++, you might like to use __based
pointers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With