dpp icon indicating copy to clipboard operation
dpp copied to clipboard

ENH: parse C++ pure virtual function, and if a class only contains C++ pure virtual function, translate to D interface.

Open mw66 opened this issue 1 year ago • 3 comments

$ cat source/cpp.dpp 

class PriceBarI {
 public:
  // OHLC chart
  virtual double open_()   = 0;
  virtual double high_()   = 0;
  virtual double low_()    = 0;
  virtual double close_()  = 0;
  virtual double volume_() = 0;
  virtual int64  millis_() = 0;
};

$ ~/.dub/packages/dpp/0.5.4/dpp/bin/d++ --keep-d-files --preprocess-only --parse-as-cpp source/cpp.dpp

$ tail  source/cpp.d
class PriceBarI {
 public:
  // OHLC chart
  virtual double open_() = 0;
  virtual double high_() = 0;
  virtual double low_() = 0;
  virtual double close_() = 0;
  virtual double volume_() = 0;
  virtual int64 millis_() = 0;
};

Looks like the C++ pure virtual function is not recognized.

It will be nice to parse C++ pure virtual function, and if a class only contains C++ pure virtual function:

  1. translate to D interface
  2. translate C++ pure virtual functions to D abstract methods.

Then it will be easier to interface D and C++:

https://dlang.org/spec/cpp_interface.html#using_d_classes_from_cpp

so the expected output are:

extern (C++) {                                                                                                                                                                                                          
interface PriceBarI {                                                                                                                                                                                                   
 public:                                                                                                                                                                                                                
  // OHLC chart                                                                                                                                                                                                         
  abstract double open_();                                                                                                                                                                                              
  abstract double high_();                                                                                                                                                                                              
  abstract double low_();                                                                                                                                                                                               
  abstract double close_();                                                                                                                                                                                             
  abstract double volume_();                                                                                                                                                                                            
  abstract long millis_();                                                                                                                                                                                              
};                                                                                                                                                                                                                      
}    

This example can be used as unittest case.

mw66 avatar Jun 30 '23 23:06 mw66