Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsQueryObject_h
8 : #define nsQueryObject_h
9 :
10 : #include "mozilla/Attributes.h"
11 :
12 : #include "nsCOMPtr.h"
13 : #include "mozilla/RefPtr.h"
14 :
15 : /*****************************************************************************/
16 :
17 : template<class T>
18 : class MOZ_STACK_CLASS nsQueryObject final : public nsCOMPtr_helper
19 : {
20 : public:
21 28125 : explicit nsQueryObject(T* aRawPtr)
22 28125 : : mRawPtr(aRawPtr)
23 : {
24 28125 : }
25 :
26 28125 : virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
27 : void** aResult) const
28 : {
29 28125 : nsresult status = mRawPtr ? mRawPtr->QueryInterface(aIID, aResult)
30 28125 : : NS_ERROR_NULL_POINTER;
31 28125 : return status;
32 : }
33 : private:
34 : T* MOZ_NON_OWNING_REF mRawPtr;
35 : };
36 :
37 : template<class T>
38 : class MOZ_STACK_CLASS nsQueryObjectWithError final : public nsCOMPtr_helper
39 : {
40 : public:
41 3 : nsQueryObjectWithError(T* aRawPtr, nsresult* aErrorPtr)
42 3 : : mRawPtr(aRawPtr), mErrorPtr(aErrorPtr)
43 : {
44 3 : }
45 :
46 3 : virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
47 : void** aResult) const
48 : {
49 3 : nsresult status = mRawPtr ? mRawPtr->QueryInterface(aIID, aResult)
50 3 : : NS_ERROR_NULL_POINTER;
51 3 : if (mErrorPtr) {
52 3 : *mErrorPtr = status;
53 : }
54 3 : return status;
55 : }
56 : private:
57 : T* MOZ_NON_OWNING_REF mRawPtr;
58 : nsresult* mErrorPtr;
59 : };
60 :
61 : /*****************************************************************************/
62 :
63 : /*****************************************************************************/
64 :
65 : template<class T>
66 : inline nsQueryObject<T>
67 28118 : do_QueryObject(T* aRawPtr)
68 : {
69 28118 : return nsQueryObject<T>(aRawPtr);
70 : }
71 :
72 : template<class T>
73 : inline nsQueryObject<T>
74 3 : do_QueryObject(nsCOMPtr<T>& aRawPtr)
75 : {
76 3 : return nsQueryObject<T>(aRawPtr);
77 : }
78 :
79 : template<class T>
80 : inline nsQueryObject<T>
81 4 : do_QueryObject(RefPtr<T>& aRawPtr)
82 : {
83 4 : return nsQueryObject<T>(aRawPtr);
84 : }
85 :
86 : template<class T>
87 : inline nsQueryObjectWithError<T>
88 : do_QueryObject(T* aRawPtr, nsresult* aErrorPtr)
89 : {
90 : return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
91 : }
92 :
93 : template<class T>
94 : inline nsQueryObjectWithError<T>
95 3 : do_QueryObject(nsCOMPtr<T>& aRawPtr, nsresult* aErrorPtr)
96 : {
97 3 : return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
98 : }
99 :
100 : template<class T>
101 : inline nsQueryObjectWithError<T>
102 : do_QueryObject(RefPtr<T>& aRawPtr, nsresult* aErrorPtr)
103 : {
104 : return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
105 : }
106 :
107 : /*****************************************************************************/
108 :
109 : #endif // !defined(nsQueryObject_h)
|