Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to represent C++ object inheritance as Java object inheritance using HybridClass? #96

Open
mrousavy opened this issue Aug 16, 2024 · 2 comments

Comments

@mrousavy
Copy link
Contributor

Hey all! I've been using fbjni in a lot of libraries now, and now I stumbled upon an interesting task.

I have pure, shared C++ classes that define virtual methods. E.g. Car, and SportsCar which inherits from Car, overrides a few methods and adds some methods.

// Car.cpp
class Car {
public:
  virtual double getSpeed() = 0;
  virtual bool isFast() = 0;
};

// SportsCar.cpp
class SportsCar: public Car {
public:
  virtual double getSpeed() = 0;
  virtual bool isFast() { return true; } 
};

I want to resemble that C++ inheritance in Java, without adding something to those C++ classes (they are shared between platforms).

So my understanding is that I create the same types in Java:

// Car.java
public abstract class Car {
  HybridData mHybridData;
  Car() {
    mHybridData = initHybrid();
  }

  abstract double getSpeed();
  abstract boolean isFast();
  private static native HybridData initHybrid();
}

// SportsCar.java
public abstract class SportsCar extends Car {
  HybridData mHybridData;
  SportsCar() {
    super();
    mHybridData = initHybrid();
  }

  @Override
  boolean isFast() { return true; }

  abstract double getSpeed();
  private static native HybridData initHybrid();
}

..and then how do I properly bridge them to C++ using fbjni? Can my fbjni class inherit from both the Java base, and the SportsCar base?

// JCar.cpp
struct JCar : public jni::HybridClass<JCar>, public Car {
public:
  static auto constexpr kJavaDescriptor = "Lcom/mrousavy/Car;";
  
  double getSpeed() override {
    // TODO: get `speed` from java part, with proper virtual lookup
  }
  bool isFast() override {
    // TODO: get `isFast` from java part, with proper virtual lookup
  }

  static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis);
  static void registerNatives();
}

// JSportsCar.cpp
struct JSportsCar : public jni::HybridClass<JSportsCar, JCar>, public SportsCar {
public:
  static auto constexpr kJavaDescriptor = "Lcom/mrousavy/SportsCar;";
  
  double getSpeed() override {
    // TODO: get `speed` from java part, with proper virtual lookup
  }
  bool isFast() override {
    // TODO: get `isFast` from java part, with proper virtual lookup
  }

  static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis);
  static void registerNatives();
}

Can I call initHybrid() in both base and inherited class? Do I now have two base instances of the same type (Car) in memory?

@mrousavy mrousavy changed the title How to build a proper HybridClass inheritance? How to represent C++ object inheritance as Java object inheritance using HybridClass? Aug 16, 2024
@mrousavy
Copy link
Contributor Author

cc @passy, maybe you know the answer to this :)

@mrousavy
Copy link
Contributor Author

I think what I want is to use virtual inheritance to avoid having Car twice as my base class.

class JCar : public HybridClass<JCar>, virtual public Car;
class JSportsCar : public HybridClass<JSportsCar, JCar>, virtual public SportsCar;

But I can't seem to properly construct that hybrid object then:

- Constructor inherited by 'HybridClass<JSportsCar, JCar>' from base class 'JCar' is implicitly deleted

🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant