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 __nsInterfaceRequestorUtils_h
8 #define __nsInterfaceRequestorUtils_h
9 
10 #include "nsCOMPtr.h"
11 
12 // a type-safe shortcut for calling the |GetInterface()| member function
13 // T must inherit from nsIInterfaceRequestor, but the cast may be ambiguous.
14 template <class T, class DestinationType>
CallGetInterface(T * aSource,DestinationType ** aDestination)15 inline nsresult CallGetInterface(T* aSource, DestinationType** aDestination) {
16   MOZ_ASSERT(aSource, "null parameter");
17   MOZ_ASSERT(aDestination, "null parameter");
18 
19   return aSource->GetInterface(NS_GET_TEMPLATE_IID(DestinationType),
20                                reinterpret_cast<void**>(aDestination));
21 }
22 
23 class MOZ_STACK_CLASS nsGetInterface final : public nsCOMPtr_helper {
24  public:
nsGetInterface(nsISupports * aSource,nsresult * aError)25   nsGetInterface(nsISupports* aSource, nsresult* aError)
26       : mSource(aSource), mErrorPtr(aError) {}
27 
28   virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
29 
30  private:
31   nsISupports* MOZ_NON_OWNING_REF mSource;
32   nsresult* mErrorPtr;
33 };
34 
35 inline const nsGetInterface do_GetInterface(nsISupports* aSource,
36                                             nsresult* aError = 0) {
37   return nsGetInterface(aSource, aError);
38 }
39 
40 #endif  // __nsInterfaceRequestorUtils_h
41