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 mozilla_mscom_Utils_h
8 #define mozilla_mscom_Utils_h
10 #if defined(MOZILLA_INTERNAL_API)
11 # include "nsString.h"
12 #endif // defined(MOZILLA_INTERNAL_API)
14 #include "mozilla/Attributes.h"
30 long BuildRegGuidPath(REFGUID aGuid
, const GuidType aGuidType
, wchar_t* aBuf
,
31 const size_t aBufLen
);
35 bool IsCOMInitializedOnCurrentThread();
36 bool IsCurrentThreadMTA();
37 bool IsCurrentThreadExplicitMTA();
38 bool IsCurrentThreadImplicitMTA();
39 #if defined(MOZILLA_INTERNAL_API)
40 bool IsCurrentThreadNonMainMTA();
41 #endif // defined(MOZILLA_INTERNAL_API)
42 bool IsProxy(IUnknown
* aUnknown
);
43 bool IsValidGUID(REFGUID aCheckGuid
);
44 uintptr_t GetContainingModuleHandle();
47 inline long BuildAppidPath(REFGUID aAppId
, wchar_t (&aPath
)[N
]) {
48 return detail::BuildRegGuidPath(aAppId
, detail::GuidType::AppID
, aPath
, N
);
52 inline long BuildClsidPath(REFCLSID aClsid
, wchar_t (&aPath
)[N
]) {
53 return detail::BuildRegGuidPath(aClsid
, detail::GuidType::CLSID
, aPath
, N
);
57 * Given a buffer, create a new IStream object.
58 * @param aBuf Buffer containing data to initialize the stream. This parameter
59 * may be nullptr, causing the stream to be created with aBufLen
60 * bytes of uninitialized data.
61 * @param aBufLen Length of data in aBuf, or desired stream size if aBuf is
63 * @param aOutStream Outparam to receive the newly created stream.
64 * @return HRESULT error code.
66 long CreateStream(const uint8_t* aBuf
, const uint32_t aBufLen
,
67 IStream
** aOutStream
);
70 * Length of a stringified GUID as formatted for the registry, i.e. including
71 * curly-braces and dashes.
73 constexpr size_t kGuidRegFormatCharLenInclNul
= 39;
75 #if defined(MOZILLA_INTERNAL_API)
76 void GUIDToString(REFGUID aGuid
, nsAString
& aOutString
);
79 * Converts an IID to a human-readable string for the purposes of diagnostic
80 * tools such as the profiler. For some special cases, we output a friendly
81 * string that describes the purpose of the interface. If no such description
82 * exists, we simply fall back to outputting the IID as a string formatted by
85 void DiagnosticNameForIID(REFIID aIid
, nsACString
& aOutString
);
87 void GUIDToString(REFGUID aGuid
,
88 wchar_t (&aOutBuf
)[kGuidRegFormatCharLenInclNul
]);
89 #endif // defined(MOZILLA_INTERNAL_API)
92 * Execute cleanup code when going out of scope if a condition is met.
93 * This is useful when, for example, particular cleanup needs to be performed
94 * whenever a call returns a failure HRESULT.
95 * Both the condition and cleanup code are provided as functions (usually
98 template <typename CondFnT
, typename ExeFnT
>
99 class MOZ_RAII ExecuteWhen final
{
101 ExecuteWhen(CondFnT
& aCondFn
, ExeFnT
& aExeFn
)
102 : mCondFn(aCondFn
), mExeFn(aExeFn
) {}
110 ExecuteWhen(const ExecuteWhen
&) = delete;
111 ExecuteWhen(ExecuteWhen
&&) = delete;
112 ExecuteWhen
& operator=(const ExecuteWhen
&) = delete;
113 ExecuteWhen
& operator=(ExecuteWhen
&&) = delete;
121 } // namespace mozilla
123 #endif // mozilla_mscom_Utils_h