Skip to content

Commit 154a5e8

Browse files
committed
Allow implicit conversions from Object<Derived> to Object<Base>
1 parent 25a40bf commit 154a5e8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

include/jni/object.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ namespace jni
6060
: obj(&o)
6161
{}
6262

63+
template < class Tag >
64+
Object(const Object<Tag>& o, std::enable_if_t< std::is_convertible<Tag, TagType>::value >* = 0)
65+
: obj(o.Get())
66+
{}
67+
6368
explicit operator bool() const { return obj; }
6469

6570
operator UntaggedObjectType*() const { return obj; }

test/high_level.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace
2121
void Void(jni::JNIEnv&, jni::jboolean b) { assert(b == jni::jni_true); }
2222
static void Static(jni::JNIEnv&, Peer&) {}
2323
};
24+
25+
struct Base {};
26+
struct Derived : public Base {};
2427
}
2528

2629
int main()
@@ -120,6 +123,10 @@ int main()
120123
string.NewWeakGlobalRef(env);
121124
string.NewLocalRef(env);
122125

126+
jni::Object<Derived> derived;
127+
jni::Object<Base> base(derived);
128+
base = derived;
129+
(void)[] () -> jni::Object<Base> { return jni::Object<Derived>(); };
123130

124131
/// Constructor
125132

0 commit comments

Comments
 (0)