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"
18 class MOZ_NON_TEMPORARY_CLASS ApartmentRegion
{
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());
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());
53 GetHResult() const { return mInitResult
; }
56 ApartmentRegion(const ApartmentRegion
&) = delete;
57 ApartmentRegion
& operator=(const ApartmentRegion
&) = delete;
58 ApartmentRegion(ApartmentRegion
&&) = delete;
59 ApartmentRegion
& operator=(ApartmentRegion
&&) = delete;
65 class MOZ_NON_TEMPORARY_CLASS ApartmentRegionT
{
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(); }
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
;
92 } // namespace mozilla
94 #endif // mozilla_mscom_ApartmentRegion_h