]> git.sesse.net Git - casparcg/blob - common/concurrency/com_context.h
2.0.0.2: Fixed a bug in com_context where the old context was destroyed after the...
[casparcg] / common / concurrency / com_context.h
1 #pragma once\r
2 \r
3 #include "executor.h"\r
4 \r
5 #include <Windows.h>\r
6 \r
7 #include <boost/noncopyable.hpp>\r
8 \r
9 #include <functional>\r
10 \r
11 namespace caspar {\r
12 \r
13 template<typename T>\r
14 class com_context : public executor\r
15 {\r
16         std::unique_ptr<T> instance_;\r
17 public:\r
18         com_context(const std::wstring& name) : executor(name)\r
19         {\r
20                 executor::begin_invoke([]\r
21                 {\r
22                         ::CoInitialize(nullptr);\r
23                 });\r
24         }\r
25 \r
26         ~com_context()\r
27         {\r
28                 executor::invoke([&]\r
29                 {\r
30                         instance_.reset(nullptr);\r
31                         ::CoUninitialize();\r
32                 });\r
33         }\r
34         \r
35         void reset(const std::function<T*()>& factory = nullptr)\r
36         {\r
37                 executor::invoke([&]\r
38                 {\r
39                         instance_.reset();\r
40                         if(factory)\r
41                                 instance_.reset(factory());\r
42                 });\r
43         }\r
44 \r
45         T& operator*() const { return *instance_.get();}  // noexcept\r
46 \r
47         T* operator->() const { return instance_.get();}  // noexcept\r
48 \r
49         T* get() const { return instance_.get();}  // noexcept\r
50 \r
51         operator bool() const {return get() != nullptr;}\r
52 };\r
53 \r
54 }