Skip to content

Commit

Permalink
Update JVariant.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 24, 2024
1 parent 813d33d commit 48594a9
Showing 1 changed file with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,53 @@ class JVariant2 : public jni::JavaClass<JVariant2<A, B>> {
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/Variant2;";
using jni::JavaClass<JVariant2<A, B>>::self;

template<typename T>
bool is() {
if constexpr (std::is_same_v<T, A>) {
return jni::isObjectRefType(self(), JFirst::javaClassStatic());
} else if constexpr (std::is_same_v<T, B>) {
return jni::isObjectRefType(self(), JSecond::javaClassStatic());
} else {
static_assert("Type " + std::string(typeid(T).name()) + " is not part of this variant! "
"(" + std::string(typeid(decltype(this)).name()) + ")");
}
}

class JFirst : public jni::JavaClass<JFirst, JVariant2<A, jobject>> {
public:
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/Variant2$First;";
class JFirst : public jni::JavaClass<JFirst, JVariant2<A, jobject>> {
public:
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/Variant2$First;";
A getValue() {
static const auto method = JFirst::javaClassStatic()->getMethod<A()>("getValue");
return method(this->self());
}
};
class JSecond : public jni::JavaClass<JSecond, JVariant2<jobject, B>> {
public:
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/Variant2$Second;";
B getValue() {
static const auto method = JSecond::javaClassStatic()->getMethod<B()>("getValue");
return method(this->self());
}
};

static jni::local_ref<JFirst> create(A value) {
return JFirst::newInstance(value);
}

A getValue() {
static const auto method = JFirst::javaClassStatic()->getMethod<A()>("getValue");
return method(this->self());
}
};
class JSecond : public jni::JavaClass<JSecond, JVariant2<jobject, B>> {
public:
static constexpr auto kJavaDescriptor = "Lcom/margelo/nitro/Variant2$Second;";
static jni::local_ref<JVariant2<A, B>> create(A value) {
return JFirst::newInstance(value);
}
static jni::local_ref<JVariant2<A, B>> create(B value) {
return JSecond::newInstance(value);
}

static jni::local_ref<JSecond> create(B value) {
return JSecond::newInstance(value);
}
template <typename T>
bool is() {
if constexpr (std::is_same_v<T, A>) {
return jni::isObjectRefType(self(), JFirst::javaClassStatic());
} else if constexpr (std::is_same_v<T, B>) {
return jni::isObjectRefType(self(), JSecond::javaClassStatic());
} else {
static_assert("Type " + std::string(typeid(T).name()) + " is not part of this variant! (" +
std::string(typeid(decltype(this)).name()) + ")");
}
}

B getValue() {
static const auto method = JSecond::javaClassStatic()->getMethod<B()>("getValue");
return method(this->self());
}
};
template <typename T>
T get() {
if constexpr (std::is_same_v<T, A>) {
return jni::static_ref_cast<JFirst>(self())->getValue();
} else if constexpr (std::is_same_v<T, B>) {
return jni::static_ref_cast<JSecond>(self())->getValue();
} else {
static_assert("Type " + std::string(typeid(T).name()) + " is not part of this variant! (" +
std::string(typeid(decltype(this)).name()) + ")");
}
}
};


} // namespace margelo::nitro

0 comments on commit 48594a9

Please sign in to comment.