00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 #pragma once 00012 00013 #include <opc/common/interface.h> 00014 #include <opc/common/class_pointers.h> 00015 00016 #include <thread> 00017 #include <stdexcept> 00018 00019 namespace Common 00020 { 00021 00022 class ThreadObserver : private Common::Interface 00023 { 00024 public: 00026 virtual void OnSuccess() = 0; 00028 virtual void OnError(const std::exception& exc) = 0; 00029 }; 00030 00031 typedef std::function<void()> ThreadProc; 00032 00033 class Thread 00034 { 00035 public: 00036 DEFINE_CLASS_POINTERS(Thread); 00037 00038 public: 00040 Thread(std::function<void()> f, ThreadObserver* observer = 0); 00041 00042 static Thread::UniquePtr Create(ThreadProc f, ThreadObserver* observer = 0) 00043 { 00044 return Thread::UniquePtr(new Thread(f, observer)); 00045 } 00046 00047 static Thread::UniquePtr Create(void (*f)(), ThreadObserver* observer = 0) 00048 { 00049 Common::ThreadProc proc(f); 00050 return Thread::UniquePtr(new Common::Thread(proc, observer)); 00051 } 00052 00053 ~Thread(); 00055 void Join(); 00056 00057 public: 00059 void Run(); 00061 static void ThreadProc(Thread* thread); 00062 00063 private: 00064 ThreadObserver* Observer; 00065 Common::ThreadProc Func; 00066 std::thread Impl; 00067 }; 00068 00069 } // namespace Common 00070