Skip to content

Commit b14b8b9

Browse files
committed
Make Object/Class/Array non-reassignable except by UniquePointerlike
1 parent 6ca9cd8 commit b14b8b9

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

include/jni/array.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ namespace jni
3030
private:
3131
UntaggedType* array = nullptr;
3232

33+
template < class T, class D > friend class UniquePointerlike;
34+
35+
void reset(UntaggedType* a) { array = a; }
36+
3337
public:
3438
explicit Array(std::nullptr_t = nullptr)
3539
{}
@@ -42,6 +46,13 @@ namespace jni
4246
: array(&a)
4347
{}
4448

49+
Array(const Array& a)
50+
: array(a.array)
51+
{}
52+
53+
// Not reassignable; it would break UniquePointerlike's abstraction.
54+
Array& operator=(const Array&) = delete;
55+
4556
explicit operator bool() const { return array; }
4657

4758
operator UntaggedType*() const { return array; }
@@ -103,6 +114,10 @@ namespace jni
103114
private:
104115
UntaggedType* array = nullptr;
105116

117+
template < class T, class D > friend class UniquePointerlike;
118+
119+
void reset(UntaggedType* a) { array = a; }
120+
106121
public:
107122
explicit Array(std::nullptr_t = nullptr)
108123
{}
@@ -115,6 +130,13 @@ namespace jni
115130
: array(&a)
116131
{}
117132

133+
Array(const Array& a)
134+
: array(a.array)
135+
{}
136+
137+
// Not reassignable; it would break UniquePointerlike's abstraction.
138+
Array& operator=(const Array&) = delete;
139+
118140
explicit operator bool() const { return array; }
119141

120142
operator UntaggedType*() const { return array; }

include/jni/class.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ namespace jni
2525
private:
2626
jclass* clazz = nullptr;
2727

28+
template < class T, class D > friend class UniquePointerlike;
29+
30+
void reset(jclass* c) { clazz = c; }
31+
2832
public:
2933
using TagType = TheTag;
3034

@@ -35,6 +39,13 @@ namespace jni
3539
: clazz(&c)
3640
{}
3741

42+
Class(const Class& c)
43+
: clazz(c.clazz)
44+
{}
45+
46+
// Not reassignable; it would break UniquePointerlike's abstraction.
47+
Class& operator=(const Class&) = delete;
48+
3849
explicit operator bool() const { return clazz; }
3950

4051
operator jclass&() const { return *clazz; }

include/jni/object.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ namespace jni
3939
private:
4040
UntaggedObjectType* obj = nullptr;
4141

42+
template < class T, class D > friend class UniquePointerlike;
43+
44+
void reset(UntaggedObjectType* o) { obj = o; }
45+
4246
public:
4347
explicit Object(std::nullptr_t = nullptr)
4448
{}
@@ -51,11 +55,18 @@ namespace jni
5155
: obj(&o)
5256
{}
5357

58+
Object(const Object& o)
59+
: obj(o.obj)
60+
{}
61+
5462
template < class Tag >
5563
Object(const Object<Tag>& o, std::enable_if_t< std::is_convertible<Tag, TagType>::value >* = nullptr)
5664
: obj(o.Get())
5765
{}
5866

67+
// Not reassignable; it would break UniquePointerlike's abstraction.
68+
Object& operator=(const Object&) = delete;
69+
5970
explicit operator bool() const { return obj; }
6071

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

include/jni/unique_pointerlike.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace jni
5656
void reset(T&& t = T())
5757
{
5858
T current = pointerlike;
59-
pointerlike = std::move(t);
59+
pointerlike.reset(t.Get());
6060
if (current)
6161
{
6262
get_deleter()(current.Get());
@@ -66,7 +66,7 @@ namespace jni
6666
T release()
6767
{
6868
T current = pointerlike;
69-
pointerlike = T();
69+
pointerlike.reset(nullptr);
7070
return current;
7171
}
7272

test/high_level.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ int main()
125125

126126
jni::Object<Derived> derived;
127127
jni::Object<Base> base(derived);
128-
base = derived;
129128
(void)[] () -> jni::Object<Base> { return jni::Object<Derived>(); };
130129
(void)[] () -> jni::Object<> { return jni::String(); };
131130

0 commit comments

Comments
 (0)