Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / base / nsQueryObject.h
blob7cfbe954ce97f1ef4ee4c68f3b3958f8e6ee2a8c
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/. */
7 #ifndef nsQueryObject_h
8 #define nsQueryObject_h
10 #include "mozilla/Attributes.h"
12 #include "nsCOMPtr.h"
13 #include "mozilla/RefPtr.h"
15 /*****************************************************************************/
17 template <class T>
18 class MOZ_STACK_CLASS nsQueryObject final : public nsCOMPtr_helper {
19 public:
20 explicit nsQueryObject(T* aRawPtr) : mRawPtr(aRawPtr) {}
22 virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
23 void** aResult) const override {
24 nsresult status = mRawPtr ? mRawPtr->QueryInterface(aIID, aResult)
25 : NS_ERROR_NULL_POINTER;
26 return status;
29 private:
30 T* MOZ_NON_OWNING_REF mRawPtr;
33 template <class T>
34 class MOZ_STACK_CLASS nsQueryObjectWithError final : public nsCOMPtr_helper {
35 public:
36 nsQueryObjectWithError(T* aRawPtr, nsresult* aErrorPtr)
37 : mRawPtr(aRawPtr), mErrorPtr(aErrorPtr) {}
39 virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
40 void** aResult) const override {
41 nsresult status = mRawPtr ? mRawPtr->QueryInterface(aIID, aResult)
42 : NS_ERROR_NULL_POINTER;
43 if (mErrorPtr) {
44 *mErrorPtr = status;
46 return status;
49 private:
50 T* MOZ_NON_OWNING_REF mRawPtr;
51 nsresult* mErrorPtr;
54 /*****************************************************************************/
56 /*****************************************************************************/
58 template <class T>
59 inline nsQueryObject<T> do_QueryObject(T* aRawPtr) {
60 return nsQueryObject<T>(aRawPtr);
63 template <class T>
64 inline nsQueryObject<T> do_QueryObject(const nsCOMPtr<T>& aRawPtr) {
65 return nsQueryObject<T>(aRawPtr);
68 template <class T>
69 inline nsQueryObject<T> do_QueryObject(const RefPtr<T>& aRawPtr) {
70 return nsQueryObject<T>(aRawPtr);
73 template <class T>
74 inline nsQueryObjectWithError<T> do_QueryObject(T* aRawPtr,
75 nsresult* aErrorPtr) {
76 return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
79 template <class T>
80 inline nsQueryObjectWithError<T> do_QueryObject(const nsCOMPtr<T>& aRawPtr,
81 nsresult* aErrorPtr) {
82 return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
85 template <class T>
86 inline nsQueryObjectWithError<T> do_QueryObject(const RefPtr<T>& aRawPtr,
87 nsresult* aErrorPtr) {
88 return nsQueryObjectWithError<T>(aRawPtr, aErrorPtr);
91 /*****************************************************************************/
93 #endif // !defined(nsQueryObject_h)