Bug 1857669 - Install libavcodec/libavutil for Selenium tests r=releng-reviewers...
[gecko.git] / ipc / mscom / ApartmentRegion.h
blob41915710df8a02c2965c7df38108f49683ee652e
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_ApartmentRegion_h
8 #define mozilla_mscom_ApartmentRegion_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/mscom/COMWrappers.h"
14 namespace mozilla {
15 namespace mscom {
17 class MOZ_NON_TEMPORARY_CLASS ApartmentRegion final {
18 public:
19 /**
20 * This constructor is to be used when we want to instantiate the object but
21 * we do not yet know which type of apartment we want. Call Init() to
22 * complete initialization.
24 constexpr ApartmentRegion() : mInitResult(CO_E_NOTINITIALIZED) {}
26 explicit ApartmentRegion(COINIT aAptType)
27 : mInitResult(wrapped::CoInitializeEx(nullptr, aAptType)) {
28 // If this fires then we're probably mixing apartments on the same thread
29 MOZ_ASSERT(IsValid());
32 ~ApartmentRegion() {
33 if (IsValid()) {
34 wrapped::CoUninitialize();
38 explicit operator bool() const { return IsValid(); }
40 bool IsValidOutermost() const { return mInitResult == S_OK; }
42 bool IsValid() const { return SUCCEEDED(mInitResult); }
44 bool Init(COINIT aAptType) {
45 MOZ_ASSERT(mInitResult == CO_E_NOTINITIALIZED);
46 mInitResult = wrapped::CoInitializeEx(nullptr, aAptType);
47 MOZ_ASSERT(IsValid());
48 return IsValid();
51 HRESULT
52 GetHResult() const { return mInitResult; }
54 private:
55 ApartmentRegion(const ApartmentRegion&) = delete;
56 ApartmentRegion& operator=(const ApartmentRegion&) = delete;
57 ApartmentRegion(ApartmentRegion&&) = delete;
58 ApartmentRegion& operator=(ApartmentRegion&&) = delete;
60 HRESULT mInitResult;
63 template <COINIT T>
64 class MOZ_NON_TEMPORARY_CLASS ApartmentRegionT final {
65 public:
66 ApartmentRegionT() : mAptRgn(T) {}
68 ~ApartmentRegionT() = default;
70 explicit operator bool() const { return mAptRgn.IsValid(); }
72 bool IsValidOutermost() const { return mAptRgn.IsValidOutermost(); }
74 bool IsValid() const { return mAptRgn.IsValid(); }
76 HRESULT GetHResult() const { return mAptRgn.GetHResult(); }
78 private:
79 ApartmentRegionT(const ApartmentRegionT&) = delete;
80 ApartmentRegionT& operator=(const ApartmentRegionT&) = delete;
81 ApartmentRegionT(ApartmentRegionT&&) = delete;
82 ApartmentRegionT& operator=(ApartmentRegionT&&) = delete;
84 ApartmentRegion mAptRgn;
87 typedef ApartmentRegionT<COINIT_APARTMENTTHREADED> STARegion;
88 typedef ApartmentRegionT<COINIT_MULTITHREADED> MTARegion;
90 } // namespace mscom
91 } // namespace mozilla
93 #endif // mozilla_mscom_ApartmentRegion_h