Using the Variant (Setting it's memory location)
Pedro Lacerda
pslacerda at gmail.com
Wed Feb 8 03:57:23 PST 2012
Umm, sounds nice. If you want to store a long into buffer, you can cast the
desired position as long* and assign the value. The following is adapted
from std.outbuffer, that is another option.
ubyte buffer[];
size_t offset;
ulong a = 1;
byte b = 2;
// allocate space for ulong e byte
buffer.length = a.sizeof + b.sizeof;
*cast(ulong *)&buffer[offset] = a; // store value
offset += a.sizeof; // increment offset
*cast(byte *)&buffer[offset] = b;
offset += b.sizeof;
Pedro Lacerda
2012/2/8 Era Scarecrow <rtcvb32 at yahoo.com>
> On Tuesday, 7 February 2012 at 17:51:42 UTC, Pedro Lacerda wrote:
>
>> You can roll your own tagged union instead. The S struct can store long
>> and
>> byte[], S.ptr is a pointer to the data.
>>
>
>
> Yep, a bit like my code, except with switch cases covering all major
> types; That and trying to do comparison and setting functions, which seems
> a bit much. Testing it using templates and testing all the types seems like
> a nightmare. I've decided I need to pass it a ubyte array so I could do
> bounds checking before binding the memory to the union. One fewer things to
> worry about.
>
> Let's see... not that I expect you to use this, but I sorta have this as
> my enum set for types.
>
>
> enum ValueType {
> raw = 0x00000000, ///default upon unknown
> mask_types = 0xfff00000, ///Upper 4k defines general
> types, or 12 flag types
> mask_size = 0x000fffff, ///lower gives 1Million for
> size.
>
> i_types = 0x00100000, ///integars types
> i_8 = i_types + byte.sizeof,
> i_16 = i_types + short.sizeof,
> i_32 = i_types + int.sizeof,
> i_64 = i_types + long.sizeof,
>
> u_types = 0x00200000, ///unsigned numbers
> u_8 = u_types + ubyte.sizeof,
> u_16 = u_types + ushort.sizeof,
> u_32 = u_types + uint.sizeof,
> u_64 = u_types + ulong.sizeof,
>
> floating_types = 0x00400000,
> float_32 = floating_types + float.sizeof, fl = float_32,
> double_64 = floating_types + double.sizeof, doub = double_64,
>
>
> flags_types = 0x00800000,
> flags_8 = flags_types + ubyte.sizeof,
> flags_16 = flags_types + ushort.sizeof,
> flags_32 = flags_types + uint.sizeof,
> flags_64 = flags_types + ulong.sizeof,
>
> ranged_types = 0x01000000,
> ranged_8 = ranged_types + ubyte.sizeof,
> ranged_16 = ranged_types + ushort.sizeof,
> ranged_32 = ranged_types + uint.sizeof,
> ranged_64 = ranged_types + ulong.sizeof,
>
> voidp = 0x02000000, pointer_types = voidp, ///pointers
> i_8p, u_8p,
> i_16p, u_16p,
> i_32p, u_32p,
> i_64p, u_64p,
> float_32p, double_64p,
>
> var_str = 0x04000000,
> fixed_str = 0x08000000,
> var_ustr = 0x10000000,
> fixed_ustr = 0x20000000
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120208/9c0ab99c/attachment.html>
More information about the Digitalmars-d-learn
mailing list