2014-07-30 17:59:52 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-09-23 04:27:34 +03:00
|
|
|
#include "mozilla/RefCounted.h"
|
2014-07-30 17:59:52 +04:00
|
|
|
|
|
|
|
using mozilla::RefCounted;
|
|
|
|
|
|
|
|
class Foo : public RefCounted<Foo> {
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(Foo)
|
|
|
|
|
|
|
|
Foo() : mDead(false) {}
|
|
|
|
|
|
|
|
static int sNumDestroyed;
|
|
|
|
|
|
|
|
~Foo() {
|
|
|
|
MOZ_ASSERT(!mDead);
|
|
|
|
mDead = true;
|
|
|
|
sNumDestroyed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mDead;
|
|
|
|
};
|
|
|
|
int Foo::sNumDestroyed;
|
|
|
|
|
|
|
|
struct Bar : public Foo {};
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Foo> NewFoo() {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f(new Foo());
|
2015-05-01 16:14:16 +03:00
|
|
|
return f.forget();
|
2014-07-30 17:59:52 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Foo> NewBar() {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Bar> bar = new Bar();
|
2015-04-30 22:17:08 +03:00
|
|
|
return bar.forget();
|
2014-07-30 17:59:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetNewFoo(Foo** aFoo) {
|
|
|
|
*aFoo = new Bar();
|
|
|
|
// Kids, don't try this at home
|
|
|
|
(*aFoo)->AddRef();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void GetNewFoo(RefPtr<Foo>* aFoo) { *aFoo = new Bar(); }
|
2014-07-30 17:59:52 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Foo> GetNullFoo() { return 0; }
|
2014-07-30 17:59:52 +04:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
MOZ_RELEASE_ASSERT(0 == Foo::sNumDestroyed);
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f = new Foo();
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(f->refCount() == 1);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f1 = NewFoo();
|
|
|
|
RefPtr<Foo> f2(NewFoo());
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed);
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> b = NewBar();
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed);
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f1;
|
2014-07-30 17:59:52 +04:00
|
|
|
{
|
|
|
|
f1 = new Foo();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f2(f1);
|
|
|
|
RefPtr<Foo> f3 = f2;
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(5 == Foo::sNumDestroyed);
|
|
|
|
|
|
|
|
{
|
2015-06-17 17:00:52 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
RefPtr<Foo> g = f.forget();
|
2015-06-17 17:00:52 +03:00
|
|
|
}
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(6 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f = new Foo();
|
2015-10-18 07:40:10 +03:00
|
|
|
GetNewFoo(getter_AddRefs(f));
|
2014-07-30 17:59:52 +04:00
|
|
|
MOZ_RELEASE_ASSERT(7 == Foo::sNumDestroyed);
|
|
|
|
}
|
|
|
|
MOZ_RELEASE_ASSERT(8 == Foo::sNumDestroyed);
|
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f = new Foo();
|
2014-07-30 17:59:52 +04:00
|
|
|
GetNewFoo(&f);
|
2015-09-24 06:07:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(9 == Foo::sNumDestroyed);
|
2014-07-30 17:59:52 +04:00
|
|
|
}
|
2015-09-24 06:07:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(10 == Foo::sNumDestroyed);
|
2014-07-30 17:59:52 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
{ RefPtr<Foo> f1 = new Bar(); }
|
2015-09-24 06:07:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
2014-07-30 17:59:52 +04:00
|
|
|
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Foo> f = GetNullFoo();
|
2015-09-24 06:07:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
2014-07-30 17:59:52 +04:00
|
|
|
}
|
2015-09-24 06:07:29 +03:00
|
|
|
MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed);
|
2014-07-30 17:59:52 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|