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

Make inheritance code generation great #22

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ test-suite/djinni/vendor/third-party/proto/ts/*.js
/.metals
*.db
src/source/.metals/*
src/source/.scala-build/*
src/source/.scala-build/*
17 changes: 17 additions & 0 deletions examples/example.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ LevelB = record extends LevelA {
fieldB: string;
}

LevelB2 = record extends LevelB {
fieldB2: string;
}

LevelB2C = record extends LevelB2 {
fieldB2C: string;
}

LevelC = record extends LevelB {
fieldC: string;
}
Expand All @@ -18,6 +26,15 @@ LevelE = record extends LevelD {
fieldE: string;
}

LevelD2 = record extends LevelD {
fieldD2: string;
}

LevelF = record extends LevelE {

}

Container = record {
levels: list<LevelA>;
levelA : LevelA;
}
22 changes: 22 additions & 0 deletions examples/generated/android/djinni/java/src/Container.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

package djinni.java.src

import androidx.compose.runtime.Immutable
import java.util.ArrayList

@Immutable
data class Container(
val levels: ArrayList<LevelA>,
val levelA: LevelA
) {

override fun toString(): String {
return "Container {" +
"levels=" + levels +
"," + "levelA=" + levelA +
"}"
}

}
23 changes: 23 additions & 0 deletions examples/generated/android/djinni/java/src/LevelB2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

package djinni.java.src

import androidx.compose.runtime.Immutable

@Immutable
open class LevelB2(
override val fieldA: String,
override val fieldB: String,
open val fieldB2: String
) : LevelB(fieldA, fieldB) {

override fun toString(): String {
return "LevelB2 {" +
"fieldA=" + fieldA +
"," + "fieldB=" + fieldB +
"," + "fieldB2=" + fieldB2 +
"}"
}

}
25 changes: 25 additions & 0 deletions examples/generated/android/djinni/java/src/LevelB2C.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

package djinni.java.src

import androidx.compose.runtime.Immutable

@Immutable
data class LevelB2C(
override val fieldA: String,
override val fieldB: String,
override val fieldB2: String,
val fieldB2C: String
) : LevelB2(fieldA, fieldB, fieldB2) {

override fun toString(): String {
return "LevelB2C {" +
"fieldA=" + fieldA +
"," + "fieldB=" + fieldB +
"," + "fieldB2=" + fieldB2 +
"," + "fieldB2C=" + fieldB2C +
"}"
}

}
27 changes: 27 additions & 0 deletions examples/generated/android/djinni/java/src/LevelD2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

package djinni.java.src

import androidx.compose.runtime.Immutable

@Immutable
data class LevelD2(
override val fieldA: String,
override val fieldB: String,
override val fieldC: String,
override val fieldD: String,
val fieldD2: String
) : LevelD(fieldA, fieldB, fieldC, fieldD) {

override fun toString(): String {
return "LevelD2 {" +
"fieldA=" + fieldA +
"," + "fieldB=" + fieldB +
"," + "fieldC=" + fieldC +
"," + "fieldD=" + fieldD +
"," + "fieldD2=" + fieldD2 +
"}"
}

}
34 changes: 34 additions & 0 deletions examples/generated/android/jni/NativeContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

#include "NativeContainer.h" // my header
#include "Marshal.hpp"
#include "NativeLevelA.h"

namespace djinni_generated {

NativeContainer::NativeContainer() = default;

NativeContainer::~NativeContainer() = default;

auto NativeContainer::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> {
::djinni::LocalRef<JniType> r;
const auto& data = ::djinni::JniClass<NativeContainer>::get();
r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::List<::djinni_generated::NativeLevelA>::fromCpp(jniEnv, c.levels)),
::djinni::get(::djinni_generated::NativeLevelA::fromCpp(jniEnv, c.levelA)))};
::djinni::jniExceptionCheck(jniEnv);
return r;
}

auto NativeContainer::toCpp(JNIEnv* jniEnv, JniType j) -> CppType {
::djinni::JniLocalScope jscope(jniEnv, 3);
assert(j != nullptr);
const auto& data = ::djinni::JniClass<NativeContainer>::get();
::transitLib::viewModel::Container model;
model.levels = ::djinni::List<::djinni_generated::NativeLevelA>::toCpp(jniEnv, jniEnv->GetObjectField(j, data.field_levels));
model.levelA = ::djinni_generated::NativeLevelA::toCpp(jniEnv, jniEnv->GetObjectField(j, data.field_levelA));
return model;
}

} // namespace djinni_generated
33 changes: 33 additions & 0 deletions examples/generated/android/jni/NativeContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from example.djinni

#pragma once

#include "ContainerViewModel.h"
#include "djinni_support.hpp"

namespace djinni_generated {

class NativeContainer final {
public:
using CppType = ::transitLib::viewModel::Container;
using JniType = jobject;

using Boxed = NativeContainer;

~NativeContainer();

static CppType toCpp(JNIEnv* jniEnv, JniType j);
static ::djinni::LocalRef<JniType> fromCpp(JNIEnv* jniEnv, const CppType& c);

private:
NativeContainer();
friend ::djinni::JniClass<NativeContainer>;

const ::djinni::GlobalRef<jclass> clazz { ::djinni::jniFindClass("djinni/java/src/Container") };
const jmethodID jconstructor { ::djinni::jniGetMethodID(clazz.get(), "<init>", "(Ljava/util/ArrayList;Ldjinni/java/src/LevelA;)V") };
const jfieldID field_levels { ::djinni::jniGetFieldID(clazz.get(), "levels", "Ljava/util/ArrayList;") };
const jfieldID field_levelA { ::djinni::jniGetFieldID(clazz.get(), "levelA", "Ldjinni/java/src/LevelA;") };
};

} // namespace djinni_generated
55 changes: 49 additions & 6 deletions examples/generated/android/jni/NativeLevelA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

#include "NativeLevelA.h" // my header
#include "Marshal.hpp"
#include "NativeLevelB.h"
#include "NativeLevelB2.h"
#include "NativeLevelB2C.h"
#include "NativeLevelC.h"
#include "NativeLevelD.h"
#include "NativeLevelD2.h"
#include "NativeLevelE.h"
#include "NativeLevelF.h"

namespace djinni_generated {

Expand All @@ -11,19 +19,54 @@ NativeLevelA::NativeLevelA() = default;
NativeLevelA::~NativeLevelA() = default;

auto NativeLevelA::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> {
const auto& data = ::djinni::JniClass<NativeLevelA>::get();
auto r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::String::fromCpp(jniEnv, c.fieldA)))};
::djinni::jniExceptionCheck(jniEnv);
::djinni::LocalRef<JniType> r;
if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelD2>(c))
{
r = NativeLevelD2::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelF>(c))
{
r = NativeLevelF::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelE>(c))
{
r = NativeLevelE::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelD>(c))
{
r = NativeLevelD::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelC>(c))
{
r = NativeLevelC::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelB2C>(c))
{
r = NativeLevelB2C::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelB2>(c))
{
r = NativeLevelB2::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelB>(c))
{
r = NativeLevelB::fromCpp(jniEnv, myObject);
}
else {
const auto& data = ::djinni::JniClass<NativeLevelA>::get();
r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::String::fromCpp(jniEnv, c->fieldA)))};
::djinni::jniExceptionCheck(jniEnv);
}
return r;
}

auto NativeLevelA::toCpp(JNIEnv* jniEnv, JniType j) -> CppType {
::djinni::JniLocalScope jscope(jniEnv, 2);
assert(j != nullptr);
const auto& data = ::djinni::JniClass<NativeLevelA>::get();
::transitLib::viewModel::LevelA model;
model.fieldA = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldA));
std::shared_ptr<::transitLib::viewModel::LevelA> model;
model->fieldA = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldA));
return model;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/generated/android/jni/NativeLevelA.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace djinni_generated {

class NativeLevelA final {
public:
using CppType = ::transitLib::viewModel::LevelA;
using CppType = std::shared_ptr<::transitLib::viewModel::LevelA>;
using JniType = jobject;

using Boxed = NativeLevelA;
Expand Down
54 changes: 46 additions & 8 deletions examples/generated/android/jni/NativeLevelB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

#include "NativeLevelB.h" // my header
#include "Marshal.hpp"
#include "NativeLevelB2.h"
#include "NativeLevelB2C.h"
#include "NativeLevelC.h"
#include "NativeLevelD.h"
#include "NativeLevelD2.h"
#include "NativeLevelE.h"
#include "NativeLevelF.h"

namespace djinni_generated {

Expand All @@ -11,21 +18,52 @@ NativeLevelB::NativeLevelB() = default;
NativeLevelB::~NativeLevelB() = default;

auto NativeLevelB::fromCpp(JNIEnv* jniEnv, const CppType& c) -> ::djinni::LocalRef<JniType> {
const auto& data = ::djinni::JniClass<NativeLevelB>::get();
auto r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::String::fromCpp(jniEnv, c.fieldA)),
::djinni::get(::djinni::String::fromCpp(jniEnv, c.fieldB)))};
::djinni::jniExceptionCheck(jniEnv);
::djinni::LocalRef<JniType> r;
if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelD2>(c))
{
r = NativeLevelD2::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelF>(c))
{
r = NativeLevelF::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelE>(c))
{
r = NativeLevelE::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelD>(c))
{
r = NativeLevelD::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelC>(c))
{
r = NativeLevelC::fromCpp(jniEnv, myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelB2C>(c))
{
r = NativeLevelB2C::fromCpp(jniEnv, *myObject);
}
else if (auto myObject = dynamic_pointer_cast<::transitLib::viewModel::LevelB2>(c))
{
r = NativeLevelB2::fromCpp(jniEnv, myObject);
}
else {
const auto& data = ::djinni::JniClass<NativeLevelB>::get();
r = ::djinni::LocalRef<JniType>{jniEnv->NewObject(data.clazz.get(), data.jconstructor,
::djinni::get(::djinni::String::fromCpp(jniEnv, c->fieldA)),
::djinni::get(::djinni::String::fromCpp(jniEnv, c->fieldB)))};
::djinni::jniExceptionCheck(jniEnv);
}
return r;
}

auto NativeLevelB::toCpp(JNIEnv* jniEnv, JniType j) -> CppType {
::djinni::JniLocalScope jscope(jniEnv, 3);
assert(j != nullptr);
const auto& data = ::djinni::JniClass<NativeLevelB>::get();
::transitLib::viewModel::LevelB model;
model.fieldA = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldA));
model.fieldB = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldB));
std::shared_ptr<::transitLib::viewModel::LevelB> model;
model->fieldA = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldA));
model->fieldB = ::djinni::String::toCpp(jniEnv, (jstring)jniEnv->GetObjectField(j, data.field_fieldB));
return model;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/generated/android/jni/NativeLevelB.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace djinni_generated {

class NativeLevelB final {
public:
using CppType = ::transitLib::viewModel::LevelB;
using CppType = std::shared_ptr<::transitLib::viewModel::LevelB>;
using JniType = jobject;

using Boxed = NativeLevelB;
Expand Down
Loading