www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do i depend on struct composition

reply "Mariusz `shd` =?UTF-8?B?R2xpd2nFhHNraSI=?= <alienballance gmail.com> writes:
Let me try to illustrate my question by following pseudo-code:

struct InnerA
{
   /* .. */
   void* ptr;
}
struct InnerB
{
   /* .. */
   void* ptr;
}
struct InnerC
{
   void operate()
   {
     auto ptr = this-(void*).sizeof;
     /* do something with pointer */
   }
}

struct Compound(Inner)
{
   Inner  a_or_b;
   InnerC c;
}

I need to pass "c" to some subsystem which shouldn't be aware of 
"a_or_b"

However, subsystem should be able to call function 
InnerB.operate() which needs access to "a_or_b".

Now my questions:
* Is there any way to tell subsystem, that he can't instantiate 
nor move InnerC
* Is there any better,platform-independent way of accessing 
pointer from struct a_or_b ?

Thanks in advance,
Mariusz
Aug 22 2013
parent reply "JS" <js.mdnq gmail.com> writes:
On Thursday, 22 August 2013 at 21:07:27 UTC, Mariusz `shd` 
Gliwiński wrote:
 Let me try to illustrate my question by following pseudo-code:

 struct InnerA
 {
   /* .. */
   void* ptr;
 }
 struct InnerB
 {
   /* .. */
   void* ptr;
 }
 struct InnerC
 {
   void operate()
   {
     auto ptr = this-(void*).sizeof;
     /* do something with pointer */
   }
 }

 struct Compound(Inner)
 {
   Inner  a_or_b;
   InnerC c;
 }

 I need to pass "c" to some subsystem which shouldn't be aware 
 of "a_or_b"

 However, subsystem should be able to call function 
 InnerB.operate() which needs access to "a_or_b".

 Now my questions:
 * Is there any way to tell subsystem, that he can't instantiate 
 nor move InnerC
 * Is there any better,platform-independent way of accessing 
 pointer from struct a_or_b ?

 Thanks in advance,
 Mariusz
If you actually nest InnerC inside Compound then it will contain a pointer(using this) to the parent. You can then use `parent` inside `operator` to access the parent which can access a_or_b. if you need to return a member from a_or_b without going through a_or_b then just wrap it. I'm not sure exactly what you are trying to do but I think it can easily be done with standard techniques.
Aug 22 2013
parent reply "anonymous" <anonymous example.com> writes:
On Thursday, 22 August 2013 at 22:32:53 UTC, JS wrote:
 On Thursday, 22 August 2013 at 21:07:27 UTC, Mariusz `shd` 
 Gliwiński wrote:
[...]
 struct InnerC
 {
  void operate()
  {
    auto ptr = this-(void*).sizeof;
    /* do something with pointer */
  }
 }

 struct Compound(Inner)
 {
  Inner  a_or_b;
  InnerC c;
 }
[...]
 If you actually nest InnerC inside Compound then it will 
 contain a pointer(using this) to the parent. You can then use 
 `parent` inside `operator` to access the parent which can 
 access a_or_b.
I think you're thinking of nested classes[1] here. Nested structs[2] are a little different. A nested struct is associated with a function, not with another struct. (By the way, I guess you mean `outer` instead of `parent`.) [1] http://dlang.org/class.html#nested [2] http://dlang.org/struct.html#nested
Aug 22 2013
parent "JS" <js.mdnq gmail.com> writes:
On Thursday, 22 August 2013 at 23:10:40 UTC, anonymous wrote:
 On Thursday, 22 August 2013 at 22:32:53 UTC, JS wrote:
 On Thursday, 22 August 2013 at 21:07:27 UTC, Mariusz `shd` 
 Gliwiński wrote:
[...]
 struct InnerC
 {
 void operate()
 {
   auto ptr = this-(void*).sizeof;
   /* do something with pointer */
 }
 }

 struct Compound(Inner)
 {
 Inner  a_or_b;
 InnerC c;
 }
[...]
 If you actually nest InnerC inside Compound then it will 
 contain a pointer(using this) to the parent. You can then use 
 `parent` inside `operator` to access the parent which can 
 access a_or_b.
I think you're thinking of nested classes[1] here. Nested structs[2] are a little different. A nested struct is associated with a function, not with another struct. (By the way, I guess you mean `outer` instead of `parent`.) [1] http://dlang.org/class.html#nested [2] http://dlang.org/struct.html#nested
Your right, I don't know why there isn't an equivalent for structs. In any case, check out http://dpaste.dzfl.pl/7f086694 and http://www.digitalmars.com/d/archives/digitalmars/D/Nested_Structs_183295.html for a solution
Aug 22 2013