Skip to content

Commit

Permalink
Allow std::nullptr_t for all Scopable types (this was an oversight …
Browse files Browse the repository at this point in the history
…in the API).

In rare instances classes may want to have uninitialized members that reside on the stack. Previously, the base declaration will call the constructor with no arguments, but this API provides for declaring members with "LocalObject<kClass> obj{nullptr};" syntax.

#274

PiperOrigin-RevId: 617944956
  • Loading branch information
jwhpryor authored and copybara-github committed Mar 21, 2024
1 parent b9be99d commit d7d9c5c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion implementation/global_object_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;

TEST_F(JniTest, GlobalObject_AllowsNullPtrT) {
static constexpr Class kClass{"kClass"};
EXPECT_CALL(*env_, NewLocalRef).Times(0);
EXPECT_CALL(*env_, NewGlobalRef).Times(0);
EXPECT_CALL(*env_, DeleteLocalRef).Times(0);
EXPECT_CALL(*env_, DeleteGlobalRef).Times(0);

GlobalObject<kClass> obj{nullptr};
EXPECT_EQ(jobject{obj}, nullptr);
}

TEST_F(JniTest, GlobalObject_CallsNewAndDeleteOnNewObject) {
static constexpr Class kClass{"kClass"};

Expand All @@ -65,7 +76,7 @@ TEST_F(JniTest, GlobalObject_ConstructsFromNonStandardConstructor) {

GlobalObject<kClass> global_object{1.f, 2.f};

EXPECT_NE(jobject{global_object}, nullptr);
EXPECT_EQ(jobject{global_object}, AsGlobal(Fake<jobject>()));
}

TEST_F(JniTest, GlobalObject_DoesNotDeleteAnyLocalsForAdoptedGlobalJobject) {
Expand Down
8 changes: 8 additions & 0 deletions implementation/local_object_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ using ::testing::StrEq;
static constexpr Class kClass{"kClass"};
static constexpr Class kClass2{"kClass2"};

TEST_F(JniTest, LocalObject_AllowsNullPtrT) {
EXPECT_CALL(*env_, NewLocalRef).Times(0);
EXPECT_CALL(*env_, DeleteLocalRef).Times(0);

LocalObject<kClass> obj{nullptr};
EXPECT_EQ(jobject{obj}, nullptr);
}

TEST_F(JniTest, LocalObject_DoesntTryToDeleteNull) {
EXPECT_CALL(*env_, NewLocalRef).Times(0);
EXPECT_CALL(*env_, DeleteLocalRef).Times(0);
Expand Down
1 change: 0 additions & 1 deletion implementation/local_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class LocalString : public LocalStringImpl {
using Base = LocalStringImpl;
using Base::Base;

LocalString(std::nullptr_t) : Base(jstring{nullptr}) {}
LocalString(LocalObject<kJavaLangString>&& obj)
: Base(AdoptLocal{}, static_cast<jstring>(obj.Release())) {}

Expand Down
2 changes: 2 additions & 0 deletions implementation/object_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class ObjectRef
}

public:
ObjectRef(std::nullptr_t) : RefBase(nullptr) {}

explicit ObjectRef(RefBaseTag<typename JniT::StorageType>&& rhs)
: RefBase(std::move(rhs)) {}

Expand Down

0 comments on commit d7d9c5c

Please sign in to comment.