Generic functions to convert to void* and from void*

TSalm TSalm at free.fr
Tue Feb 24 13:45:24 PST 2009


> TSalm wrote:
>>>>> I'm trying to build function which have the hability to convert a  
>>>>> type
>>>>> to void* and from void*.
>>>
>>> First of all, I have to ask: have you looked at std.variant /
>>> tango.core.Variant?
>>>
>> Yes, but it seems that Variant class uses more memory than void* .
>
> The Phobos Variant will use however much space you reserve as the
> maximum, plus 4 bytes for a function pointer, but it can only store
> types as big as you allow for.  The Tango version will use
> max(real.sizeof,void[].sizeof) + 4 bytes for the typeid and can store
> anything you throw at it.
>
> For that extra space, both of these will give you runtime type safety,
> meaning you can't accidentally get the types wrong.  They're MUCH safer
> than void*.
>
In my case, there's also no possibility to get the wrong type, because it  
is managed by the type of the ColumnMem.


>>>>> [...]
>>
>>> I get the distinct impression that you're seriously over-thinking this.
>>>  Both of these functions could be rewritten as casts.  Aside from that,
>>> you've given no context for me to have any idea what you're trying to
>>> accomplish here.
>>>
>>
>> I'm really a newbie concerning the use of void* ( I think you have
>> notice this ;-)  )
>> Thanks for your usefull remarks.
>
> void* is just a pointer like any other.  It doesn't have any special
> properties except that you cannot dereference it; that's it.  If you're
> not sure how to use pointers, then don't.
>
> For example, you could store objects instead; this takes the same amount
> of storage in the DBMS, and allows for safe casting back to the original
> type.  Plus, you don't have to stuff about with casting things to void*
> and back.
>

>> I'm simply trying to make a little and lightweight "DBMS in memory".
>> Simply made by classes like TableMem, RowMem and ColumnMem(T).
>> There's also a "private" class which aims to store datas, using  
>> void*[][].
>
> Unless you really need to store small value types like integers, etc. in
> that private data, objects might be the best bet for now.
>

Thanks.

And about Object, if I want to store base type like int,double,etc..., if  
I do something like :

  Object o;
  int a = 30 ;
  o = cast(Object) &a ;

is this syntax is GC safe ?



More information about the Digitalmars-d-learn mailing list