nsIInterfaceRequestorUtils.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef __nsInterfaceRequestorUtils_h
  6. #define __nsInterfaceRequestorUtils_h
  7. #include "nsCOMPtr.h"
  8. // a type-safe shortcut for calling the |GetInterface()| member function
  9. // T must inherit from nsIInterfaceRequestor, but the cast may be ambiguous.
  10. template<class T, class DestinationType>
  11. inline nsresult
  12. CallGetInterface(T* aSource, DestinationType** aDestination)
  13. {
  14. NS_PRECONDITION(aSource, "null parameter");
  15. NS_PRECONDITION(aDestination, "null parameter");
  16. return aSource->GetInterface(NS_GET_TEMPLATE_IID(DestinationType),
  17. reinterpret_cast<void**>(aDestination));
  18. }
  19. class MOZ_STACK_CLASS nsGetInterface final : public nsCOMPtr_helper
  20. {
  21. public:
  22. nsGetInterface(nsISupports* aSource, nsresult* aError)
  23. : mSource(aSource)
  24. , mErrorPtr(aError)
  25. {
  26. }
  27. virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const
  28. override;
  29. private:
  30. nsISupports* MOZ_NON_OWNING_REF mSource;
  31. nsresult* mErrorPtr;
  32. };
  33. inline const nsGetInterface
  34. do_GetInterface(nsISupports* aSource, nsresult* aError = 0)
  35. {
  36. return nsGetInterface(aSource, aError);
  37. }
  38. #endif // __nsInterfaceRequestorUtils_h