www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static member functions

reply John Fletcher <J.P.Fletcher aston.ac.uk> writes:
I am working with some automatically generated D interface code where
code for each C++ class being wrapped is put into a different file.  The
problem is that the classes interact, and that I have a problem with
permission to see a specific static member function between classes.  It
all works if the code is all in one file.

Example:

  public class A {
    private void* Ptr;
    private static void* getPtr(A obj) {
     return (obj === null) ? null : Ptr; }
  }

is an extract from automatically generated code.

If a second class B has a constructor with an A as an argument the code
generated is

  public class B {
  // ...
    public this(A a)  {
    // contains   A.getPtr(a) which fails unless the code for B is in
the same file as the code for A.
    // This applies even if I change the permission of the static
function to public.
    }
  }

One workaround is to put into the defintion of A the extra member
function

  public void* Ptr() { return getPtr(this); }

which can be seen from the other file, i.e. putting a.Ptr() for
A.getPtr().

I am in a position to edit the code which does the automatic generation,
and the aim is to find a general solution which would result in the
generation of valid code which works.  There should be no need to edit
the result.

At the moment the question is, is there a way in which the code of class
A could be altered so that class B can see  A.setPtr(obj) even when they
are in separate files?

More details of the background to this are on

http://www.prowiki.org/wiki4d/wiki.cgi?DwithSwig

Thanks

John Fletcher
Jun 15 2004
next sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
Declaring getPtr as static and public should work. I recommend getting an
example of the problem with as few lines as possible and post it on
digitalmars.D.bugs.

John Fletcher wrote:

 I am working with some automatically generated D interface code where
 code for each C++ class being wrapped is put into a different file.  The
 problem is that the classes interact, and that I have a problem with
 permission to see a specific static member function between classes.  It
 all works if the code is all in one file.
 
 Example:
 
   public class A {
     private void* Ptr;
     private static void* getPtr(A obj) {
      return (obj === null) ? null : Ptr; }
   }
 
 is an extract from automatically generated code.
 
 If a second class B has a constructor with an A as an argument the code
 generated is
 
   public class B {
   // ...
     public this(A a)  {
     // contains   A.getPtr(a) which fails unless the code for B is in
 the same file as the code for A.
     // This applies even if I change the permission of the static
 function to public.
     }
   }
 
 One workaround is to put into the defintion of A the extra member
 function
 
   public void* Ptr() { return getPtr(this); }

 which can be seen from the other file, i.e. putting a.Ptr() for
 A.getPtr().
 
 I am in a position to edit the code which does the automatic generation,
 and the aim is to find a general solution which would result in the
 generation of valid code which works.  There should be no need to edit
 the result.
 
 At the moment the question is, is there a way in which the code of class
 A could be altered so that class B can see  A.setPtr(obj) even when they
 are in separate files?
 
 More details of the background to this are on
 
 http://www.prowiki.org/wiki4d/wiki.cgi?DwithSwig
 
 Thanks
 
 John Fletcher
Jun 15 2004
parent reply john bunbury28.pluc.com writes:
In article <camq9o$1sb1$1 digitaldaemon.com>, Ben Hinkle says...
Declaring getPtr as static and public should work. I recommend getting an
example of the problem with as few lines as possible and post it on
digitalmars.D.bugs.
Thanks for the hint. It isn't a bug, but a misunderstanding of mine. The problem is that the automatically generated file has the same name as the class within it. When class A has a public static member e.g. class A { public static void* getPtr(A obj) { // something } } The structure in a class B importing a class A is that A is the module and the class is referred to as A.A import A; class B { public void* getAPtr(A obj) { return A.A.getPtr(a); // This works. } //public void* getAPtr(A obj) { // return A.getPtr(a); This fails. //} } I now need to alter the SWIG code generator for D to do the correct thing. Thanks again. John
Jun 16 2004
parent John Fletcher <J.P.Fletcher aston.ac.uk> writes:
john bunbury28.plus.com wrote:

 In article <camq9o$1sb1$1 digitaldaemon.com>, Ben Hinkle says...
Declaring getPtr as static and public should work. I recommend getting an
example of the problem with as few lines as possible and post it on
digitalmars.D.bugs.
Thanks for the hint. It isn't a bug, but a misunderstanding of mine. The problem is that the automatically generated file has the same name as the class within it. I now need to alter the SWIG code generator for D to do the correct thing. Thanks again. John
The alternative is to rename the file e.g. classA.d instead of A.d This is what I have done. John
Jun 18 2004
prev sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
John Fletcher wrote:

 I am working with some automatically generated D interface code where
 code for each C++ class being wrapped is put into a different file.  The
 problem is that the classes interact, and that I have a problem with
 permission to see a specific static member function between classes.  It
 all works if the code is all in one file.
 
 Example:
 
   public class A {
     private void* Ptr;
     private static void* getPtr(A obj) {
      return (obj === null) ? null : Ptr; }
   }
It might just be a typo in the post, but still: accessing "Ptr" within a static function will not work. Instead, it should be "obj.Ptr".
Jun 18 2004