Re: Generic functions to convert to void* and from void*

2009-02-25 Thread TSalm
Le Wed, 25 Feb 2009 01:03:32 +0100, Daniel Keep  
daniel.keep.li...@gmail.com a écrit:




TSalm wrote:

In my case, there's also no possibility to get the wrong type, because
it is managed by the type of the ColumnMem.


You still have to get the code right.  There's a surprising number of
corner cases trying to store arbitrary types.


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 ?


It's not safe, period.  If the compiler lets you do that, I'd be
tremendously surprised; even more surprised if it doesn't cause major
problems later.  This is what I'm worried about; you're doing dangerous
things with a type system you don't understand.  Don't do this.

Here's the problem: void* isn't going to work for everything.  It'll
work for Object references, other pointers, and that's it.  You can't
store arrays, and you can't store value types like structs or primitive
types.  For that, you need to allocate heap storage, copy the value and
then store the pointer to that.  Oh, and don't forget that fixed-length
arrays have value semantics whereas dynamic arrays and slices have
reference semantics; although you generally solve that issue by having a
special template for your type which rewrites T[n] as T[].  Also,
delegates won't fit, but function pointers will.

This is why I was pointing you at Variant because I already went through
the trouble to solve all this once.  :P

If you still want to do this with void*, build that code in isolation
and test the heck out of it.


You are right.
I will first use Variant to implements all functionnalities, and  
insolating the storage type.

And in the future, I will be able to use void* as an improvement.


Re: Generic functions to convert to void* and from void*

2009-02-24 Thread TSalm



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 ?



Re: Generic functions to convert to void* and from void*

2009-02-24 Thread Daniel Keep

TSalm wrote:
 In my case, there's also no possibility to get the wrong type, because
 it is managed by the type of the ColumnMem.

You still have to get the code right.  There's a surprising number of
corner cases trying to store arbitrary types.

 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 ?

It's not safe, period.  If the compiler lets you do that, I'd be
tremendously surprised; even more surprised if it doesn't cause major
problems later.  This is what I'm worried about; you're doing dangerous
things with a type system you don't understand.  Don't do this.

Here's the problem: void* isn't going to work for everything.  It'll
work for Object references, other pointers, and that's it.  You can't
store arrays, and you can't store value types like structs or primitive
types.  For that, you need to allocate heap storage, copy the value and
then store the pointer to that.  Oh, and don't forget that fixed-length
arrays have value semantics whereas dynamic arrays and slices have
reference semantics; although you generally solve that issue by having a
special template for your type which rewrites T[n] as T[].  Also,
delegates won't fit, but function pointers will.

This is why I was pointing you at Variant because I already went through
the trouble to solve all this once.  :P

If you still want to do this with void*, build that code in isolation
and test the heck out of it.

  -- Daniel


Re: Generic functions to convert to void* and from void*

2009-02-23 Thread TSalm

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* .


[...]



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.

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*[][].


Re: Generic functions to convert to void* and from void*

2009-02-23 Thread Daniel Keep

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*.

 [...]
 
 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.

  -- Daniel


Generic functions to convert to void* and from void*

2009-02-22 Thread TSalm

Hello,

I'm trying to build function which have the hability to convert a type to  
void* and from void*.

I must use ref in the toPtr function because of this :
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=15600

Do you think that what I done is correct ?


/* - CODE - */

/***
 * Convert a value to a void*
 * Params:
 * val =
 * Returns:
 */
void* toPtr(T)(ref T val)
{
  void* p ;

  static if ( is( T b : Type ) )
  {
p = new T ;
p = val ;
  }
  else
  {
p = val ;
  }

  return p ;
}


/***
 * Convert a void* to his value
 * Params:
 * ptr =
 * Returns:
 */
T fromPtr(T)(void* ptr)
{
  return *(cast(T*)ptr) ;
}
/* --- END CODE  */


Thanks in advance,
TSalm


Re: Generic functions to convert to void* and from void*

2009-02-22 Thread TSalm


I'm trying to build function which have the hability to convert a type  
to void* and from void*.

I must use ref in the toPtr function because of this :
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=15600

Do you think that what I done is correct ?


/* - CODE - */

/***
  * Convert a value to a void*
  * Params:
  * val =
  * Returns:
  */
void* toPtr(T)(ref T val)
{
   void* p ;

   static if ( is( T b : Type ) )
   {
 p = new T ;
 p = val ;
   }
   else
   {
 p = val ;
   }

   return p ;
}


/***
  * Convert a void* to his value
  * Params:
  * ptr =
  * Returns:
  */
T fromPtr(T)(void* ptr)
{
   return *(cast(T*)ptr) ;
}
/* --- END CODE  */



I've forget to say that toPtr can't be call with constants.


Re: Generic functions to convert to void* and from void*

2009-02-22 Thread Daniel Keep


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?

 I must use ref in the toPtr function because of this :
 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.comgroup=digitalmars.D.learnartnum=15600


 Do you think that what I done is correct ?


 /* - CODE - */

 /***
   * Convert a value to a void*
   * Params:
   * val =
   * Returns:
   */
 void* toPtr(T)(ref T val)
 {
void* p ;

static if ( is( T b : Type ) )

What is this test doing?  You're checking to see if T can be implicitly
cast to Type... but Type isn't defined.  And what is 'b' doing there?

{
  p = new T ;
  p = val ;

You never use the freshly-allocated T.  You assign it to p and then
immediately overwrite it with val.

}
else
{
  p = val ;
}

return p ;
 }

So the function is basically doing this:

T fromPtr(T)(ref T val)
{
return val;
}

 /***
   * Convert a void* to his value
   * Params:
   * ptr =
   * Returns:
   */
 T fromPtr(T)(void* ptr)
 {
return *(cast(T*)ptr) ;
 }
 /* --- END CODE  */

 
 I've forget to say that toPtr can't be call with constants.

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.

  -- Daniel