www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting to a voldemort type

reply Alex <sascha.orlov gmail.com> writes:
Finally. A question about Voldemort types :)

I have the following class, with an accompanying function. Skip 
the code to the link for a runnable version.
/*--- code begin ---*/
class roof
{
     int huhu = 9;
     void* funVoldemort(size_t my_size)
     {
         auto gg = huhu;
         if(my_size < 5)
         {
             return new size_t(3);
         }
         struct Vold
         {
             int begin;
             int end;
             bool b;

             int ff()
             {
                 return gg;
             }

         }
         return new Vold();
     }
}

void rara_fun(void* ptr, uint mysize)
{
     if(mysize < 5)
     {
         writeln("rara_fun: ", *(cast(size_t*)ptr));
     }
     else
     {
         //???
     }
}
/*--- code end ---*/

see
http://dpaste.dzfl.pl/2e361adf04b6
for a runnable copy.

First of all: it is a little bit unsatisfying, that the 
funVoldemort is not working with an auto or auto ref return type. 
But I see, the types are different after all. And, to solve this 
part I changed the return type to a void*.

The question is, how to write the accompanied function in the 
part, which should cast the pointer to the voldemort type?

What I already thought about is: writing a union inside the 
struct, which is either my long or some other data. But this is 
in general not possible, as the "other data" could be together 
greater, then the long.

The obvious solution would be: to define the struct outside the 
function. But the point is, that these structs only defines 
behavior and the data inside them is just auxiliary data to pass 
the queries to the real data storages.

The question is motivated by the following:
I try to write some recursive structure. And if the accompanying 
function is called, it passes the question to a voldemort object. 
The voldemort object by itself doesn't store the information but 
knows, how to reduce the "my_size" parameter and which other 
voldemort object to ask. This goes on, till the real data is 
accessed.
Mar 19 2016
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 19 March 2016 at 15:10:56 UTC, Alex wrote:
     void* funVoldemort(size_t my_size)
The term 'voldemort type' refers to a public type, just an unnamed one. What you have here is a pointer to a private type... and void* is something you often should avoid since the compiler doesn't help you with them. You can pass an entirely different object and the function would never know (until it crashes). It might look like a feature now, passing those size_t's around, but you'll probably find it buggy later...
 The question is, how to write the accompanied function in the 
 part, which should cast the pointer to the voldemort type?
You can't, the struct is too private to be seen. ff 20 and encapsulate it all in the same struct.
 What I already thought about is: writing a union inside the 
 struct, which is either my long or some other data. But this is 
 in general not possible, as the "other data" could be together 
 greater, then the long.
The compiler knows how to handle that, or you could use a wrapped pointer and ordinary private definitions so the outside doesn't pry too far.
Mar 19 2016