/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ #include #include #include "nsCOMPtr.h" #include "nsISupports.h" #ifdef HAVE_CPP_NEW_CASTS #define STATIC_CAST(T,x) static_cast(x) #define REINTERPRET_CAST(T,x) reinterpret_cast(x) #else #define STATIC_CAST(T,x) ((T)(x)) #define REINTERPRET_CAST(T,x) ((T)(x)) #endif #define NS_IFOO_IID \ { 0x6f7652e0, 0xee43, 0x11d1, \ { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } } class IFoo : public nsISupports { public: static const nsIID& GetIID() { static nsIID iid = NS_IFOO_IID; return iid; } public: IFoo(); virtual ~IFoo(); NS_IMETHOD_(nsrefcnt) AddRef(); NS_IMETHOD_(nsrefcnt) Release(); NS_IMETHOD QueryInterface( const nsIID&, void** ); static void print_totals(); private: unsigned int refcount_; static unsigned int total_constructions_; static unsigned int total_destructions_; }; class IBar; // some types I'll need typedef unsigned long NS_RESULT; // some functions I'll need (and define below) nsresult CreateIFoo( void** ); nsresult CreateIBar( void** result ); void AnIFooPtrPtrContext( IFoo** ); void AnISupportsPtrPtrContext( nsISupports** ); void AVoidPtrPtrContext( void** ); void set_a_IFoo( nsCOMPtr* result ); nsCOMPtr return_a_IFoo(); unsigned int IFoo::total_constructions_; unsigned int IFoo::total_destructions_; class test_message { public: test_message() { cout << "BEGIN unit tests for |nsCOMPtr|, compiled " __DATE__ << endl; } ~test_message() { IFoo::print_totals(); cout << "END unit tests for |nsCOMPtr|." << endl; } }; test_message gTestMessage; /* ... */ void IFoo::print_totals() { cout << "total constructions/destructions --> " << total_constructions_ << "/" << total_destructions_ << endl; } IFoo::IFoo() : refcount_(0) { ++total_constructions_; cout << " new IFoo@" << STATIC_CAST(void*, this) << " [#" << total_constructions_ << "]" << endl; } IFoo::~IFoo() { ++total_destructions_; cout << "IFoo@" << STATIC_CAST(void*, this) << "::~IFoo()" << " [#" << total_destructions_ << "]" << endl; } nsrefcnt IFoo::AddRef() { ++refcount_; cout << "IFoo@" << STATIC_CAST(void*, this) << "::AddRef(), refcount --> " << refcount_ << endl; return refcount_; } nsrefcnt IFoo::Release() { int wrap_message = (refcount_ == 1); if ( wrap_message ) cout << ">>"; --refcount_; cout << "IFoo@" << STATIC_CAST(void*, this) << "::Release(), refcount --> " << refcount_ << endl; if ( !refcount_ ) { cout << " delete IFoo@" << STATIC_CAST(void*, this) << endl; delete this; } if ( wrap_message ) cout << "<>CreateIFoo() --> "; IFoo* foop = new IFoo; cout << "IFoo@" << STATIC_CAST(void*, foop) << endl; foop->AddRef(); *result = foop; cout << "<* result ) { cout << ">>set_a_IFoo()" << endl; assert(result); nsCOMPtr foop( do_QueryInterface(new IFoo) ); *result = foop; cout << "< return_a_IFoo() { cout << ">>return_a_IFoo()" << endl; nsCOMPtr foop( do_QueryInterface(new IFoo) ); cout << "<>CreateIBar() --> "; IBar* barp = new IBar; cout << "IBar@" << STATIC_CAST(void*, barp) << endl; barp->AddRef(); *result = barp; cout << "<QueryInterface(IFoo::GetIID(), REINTERPRET_CAST(void**, &fooP)) ) ) { try { fooP->print_totals(); } catch( ... ) { NS_RELEASE(fooP); throw; } NS_RELEASE(fooP); } } catch( ... ) { NS_RELEASE(barP); throw; } NS_RELEASE(barP); } return result; } #endif // TEST_EXCEPTIONS static nsresult TestBloat_Raw_Unsafe() { IBar* barP = 0; nsresult result = CreateIBar(REINTERPRET_CAST(void**, &barP)); if ( barP ) { IFoo* fooP = 0; if ( NS_SUCCEEDED( result = barP->QueryInterface(IFoo::GetIID(), REINTERPRET_CAST(void**, &fooP)) ) ) { fooP->print_totals(); NS_RELEASE(fooP); } NS_RELEASE(barP); } return result; } static nsresult TestBloat_Smart() { nsCOMPtr barP; nsresult result = CreateIBar( getter_AddRefs(barP) ); nsCOMPtr fooP( do_QueryInterface(barP, &result) ); if ( fooP ) fooP->print_totals(); return result; } nsCOMPtr gFoop; int main() { cout << ">>main()" << endl; cout << "sizeof(nsCOMPtr) --> " << sizeof(nsCOMPtr) << endl; #ifdef TEST_EXCEPTIONS TestBloat_Raw(); #endif // TEST_EXCEPTIONS TestBloat_Raw_Unsafe(); TestBloat_Smart(); { cout << endl << "### Test 1: will a |nsCOMPtr| call |AddRef| on a pointer assigned into it?" << endl; nsCOMPtr foop( do_QueryInterface(new IFoo) ); cout << endl << "### Test 2: will a |nsCOMPtr| |Release| its old pointer when a new one is assigned in?" << endl; foop = do_QueryInterface(new IFoo); // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand? //foop->AddRef(); // [Shouldn't compile] Is it a compile time error to try to |Release| be hand? //foop->Release(); // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|? //delete foop; /* Solaris Workshop compiler fails to compile this. */ #if !defined(XP_UNIX) || !(defined(SOLARIS) && !defined(__GNUG__)) cout << endl << "### Test 3: can you |AddRef| if you must?" << endl; STATIC_CAST(IFoo*, foop)->AddRef(); cout << endl << "### Test 4: can you |Release| if you must?" << endl; STATIC_CAST(IFoo*, foop)->Release(); #endif cout << endl << "### Test 5: will a |nsCOMPtr| |Release| when it goes out of scope?" << endl; } { cout << endl << "### Test 6: will a |nsCOMPtr| call the correct destructor?" << endl; nsCOMPtr foop( do_QueryInterface(new IBar) ); } { cout << endl << "### Test 7: can you compare one |nsCOMPtr| with another [!=]?" << endl; nsCOMPtr foo1p( do_QueryInterface(new IFoo) ); // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|? //AnIFooPtrPtrContext(&foo1p); // [Shouldn't compile] Is it a compile time error to omit |getter_[doesnt_]AddRef[s]|? //AVoidPtrPtrContext(&foo1p); nsCOMPtr foo2p( do_QueryInterface(new IFoo) ); if ( foo1p != foo2p ) cout << "foo1p != foo2p" << endl; else cout << "foo1p == foo2p" << endl; cout << endl << "### Test 7.5: can you compare a |nsCOMPtr| with NULL, 0, nsnull [!=]?" << endl; if ( foo1p != 0 ) cout << "foo1p != 0" << endl; if ( 0 != foo1p ) cout << "0 != foo1p" << endl; if ( foo1p == 0 ) cout << "foo1p == 0" << endl; if ( 0 == foo1p ) cout << "0 == foo1p" << endl; IFoo* raw_foo2p = foo2p.get(); cout << endl << "### Test 8: can you compare a |nsCOMPtr| with a raw interface pointer [!=]?" << endl; if ( foo1p.get() != raw_foo2p ) cout << "foo1p != raw_foo2p" << endl; else cout << "foo1p == raw_foo2p" << endl; cout << endl << "### Test 9: can you assign one |nsCOMPtr| into another?" << endl; foo1p = foo2p; cout << endl << "### Test 10: can you compare one |nsCOMPtr| with another [==]?" << endl; if ( foo1p == foo2p ) cout << "foo1p == foo2p" << endl; else cout << "foo1p != foo2p" << endl; cout << endl << "### Test 11: can you compare a |nsCOMPtr| with a raw interface pointer [==]?" << endl; if ( raw_foo2p == foo2p.get() ) cout << "raw_foo2p == foo2p" << endl; else cout << "raw_foo2p != foo2p" << endl; #if 1 cout << endl << "### Test 11.5: can you compare a |nsCOMPtr| with a raw interface pointer [==]?" << endl; if ( nsCOMPtr( dont_QueryInterface(raw_foo2p) ) == foo2p ) cout << "raw_foo2p == foo2p" << endl; else cout << "raw_foo2p != foo2p" << endl; #endif cout << endl << "### Test 12: bare pointer test?" << endl; if ( foo1p ) cout << "foo1p is not NULL" << endl; else cout << "foo1p is NULL" << endl; cout << endl << "### Test 13: numeric pointer test?" << endl; if ( foo1p == 0 ) cout << "foo1p is NULL" << endl; else cout << "foo1p is not NULL" << endl; #if 0 if ( foo1p == 1 ) cout << "foo1p allowed compare with in" << endl; #endif cout << endl << "### Test 14: how about when two |nsCOMPtr|s refering to the same object go out of scope?" << endl; } { cout << endl << "### Test 15,16 ...setup..." << endl; IFoo* raw_foo1p = new IFoo; raw_foo1p->AddRef(); IFoo* raw_foo2p = new IFoo; raw_foo2p->AddRef(); cout << endl << "### Test 15: what if I don't want to |AddRef| when I construct?" << endl; nsCOMPtr foo1p( dont_AddRef(raw_foo1p) ); //nsCOMPtr foo1p = dont_AddRef(raw_foo1p); cout << endl << "### Test 16: what if I don't want to |AddRef| when I assign in?" << endl; nsCOMPtr foo2p; foo2p = dont_AddRef(raw_foo2p); } { cout << endl << "### setup for Test 17" << endl; nsCOMPtr foop; cout << "### Test 17: basic parameter behavior?" << endl; CreateIFoo( nsGetterAddRefs(foop) ); } cout << "### End Test 17" << endl; { cout << endl << "### setup for Test 18" << endl; nsCOMPtr foop; cout << "### Test 18: basic parameter behavior, using the short form?" << endl; CreateIFoo( getter_AddRefs(foop) ); } cout << "### End Test 18" << endl; { cout << endl << "### setup for Test 19, 20" << endl; nsCOMPtr foop; cout << "### Test 19: reference parameter behavior?" << endl; set_a_IFoo(&foop); cout << "### Test 20: return value behavior?" << endl; foop = return_a_IFoo(); } cout << "### End Test 19, 20" << endl; { cout << endl << "### setup for Test 21" << endl; nsCOMPtr fooP; cout << "### Test 21: is |QueryInterface| called on assigning in a raw pointer?" << endl; fooP = do_QueryInterface(new IFoo); } cout << "### End Test 21" << endl; { cout << endl << "### setup for Test 22" << endl; nsCOMPtr fooP; fooP = do_QueryInterface(new IFoo); nsCOMPtr foo2P; cout << "### Test 22: is |QueryInterface| _not_ called when assigning in a smart-pointer of the same type?" << endl; foo2P = fooP; } cout << "### End Test 22" << endl; { cout << endl << "### setup for Test 23" << endl; nsCOMPtr barP( do_QueryInterface(new IBar) ); cout << "### Test 23: is |QueryInterface| called when assigning in a smart-pointer of a different type?" << endl; nsCOMPtr fooP( do_QueryInterface(barP) ); if ( fooP ) cout << "an IBar* is an IFoo*" << endl; } cout << "### End Test 23" << endl; { cout << endl << "### setup for Test 24" << endl; nsCOMPtr fooP; IFoo* rawFooP = new IFoo; cout << "### Test 24: is |QueryInterface| _not_ called when explicitly barred?" << endl; fooP = dont_QueryInterface(rawFooP); cout << "### cleanup for Test 24" << endl; } cout << "### End Test 24" << endl; { nsCOMPtr fooP; AnIFooPtrPtrContext( getter_AddRefs(fooP) ); AVoidPtrPtrContext( getter_AddRefs(fooP) ); AnISupportsPtrPtrContext( getter_AddRefs(fooP) ); } { nsCOMPtr supportsP; AVoidPtrPtrContext( getter_AddRefs(supportsP) ); AnISupportsPtrPtrContext( getter_AddRefs(supportsP) ); } cout << endl << "### Test 25: will a static |nsCOMPtr| |Release| before program termination?" << endl; gFoop = do_QueryInterface(new IFoo); cout << "<