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/. */ 6 7 #ifndef mozilla_mscom_COMApartmentRegion_h 8 #define mozilla_mscom_COMApartmentRegion_h 9 10 #include "mozilla/Assertions.h" 11 #include "mozilla/Attributes.h" 12 13 #include <objbase.h> 14 15 namespace mozilla { 16 namespace mscom { 17 18 template<COINIT T> 19 class MOZ_NON_TEMPORARY_CLASS COMApartmentRegion 20 { 21 public: COMApartmentRegion()22 COMApartmentRegion() 23 : mInitResult(::CoInitializeEx(nullptr, T)) 24 { 25 // If this fires then we're probably mixing apartments on the same thread 26 MOZ_ASSERT(IsValid()); 27 } 28 ~COMApartmentRegion()29 ~COMApartmentRegion() 30 { 31 if (IsValid()) { 32 ::CoUninitialize(); 33 } 34 } 35 IsValidOutermost()36 bool IsValidOutermost() const 37 { 38 return mInitResult == S_OK; 39 } 40 IsValid()41 bool IsValid() const 42 { 43 return SUCCEEDED(mInitResult); 44 } 45 46 private: 47 COMApartmentRegion(const COMApartmentRegion&) = delete; 48 COMApartmentRegion& operator=(const COMApartmentRegion&) = delete; 49 COMApartmentRegion(COMApartmentRegion&&) = delete; 50 COMApartmentRegion& operator=(COMApartmentRegion&&) = delete; 51 52 HRESULT mInitResult; 53 }; 54 55 typedef COMApartmentRegion<COINIT_APARTMENTTHREADED> STARegion; 56 typedef COMApartmentRegion<COINIT_MULTITHREADED> MTARegion; 57 58 } // namespace mscom 59 } // namespace mozilla 60 61 #endif // mozilla_mscom_COMApartmentRegion_h 62 63