3 #include "executor.h"
\r
5 #include "../log/log.h"
\r
6 #include "../exception/exceptions.h"
\r
9 #define WIN32_LEAN_AND_MEAN
\r
11 #include <Windows.h>
\r
13 #include <boost/noncopyable.hpp>
\r
14 #include <boost/thread/future.hpp>
\r
16 #include <functional>
\r
20 template<typename T>
\r
21 class com_context : public executor
\r
23 std::unique_ptr<T> instance_;
\r
25 com_context(const std::wstring& name) : executor(name)
\r
27 executor::begin_invoke([]
\r
29 ::CoInitialize(nullptr);
\r
35 if(!executor::begin_invoke([&]
\r
37 instance_.reset(nullptr);
\r
39 }).timed_wait(boost::posix_time::milliseconds(500)))
\r
41 CASPAR_LOG(error) << L"[com_contex] Timer expired, deadlock detected and released, leaking resources.";
\r
45 void reset(const std::function<T*()>& factory = nullptr)
\r
47 executor::invoke([&]
\r
51 instance_.reset(factory());
\r
55 T& operator*() const
\r
57 if(instance_ == nullptr)
\r
58 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Tried to access null context."));
\r
60 return *instance_.get();
\r
63 T* operator->() const
\r
65 if(instance_ == nullptr)
\r
66 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Tried to access null context."));
\r
67 return instance_.get();
\r
72 return instance_.get();
\r
75 operator bool() const {return get() != nullptr;}
\r