/* -*- 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/. */
// 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;
// Non-null arrays. staticconstint N = 20; int a[N];
NotNull<int*> 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<MyRefType*> aR) { NotNull<RefPtr<MyRefType>> r = aR; }
void TestNotNullWithRefPtr() { // This MyRefType object will have a maximum refcount of 5.
NotNull<RefPtr<MyRefType>> r1 = WrapNotNull(new MyRefType(5));
// 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 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; virtualbool IsDerived() const { returnfalse; }
}; struct Derived : Base { bool IsDerived() const override { returntrue; }
};
void TestMakeNotNull() { // Raw pointer. auto nni = MakeNotNull<int*>(11);
static_assert(std::is_same_v<NotNull<int*>, decltype(nni)>, "MakeNotNull should return NotNull");
CHECK(*nni == 11); delete nni;
// Raw pointer to const. auto nnci = MakeNotNull<constint*>(12);
static_assert(std::is_same_v<NotNull<constint*>, decltype(nnci)>, "MakeNotNull should return NotNull");
CHECK(*nnci == 12); delete nnci;
auto nnd = MakeNotNull<Derived*>();
static_assert(std::is_same_v<NotNull<Derived*>, decltype(nnd)>, "MakeNotNull should return NotNull");
CHECK(nnd->IsDerived()); delete nnd;
NotNull<Base*> nnb = MakeNotNull<Derived*>();
static_assert(std::is_same_v<NotNull<Base*>, 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<MyPtr<int>>(23);
static_assert(std::is_same_v<NotNull<MyPtr<int>>, decltype(nnmi)>, "MakeNotNull> should return NotNull>");
CHECK(*nnmi == 23); delete nnmi.get().get();
auto nnui = MakeNotNull<UniquePtr<int>>(24);
static_assert(
std::is_same_v<NotNull<UniquePtr<int>>, decltype(nnui)>, "MakeNotNull> should return NotNull>");
CHECK(*nnui == 24);
// Expect only 1 RefCnt (from construction). auto nnr = MakeNotNull<RefPtr<MyRefType>>(1);
static_assert(std::is_same_v<NotNull<RefPtr<MyRefType>>, decltype(nnr)>, "MakeNotNull> should return " "NotNull>");
mozilla::Unused << nnr;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.