Generic functions to convert to void* and from void*

TSalm TSalm at free.fr
Sun Feb 22 08:09:42 PST 2009


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.com&group=digitalmars.D.learn&artnum=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


More information about the Digitalmars-d-learn mailing list