Commit 837be4ad authored by tommi@chromium.org's avatar tommi@chromium.org

Adding a non-templatized version of QI for times when the IID and type aren't associated.

Also fixing the ScopedComPtrVector unit test.
The problem with the test was that it expected AddRef to be called twice and Release() once when pushing a pointer to the vector.  However that only happens when a push causes a reallocation.  When there's no reallocation, there's only a single call to AddRef and no call to Release, causing the test to report failure.  The fix is to simply use std::list instead and fix the expected values.

TEST=Run *ScopedComPtr* unit tests.
BUG=none
Review URL: http://codereview.chromium.org/149345

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20183 0039d316-1c4b-4281-b951-d872f2087c98
parent 3a4f703d
...@@ -89,6 +89,13 @@ class ScopedComPtr : public scoped_refptr<Interface> { ...@@ -89,6 +89,13 @@ class ScopedComPtr : public scoped_refptr<Interface> {
return ptr_->QueryInterface(p); return ptr_->QueryInterface(p);
} }
// QI for times when the IID is not associated with the type.
HRESULT QueryInterface(const IID& iid, void** obj) {
DCHECK(obj != NULL);
DCHECK(ptr_ != NULL);
return ptr_->QueryInterface(iid, obj);
}
// Queries |other| for the interface this object wraps and returns the // Queries |other| for the interface this object wraps and returns the
// error code from the other->QueryInterface operation. // error code from the other->QueryInterface operation.
HRESULT QueryFrom(IUnknown* object) { HRESULT QueryFrom(IUnknown* object) {
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/scoped_comptr_win.h"
#include <shlobj.h> #include <shlobj.h>
#include "base/scoped_comptr_win.h"
#include "base/scoped_ptr.h" #include "base/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -42,6 +43,12 @@ TEST(ScopedComPtrTest, ScopedComPtr) { ...@@ -42,6 +43,12 @@ TEST(ScopedComPtrTest, ScopedComPtr) {
ScopedComPtr<IMalloc> mem_alloc; ScopedComPtr<IMalloc> mem_alloc;
EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive()))); EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive())));
ScopedComPtr<IUnknown> qi_test;
EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_IUnknown,
reinterpret_cast<void**>(qi_test.Receive())));
EXPECT_TRUE(qi_test.get() != NULL);
qi_test.Release();
// test ScopedComPtr& constructor // test ScopedComPtr& constructor
ScopedComPtr<IMalloc> copy1(mem_alloc); ScopedComPtr<IMalloc> copy1(mem_alloc);
EXPECT_TRUE(copy1.IsSameObject(mem_alloc)); EXPECT_TRUE(copy1.IsSameObject(mem_alloc));
...@@ -73,6 +80,7 @@ TEST(ScopedComPtrTest, ScopedComPtrVector) { ...@@ -73,6 +80,7 @@ TEST(ScopedComPtrTest, ScopedComPtrVector) {
// Verify we don't get error C2558. // Verify we don't get error C2558.
typedef ScopedComPtr<Dummy, &dummy_iid> Ptr; typedef ScopedComPtr<Dummy, &dummy_iid> Ptr;
std::vector<Ptr> bleh; std::vector<Ptr> bleh;
scoped_ptr<Dummy> p(new Dummy); scoped_ptr<Dummy> p(new Dummy);
{ {
Ptr p2(p.get()); Ptr p2(p.get());
...@@ -84,14 +92,16 @@ TEST(ScopedComPtrTest, ScopedComPtrVector) { ...@@ -84,14 +92,16 @@ TEST(ScopedComPtrTest, ScopedComPtrVector) {
p3 = p2; p3 = p2;
EXPECT_EQ(p->adds, 3); EXPECT_EQ(p->adds, 3);
EXPECT_EQ(p->releases, 1); EXPECT_EQ(p->releases, 1);
// To avoid hitting a reallocation.
bleh.reserve(1);
bleh.push_back(p2); bleh.push_back(p2);
EXPECT_EQ(p->adds, 5); EXPECT_EQ(p->adds, 4);
EXPECT_EQ(p->releases, 2); EXPECT_EQ(p->releases, 1);
EXPECT_EQ(bleh[0], p.get()); EXPECT_EQ(bleh[0], p.get());
bleh.pop_back(); bleh.pop_back();
EXPECT_EQ(p->adds, 5); EXPECT_EQ(p->adds, 4);
EXPECT_EQ(p->releases, 3); EXPECT_EQ(p->releases, 2);
} }
EXPECT_EQ(p->adds, 5); EXPECT_EQ(p->adds, 4);
EXPECT_EQ(p->releases, 5); EXPECT_EQ(p->releases, 4);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment