sigslot.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // sigslot.h: Signal/Slot classes
  2. // Written by Sarah Thompson (sarah@telergy.com) 2002
  3. // more info : http://sigslot.sourceforge.net/
  4. // ** only reserve no paramete function for CSerialPort 4.0 2019-07-21 itas109 **
  5. #ifndef SIGSLOT_H__
  6. #define SIGSLOT_H__
  7. #include <set>
  8. #include <list>
  9. #if defined(SIGSLOT_PURE_ISO) || (!defined(_WIN32) && !defined(__GNUG__) && !defined(SIGSLOT_USE_POSIX_THREADS))
  10. # define _SIGSLOT_SINGLE_THREADED
  11. #elif defined(_WIN32)
  12. # define _SIGSLOT_HAS_WIN32_THREADS
  13. # include <windows.h>
  14. #elif defined(__GNUG__) || defined(SIGSLOT_USE_POSIX_THREADS)
  15. # define _SIGSLOT_HAS_POSIX_THREADS
  16. # include <pthread.h>
  17. #else
  18. # define _SIGSLOT_SINGLE_THREADED
  19. #endif
  20. #ifndef SIGSLOT_DEFAULT_MT_POLICY
  21. # ifdef _SIGSLOT_SINGLE_THREADED
  22. # define SIGSLOT_DEFAULT_MT_POLICY single_threaded
  23. # else
  24. # define SIGSLOT_DEFAULT_MT_POLICY multi_threaded_local
  25. # endif
  26. #endif
  27. namespace sigslot {
  28. class single_threaded
  29. {
  30. public:
  31. single_threaded()
  32. {
  33. ;
  34. }
  35. virtual ~single_threaded()
  36. {
  37. ;
  38. }
  39. virtual void lock()
  40. {
  41. ;
  42. }
  43. virtual void unlock()
  44. {
  45. ;
  46. }
  47. };
  48. #ifdef _SIGSLOT_HAS_WIN32_THREADS
  49. // The multi threading policies only get compiled in if they are enabled.
  50. class multi_threaded_global
  51. {
  52. public:
  53. multi_threaded_global()
  54. {
  55. static bool isinitialised = false;
  56. if(!isinitialised)
  57. {
  58. InitializeCriticalSection(get_critsec());
  59. isinitialised = true;
  60. }
  61. }
  62. multi_threaded_global(const multi_threaded_global&)
  63. {
  64. ;
  65. }
  66. virtual ~multi_threaded_global()
  67. {
  68. ;
  69. }
  70. virtual void lock()
  71. {
  72. EnterCriticalSection(get_critsec());
  73. }
  74. virtual void unlock()
  75. {
  76. LeaveCriticalSection(get_critsec());
  77. }
  78. private:
  79. CRITICAL_SECTION* get_critsec()
  80. {
  81. static CRITICAL_SECTION g_critsec;
  82. return &g_critsec;
  83. }
  84. };
  85. class multi_threaded_local
  86. {
  87. public:
  88. multi_threaded_local()
  89. {
  90. InitializeCriticalSection(&m_critsec);
  91. }
  92. multi_threaded_local(const multi_threaded_local&)
  93. {
  94. InitializeCriticalSection(&m_critsec);
  95. }
  96. virtual ~multi_threaded_local()
  97. {
  98. DeleteCriticalSection(&m_critsec);
  99. }
  100. virtual void lock()
  101. {
  102. EnterCriticalSection(&m_critsec);
  103. }
  104. virtual void unlock()
  105. {
  106. LeaveCriticalSection(&m_critsec);
  107. }
  108. private:
  109. CRITICAL_SECTION m_critsec;
  110. };
  111. #endif // _SIGSLOT_HAS_WIN32_THREADS
  112. #ifdef _SIGSLOT_HAS_POSIX_THREADS
  113. // The multi threading policies only get compiled in if they are enabled.
  114. class multi_threaded_global
  115. {
  116. public:
  117. multi_threaded_global()
  118. {
  119. pthread_mutex_init(get_mutex(), NULL);
  120. }
  121. multi_threaded_global(const multi_threaded_global&)
  122. {
  123. ;
  124. }
  125. virtual ~multi_threaded_global()
  126. {
  127. ;
  128. }
  129. virtual void lock()
  130. {
  131. pthread_mutex_lock(get_mutex());
  132. }
  133. virtual void unlock()
  134. {
  135. pthread_mutex_unlock(get_mutex());
  136. }
  137. private:
  138. pthread_mutex_t* get_mutex()
  139. {
  140. static pthread_mutex_t g_mutex;
  141. return &g_mutex;
  142. }
  143. };
  144. class multi_threaded_local
  145. {
  146. public:
  147. multi_threaded_local()
  148. {
  149. pthread_mutex_init(&m_mutex, NULL);
  150. }
  151. multi_threaded_local(const multi_threaded_local&)
  152. {
  153. pthread_mutex_init(&m_mutex, NULL);
  154. }
  155. virtual ~multi_threaded_local()
  156. {
  157. pthread_mutex_destroy(&m_mutex);
  158. }
  159. virtual void lock()
  160. {
  161. pthread_mutex_lock(&m_mutex);
  162. }
  163. virtual void unlock()
  164. {
  165. pthread_mutex_unlock(&m_mutex);
  166. }
  167. private:
  168. pthread_mutex_t m_mutex;
  169. };
  170. #endif // _SIGSLOT_HAS_POSIX_THREADS
  171. template<class mt_policy>
  172. class lock_block
  173. {
  174. public:
  175. mt_policy *m_mutex;
  176. lock_block(mt_policy *mtx)
  177. : m_mutex(mtx)
  178. {
  179. m_mutex->lock();
  180. }
  181. ~lock_block()
  182. {
  183. m_mutex->unlock();
  184. }
  185. };
  186. template<class mt_policy>
  187. class has_slots;
  188. template<class mt_policy>
  189. class _connection_base0
  190. {
  191. public:
  192. virtual has_slots<mt_policy>* getdest() const = 0;
  193. virtual void _emit() = 0;
  194. virtual _connection_base0* clone() = 0;
  195. virtual _connection_base0* duplicate(has_slots<mt_policy>* pnewdest) = 0;
  196. };
  197. template<class mt_policy>
  198. class _signal_base : public mt_policy
  199. {
  200. public:
  201. virtual void slot_disconnect(has_slots<mt_policy>* pslot) = 0;
  202. virtual void slot_duplicate(const has_slots<mt_policy>* poldslot, has_slots<mt_policy>* pnewslot) = 0;
  203. };
  204. template<class mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
  205. class has_slots : public mt_policy
  206. {
  207. private:
  208. typedef typename std::set<_signal_base<mt_policy> *> sender_set;
  209. typedef typename sender_set::const_iterator const_iterator;
  210. public:
  211. has_slots()
  212. {
  213. ;
  214. }
  215. has_slots(const has_slots& hs)
  216. : mt_policy(hs)
  217. {
  218. lock_block<mt_policy> lock(this);
  219. const_iterator it = hs.m_senders.begin();
  220. const_iterator itEnd = hs.m_senders.end();
  221. while(it != itEnd)
  222. {
  223. (*it)->slot_duplicate(&hs, this);
  224. m_senders.insert(*it);
  225. ++it;
  226. }
  227. }
  228. void signal_connect(_signal_base<mt_policy>* sender)
  229. {
  230. lock_block<mt_policy> lock(this);
  231. m_senders.insert(sender);
  232. }
  233. void signal_disconnect(_signal_base<mt_policy>* sender)
  234. {
  235. lock_block<mt_policy> lock(this);
  236. m_senders.erase(sender);
  237. }
  238. virtual ~has_slots()
  239. {
  240. disconnect_all();
  241. }
  242. void disconnect_all()
  243. {
  244. lock_block<mt_policy> lock(this);
  245. const_iterator it = m_senders.begin();
  246. const_iterator itEnd = m_senders.end();
  247. while(it != itEnd)
  248. {
  249. (*it)->slot_disconnect(this);
  250. ++it;
  251. }
  252. m_senders.erase(m_senders.begin(), m_senders.end());
  253. }
  254. private:
  255. sender_set m_senders;
  256. };
  257. template<class mt_policy>
  258. class _signal_base0 : public _signal_base<mt_policy>
  259. {
  260. public:
  261. typedef typename std::list<_connection_base0<mt_policy> *> connections_list;
  262. typedef typename connections_list::const_iterator const_iterator;
  263. typedef typename connections_list::iterator iterator;
  264. _signal_base0()
  265. {
  266. ;
  267. }
  268. _signal_base0(const _signal_base0& s)
  269. : _signal_base<mt_policy>(s)
  270. {
  271. lock_block<mt_policy> lock(this);
  272. const_iterator it = s.m_connected_slots.begin();
  273. const_iterator itEnd = s.m_connected_slots.end();
  274. while(it != itEnd)
  275. {
  276. (*it)->getdest()->signal_connect(this);
  277. m_connected_slots.push_back((*it)->clone());
  278. ++it;
  279. }
  280. }
  281. ~_signal_base0()
  282. {
  283. disconnect_all();
  284. }
  285. void disconnect_all()
  286. {
  287. lock_block<mt_policy> lock(this);
  288. const_iterator it = m_connected_slots.begin();
  289. const_iterator itEnd = m_connected_slots.end();
  290. while(it != itEnd)
  291. {
  292. (*it)->getdest()->signal_disconnect(this);
  293. delete *it;
  294. ++it;
  295. }
  296. m_connected_slots.erase(m_connected_slots.begin(), m_connected_slots.end());
  297. }
  298. void disconnect(has_slots<mt_policy>* pclass)
  299. {
  300. lock_block<mt_policy> lock(this);
  301. iterator it = m_connected_slots.begin();
  302. iterator itEnd = m_connected_slots.end();
  303. while(it != itEnd)
  304. {
  305. if((*it)->getdest() == pclass)
  306. {
  307. delete *it;
  308. m_connected_slots.erase(it);
  309. pclass->signal_disconnect(this);
  310. return;
  311. }
  312. ++it;
  313. }
  314. }
  315. void slot_disconnect(has_slots<mt_policy>* pslot)
  316. {
  317. lock_block<mt_policy> lock(this);
  318. iterator it = m_connected_slots.begin();
  319. iterator itEnd = m_connected_slots.end();
  320. while(it != itEnd)
  321. {
  322. iterator itNext = it;
  323. ++itNext;
  324. if((*it)->getdest() == pslot)
  325. {
  326. m_connected_slots.erase(it);
  327. // delete *it;
  328. }
  329. it = itNext;
  330. }
  331. }
  332. void slot_duplicate(const has_slots<mt_policy>* oldtarget, has_slots<mt_policy>* newtarget)
  333. {
  334. lock_block<mt_policy> lock(this);
  335. iterator it = m_connected_slots.begin();
  336. iterator itEnd = m_connected_slots.end();
  337. while(it != itEnd)
  338. {
  339. if((*it)->getdest() == oldtarget)
  340. {
  341. m_connected_slots.push_back((*it)->duplicate(newtarget));
  342. }
  343. ++it;
  344. }
  345. }
  346. protected:
  347. connections_list m_connected_slots;
  348. };
  349. template<class dest_type, class mt_policy>
  350. class _connection0 : public _connection_base0<mt_policy>
  351. {
  352. public:
  353. _connection0()
  354. {
  355. this->pobject = NULL;
  356. this->pmemfun = NULL;
  357. }
  358. _connection0(dest_type* pobject, void (dest_type::*pmemfun)())
  359. {
  360. m_pobject = pobject;
  361. m_pmemfun = pmemfun;
  362. }
  363. virtual _connection_base0<mt_policy>* clone()
  364. {
  365. return new _connection0<dest_type, mt_policy>(*this);
  366. }
  367. virtual _connection_base0<mt_policy>* duplicate(has_slots<mt_policy>* pnewdest)
  368. {
  369. return new _connection0<dest_type, mt_policy>((dest_type *)pnewdest, m_pmemfun);
  370. }
  371. virtual void _emit()
  372. {
  373. (m_pobject->*m_pmemfun)();
  374. }
  375. virtual has_slots<mt_policy>* getdest() const
  376. {
  377. return m_pobject;
  378. }
  379. private:
  380. dest_type* m_pobject;
  381. void (dest_type::* m_pmemfun)();
  382. };
  383. template<class mt_policy = SIGSLOT_DEFAULT_MT_POLICY>
  384. class signal0 : public _signal_base0<mt_policy>
  385. {
  386. public:
  387. typedef typename _signal_base0<mt_policy>::connections_list::const_iterator const_iterator;
  388. signal0()
  389. {
  390. ;
  391. }
  392. signal0(const signal0<mt_policy>& s)
  393. : _signal_base0<mt_policy>(s)
  394. {
  395. ;
  396. }
  397. template<class desttype>
  398. void connect(desttype* pclass, void (desttype::*pmemfun)())
  399. {
  400. lock_block<mt_policy> lock(this);
  401. _connection0<desttype, mt_policy>* conn =
  402. new _connection0<desttype, mt_policy>(pclass, pmemfun);
  403. this->m_connected_slots.push_back(conn);
  404. pclass->signal_connect(this);
  405. }
  406. void _emit()
  407. {
  408. lock_block<mt_policy> lock(this);
  409. const_iterator itNext, it = this->m_connected_slots.begin();
  410. const_iterator itEnd = this->m_connected_slots.end();
  411. while(it != itEnd)
  412. {
  413. itNext = it;
  414. ++itNext;
  415. (*it)->_emit();
  416. it = itNext;
  417. }
  418. }
  419. void operator()()
  420. {
  421. lock_block<mt_policy> lock(this);
  422. const_iterator itNext, it = this->m_connected_slots.begin();
  423. const_iterator itEnd = this->m_connected_slots.end();
  424. while(it != itEnd)
  425. {
  426. itNext = it;
  427. ++itNext;
  428. (*it)->_emit();
  429. it = itNext;
  430. }
  431. }
  432. };
  433. }; // namespace sigslot
  434. #endif // SIGSLOT_H__