Cbeam
Loading...
Searching...
No Matches
com_initializer.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#ifdef _WIN32
30
31 #include <roapi.h>
32 #pragma comment(lib, "runtimeobject.lib")
33#endif
34
35namespace cbeam::platform
36{
42 {
43 public:
49 com_initializer(const bool multi_threaded, const bool do_throw = true)
50 {
51#ifdef _WIN32
52 HRESULT hr = Windows::Foundation::Initialize(multi_threaded ? RO_INIT_MULTITHREADED : RO_INIT_SINGLETHREADED);
53 if (FAILED(hr))
54 {
55 const std::string errorMessage{"acrion::acrion::platform::com_initializer: Could not initialize COM"};
56 CBEAM_LOG(errorMessage);
57 if (do_throw)
58 {
59 throw cbeam::error::runtime_error(errorMessage);
60 }
61 }
62
63 _deinitialize_on_destruction = hr == S_OK; // S_FALSE means COM was already initialized
64#endif
65 }
66
69 {
70#ifdef _WIN32
72 {
73 Windows::Foundation::Uninitialize();
74 }
75#endif
76 }
77
84
85 // Prevent copy and assignment.
88
89 protected:
91 };
92}
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
com_initializer & operator=(const com_initializer &)=delete
bool should_deinitialize_on_destruction() const
Checks if COM will be deinitialized upon destruction of this object.
Definition com_initializer.hpp:80
com_initializer(const com_initializer &)=delete
bool _deinitialize_on_destruction
Definition com_initializer.hpp:90
~com_initializer() noexcept
Destructor: Deinitializes COM in case the constructor initialized it. Does nothing if COM was already...
Definition com_initializer.hpp:68
com_initializer(const bool multi_threaded, const bool do_throw=true)
Constructor: If we are on Windows and COM is not initialized, initialize it in the specified mode.
Definition com_initializer.hpp:49
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Groups platform-specific helpers for Windows, Linux, and macOS. These utilities detect CPU architectu...
Definition clock_precision.hpp:42