/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include "mozilla/NotNull.h" #include "mozilla/RefPtr.h" #include "mozilla/UniquePtr.h" #include "mozilla/Unused.h" using mozilla::MakeNotNull; using mozilla::NotNull; using mozilla::UniquePtr; using mozilla::WrapNotNull; #define CHECK MOZ_RELEASE_ASSERT class Blah { public: Blah() : mX(0) {} void blah(){}; int mX; }; // A simple smart pointer that implicity converts to and from T*. template class MyPtr { T* mRawPtr; public: MyPtr() : mRawPtr(nullptr) {} MOZ_IMPLICIT MyPtr(T* aRawPtr) : mRawPtr(aRawPtr) {} T* get() const { return mRawPtr; } operator T*() const { return get(); } T* operator->() const { return get(); } }; // A simple class that works with RefPtr. It keeps track of the maximum // refcount value for testing purposes. class MyRefType { int mExpectedMaxRefCnt; int mMaxRefCnt; int mRefCnt; public: explicit MyRefType(int aExpectedMaxRefCnt) : mExpectedMaxRefCnt(aExpectedMaxRefCnt), mMaxRefCnt(0), mRefCnt(0) {} ~MyRefType() { CHECK(mMaxRefCnt == mExpectedMaxRefCnt); } uint32_t AddRef() { mRefCnt++; if (mRefCnt > mMaxRefCnt) { mMaxRefCnt = mRefCnt; } return mRefCnt; } uint32_t Release() { CHECK(mRefCnt > 0); mRefCnt--; if (mRefCnt == 0) { delete this; return 0; } return mRefCnt; } }; void f_i(int* aPtr) {} void f_my(MyPtr aPtr) {} void f_nni(NotNull aPtr) {} void f_nnmy(NotNull> aPtr) {} void TestNotNullWithMyPtr() { int i4 = 4; int i5 = 5; MyPtr my4 = &i4; MyPtr my5 = &i5; NotNull nni4 = WrapNotNull(&i4); NotNull nni5 = WrapNotNull(&i5); NotNull> nnmy4 = WrapNotNull(my4); // WrapNotNull(nullptr); // no wrapping from nullptr // WrapNotNull(0); // no wrapping from zero // NotNull construction combinations // NotNull nni4a; // no default // NotNull nni4a(nullptr); // no nullptr // NotNull nni4a(0); // no zero // NotNull nni4a(&i4); // no int* // NotNull nni4a(my4); // no MyPtr NotNull nni4b(WrapNotNull(&i4)); // WrapNotNull(int*) NotNull nni4c(WrapNotNull(my4)); // WrapNotNull(MyPtr) NotNull nni4d(nni4); // NotNull NotNull nni4e(nnmy4); // NotNull> CHECK(*nni4b == 4); CHECK(*nni4c == 4); CHECK(*nni4d == 4); CHECK(*nni4e == 4); // NotNull> construction combinations // NotNull> nnmy4a; // no default // NotNull> nnmy4a(nullptr); // no nullptr // NotNull> nnmy4a(0); // no zero // NotNull> nnmy4a(&i4); // no int* // NotNull> nnmy4a(my4); // no MyPtr NotNull> nnmy4b(WrapNotNull(&i4)); // WrapNotNull(int*) NotNull> nnmy4c(WrapNotNull(my4)); // WrapNotNull(MyPtr) NotNull> nnmy4d(nni4); // NotNull NotNull> nnmy4e(nnmy4); // NotNull> CHECK(*nnmy4b == 4); CHECK(*nnmy4c == 4); CHECK(*nnmy4d == 4); CHECK(*nnmy4e == 4); // NotNull assignment combinations // nni4b = nullptr; // no nullptr // nni4b = 0; // no zero // nni4a = &i4; // no int* // nni4a = my4; // no MyPtr nni4b = WrapNotNull(&i4); // WrapNotNull(int*) nni4c = WrapNotNull(my4); // WrapNotNull(MyPtr) nni4d = nni4; // NotNull nni4e = nnmy4; // NotNull> CHECK(*nni4b == 4); CHECK(*nni4c == 4); CHECK(*nni4d == 4); CHECK(*nni4e == 4); // NotNull> assignment combinations // nnmy4a = nullptr; // no nullptr // nnmy4a = 0; // no zero // nnmy4a = &i4; // no int* // nnmy4a = my4; // no MyPtr nnmy4b = WrapNotNull(&i4); // WrapNotNull(int*) nnmy4c = WrapNotNull(my4); // WrapNotNull(MyPtr) nnmy4d = nni4; // NotNull nnmy4e = nnmy4; // NotNull> CHECK(*nnmy4b == 4); CHECK(*nnmy4c == 4); CHECK(*nnmy4d == 4); CHECK(*nnmy4e == 4); NotNull> nnmy5 = WrapNotNull(&i5); CHECK(*nnmy5 == 5); CHECK(nnmy5 == &i5); // NotNull> == int* CHECK(nnmy5 == my5); // NotNull> == MyPtr CHECK(nnmy5 == nni5); // NotNull> == NotNull CHECK(nnmy5 == nnmy5); // NotNull> == NotNull> CHECK(&i5 == nnmy5); // int* == NotNull> CHECK(my5 == nnmy5); // MyPtr == NotNull> CHECK(nni5 == nnmy5); // NotNull == NotNull> CHECK(nnmy5 == nnmy5); // NotNull> == NotNull> // CHECK(nni5 == nullptr); // no comparisons with nullptr // CHECK(nullptr == nni5); // no comparisons with nullptr // CHECK(nni5 == 0); // no comparisons with zero // CHECK(0 == nni5); // no comparisons with zero CHECK(*nnmy5 == 5); CHECK(nnmy5 != &i4); // NotNull> != int* CHECK(nnmy5 != my4); // NotNull> != MyPtr CHECK(nnmy5 != nni4); // NotNull> != NotNull CHECK(nnmy5 != nnmy4); // NotNull> != NotNull> CHECK(&i4 != nnmy5); // int* != NotNull> CHECK(my4 != nnmy5); // MyPtr != NotNull> CHECK(nni4 != nnmy5); // NotNull != NotNull> CHECK(nnmy4 != nnmy5); // NotNull> != NotNull> // CHECK(nni4 != nullptr); // no comparisons with nullptr // CHECK(nullptr != nni4); // no comparisons with nullptr // CHECK(nni4 != 0); // no comparisons with zero // CHECK(0 != nni4); // no comparisons with zero // int* parameter f_i(&i4); // identity int* --> int* f_i(my4); // implicit MyPtr --> int* f_i(my4.get()); // explicit MyPtr --> int* f_i(nni4); // implicit NotNull --> int* f_i(nni4.get()); // explicit NotNull --> int* // f_i(nnmy4); // no implicit NotNull> --> int* f_i(nnmy4.get()); // explicit NotNull> --> int* f_i(nnmy4.get().get()); // doubly-explicit NotNull> --> int* // MyPtr parameter f_my(&i4); // implicit int* --> MyPtr f_my(my4); // identity MyPtr --> MyPtr f_my(my4.get()); // explicit MyPtr --> MyPtr // f_my(nni4); // no implicit NotNull --> MyPtr f_my(nni4.get()); // explicit NotNull --> MyPtr f_my(nnmy4); // implicit NotNull> --> MyPtr f_my(nnmy4.get()); // explicit NotNull> --> MyPtr f_my( nnmy4.get().get()); // doubly-explicit NotNull> --> MyPtr // NotNull parameter f_nni(nni4); // identity NotNull --> NotNull f_nni(nnmy4); // implicit NotNull> --> NotNull // NotNull> parameter f_nnmy(nni4); // implicit NotNull --> NotNull> f_nnmy(nnmy4); // identity NotNull> --> NotNull> // CHECK(nni4); // disallow boolean conversion / unary expression usage // CHECK(nnmy4); // ditto // '->' dereferencing. Blah blah; MyPtr myblah = &blah; NotNull nnblah = WrapNotNull(&blah); NotNull> nnmyblah = WrapNotNull(myblah); (&blah)->blah(); // int* myblah->blah(); // MyPtr nnblah->blah(); // NotNull nnmyblah->blah(); // NotNull> (&blah)->mX = 1; CHECK((&blah)->mX == 1); myblah->mX = 2; CHECK(myblah->mX == 2); nnblah->mX = 3; CHECK(nnblah->mX == 3); nnmyblah->mX = 4; CHECK(nnmyblah->mX == 4); // '*' dereferencing (lvalues and rvalues) *(&i4) = 7; // int* CHECK(*(&i4) == 7); *my4 = 6; // MyPtr CHECK(*my4 == 6); *nni4 = 5; // NotNull CHECK(*nni4 == 5); *nnmy4 = 4; // NotNull> CHECK(*nnmy4 == 4); // Non-null arrays. static const int N = 20; int a[N]; NotNull nna = WrapNotNull(a); for (int i = 0; i < N; i++) { nna[i] = i; } for (int i = 0; i < N; i++) { nna[i] *= 2; } for (int i = 0; i < N; i++) { CHECK(nna[i] == i * 2); } } void f_ref(NotNull aR) { NotNull> r = aR; } void TestNotNullWithRefPtr() { // This MyRefType object will have a maximum refcount of 5. NotNull> r1 = WrapNotNull(new MyRefType(5)); // At this point the refcount is 1. NotNull> r2 = r1; // At this point the refcount is 2. NotNull r3 = r2; (void)r3; // At this point the refcount is still 2. RefPtr r4 = r2; mozilla::Unused << r4; // At this point the refcount is 3. RefPtr r5 = r3.get(); mozilla::Unused << r5; // At this point the refcount is 4. // No change to the refcount occurs because of the argument passing. Within // f_ref() the refcount temporarily hits 5, due to the local RefPtr. f_ref(r2); // At this point the refcount is 4. NotNull> r6 = std::move(r2); mozilla::Unused << r6; CHECK(r2.get()); CHECK(r6.get()); // At this point the refcount is 5 again, since NotNull is not movable. // At function's end all RefPtrs are destroyed and the refcount drops to 0 // and the MyRefType is destroyed. } // Create a derived object and store its base pointer. struct Base { virtual ~Base() = default; virtual bool IsDerived() const { return false; } }; struct Derived : Base { bool IsDerived() const override { return true; } }; void TestMakeNotNull() { // Raw pointer. auto nni = MakeNotNull(11); static_assert(std::is_same_v, decltype(nni)>, "MakeNotNull should return NotNull"); CHECK(*nni == 11); delete nni; // Raw pointer to const. auto nnci = MakeNotNull(12); static_assert(std::is_same_v, decltype(nnci)>, "MakeNotNull should return NotNull"); CHECK(*nnci == 12); delete nnci; auto nnd = MakeNotNull(); static_assert(std::is_same_v, decltype(nnd)>, "MakeNotNull should return NotNull"); CHECK(nnd->IsDerived()); delete nnd; NotNull nnb = MakeNotNull(); static_assert(std::is_same_v, decltype(nnb)>, "MakeNotNull should be assignable to NotNull"); // Check that we have really built a Derived object. CHECK(nnb->IsDerived()); delete nnb; // Allow smart pointers. auto nnmi = MakeNotNull>(23); static_assert(std::is_same_v>, decltype(nnmi)>, "MakeNotNull> should return NotNull>"); CHECK(*nnmi == 23); delete nnmi.get().get(); auto nnui = MakeNotNull>(24); static_assert( std::is_same_v>, decltype(nnui)>, "MakeNotNull> should return NotNull>"); CHECK(*nnui == 24); // Expect only 1 RefCnt (from construction). auto nnr = MakeNotNull>(1); static_assert(std::is_same_v>, decltype(nnr)>, "MakeNotNull> should return " "NotNull>"); mozilla::Unused << nnr; } mozilla::MovingNotNull> CreateNotNullUniquePtr() { return mozilla::WrapMovingNotNull(mozilla::MakeUnique(42)); } void TestMovingNotNull() { UniquePtr x1 = CreateNotNullUniquePtr(); CHECK(x1); CHECK(42 == *x1); NotNull> x2 = CreateNotNullUniquePtr(); CHECK(42 == *x2); NotNull> x3 = mozilla::WrapMovingNotNull(mozilla::MakeUnique()); // Must not compile: // auto y = CreateNotNullUniquePtr(); } int main() { TestNotNullWithMyPtr(); TestNotNullWithRefPtr(); TestMakeNotNull(); TestMovingNotNull(); return 0; }