Bug 1551001 [wpt PR 16740] - Don't mark disconnected tree-scopes for style update...
[gecko.git] / ipc / mscom / ApartmentRegion.h
blobdc56e3fc967695f77c60c2eed3b421254c47ff69
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"
13 #include <objbase.h>
15 namespace mozilla {
16 namespace mscom {
18 class MOZ_NON_TEMPORARY_CLASS ApartmentRegion {
19 public:
20 /**
21 * This constructor is to be used when we want to instantiate the object but
22 * we do not yet know which type of apartment we want. Call Init() to
23 * complete initialization.
25 ApartmentRegion() : mInitResult(CO_E_NOTINITIALIZED) {}
27 explicit ApartmentRegion(COINIT aAptType)
28 : mInitResult(::CoInitializeEx(nullptr, aAptType)) {
29 // If this fires then we're probably mixing apartments on the same thread
30 MOZ_ASSERT(IsValid());
33 ~ApartmentRegion() {
34 if (IsValid()) {
35 ::CoUninitialize();
39 explicit operator bool() const { return IsValid(); }
41 bool IsValidOutermost() const { return mInitResult == S_OK; }
43 bool IsValid() const { return SUCCEEDED(mInitResult); }
45 bool Init(COINIT aAptType) {
46 MOZ_ASSERT(mInitResult == CO_E_NOTINITIALIZED);
47 mInitResult = ::CoInitializeEx(nullptr, aAptType);
48 MOZ_ASSERT(IsValid());
49 return IsValid();
52 HRESULT
53 GetHResult() const { return mInitResult; }
55 private:
56 ApartmentRegion(const ApartmentRegion&) = delete;
57 ApartmentRegion& operator=(const ApartmentRegion&) = delete;
58 ApartmentRegion(ApartmentRegion&&) = delete;
59 ApartmentRegion& operator=(ApartmentRegion&&) = delete;
61 HRESULT mInitResult;
64 template <COINIT T>
65 class MOZ_NON_TEMPORARY_CLASS ApartmentRegionT {
66 public:
67 ApartmentRegionT() : mAptRgn(T) {}
69 ~ApartmentRegionT() = default;
71 explicit operator bool() const { return mAptRgn.IsValid(); }
73 bool IsValidOutermost() const { return mAptRgn.IsValidOutermost(); }
75 bool IsValid() const { return mAptRgn.IsValid(); }
77 HRESULT GetHResult() const { return mAptRgn.GetHResult(); }
79 private:
80 ApartmentRegionT(const ApartmentRegionT&) = delete;
81 ApartmentRegionT& operator=(const ApartmentRegionT&) = delete;
82 ApartmentRegionT(ApartmentRegionT&&) = delete;
83 ApartmentRegionT& operator=(ApartmentRegionT&&) = delete;
85 ApartmentRegion mAptRgn;
88 typedef ApartmentRegionT<COINIT_APARTMENTTHREADED> STARegion;
89 typedef ApartmentRegionT<COINIT_MULTITHREADED> MTARegion;
91 } // namespace mscom
92 } // namespace mozilla
94 #endif // mozilla_mscom_ApartmentRegion_h