cCallbackHdl.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <functional>
  5. #include <map>
  6. #include <unordered_map>
  7. #include "cDataStruct.h"
  8. /***********************************************************************************************************************
  9. ***********************************************************************************************************************/
  10. enum class CALLBACK_TYPE { A, B, C, D };
  11. class cCallbackHdl {
  12. public:
  13. cCallbackHdl() {};
  14. template <class T>
  15. uint8_t CallBack_register(const CALLBACK_TYPE type, const T& cb)
  16. {
  17. // add callback to end of callback list
  18. callmap.insert(std::make_pair(type, std::move(cb)));
  19. return 0;
  20. }
  21. /// Call all the registered callbacks.
  22. template <class T>
  23. uint8_t CallBack_call(const CALLBACK_TYPE type, T& val) const
  24. {
  25. if (callmap.find(type) == callmap.end()) {
  26. // std::cout << "Index not found: " << index << std::endl;
  27. return 1;
  28. }
  29. const auto& cb = callmap.find(type)->second;
  30. return (cb)(val);
  31. // return 0;
  32. }
  33. template <class T>
  34. uint8_t CallBack_V_register(const CALLBACK_TYPE type, const T& cb)
  35. {
  36. // add callback to end of callback list
  37. m_call_map.insert(std::make_pair(type, std::move(cb)));
  38. return 0;
  39. }
  40. /// Call all the registered callbacks.
  41. // template <class T>
  42. uint8_t CallBack_V_call(const CALLBACK_TYPE type, void* val, void* xal) const
  43. {
  44. if (m_call_map.find(type) == m_call_map.end()) {
  45. // std::cout << "Index not found: " << index << std::endl;
  46. return 1;
  47. }
  48. const auto& cb = m_call_map.find(type)->second;
  49. return (cb)(val,xal);
  50. // return 0;
  51. }
  52. #if 0
  53. template <class T>
  54. uint8_t register_call_2map(const CALLBACK_TYPE type, const T& cb)
  55. {
  56. // add callback to end of callback list
  57. call_map.insert(std::make_pair(type, std::move(cb)));
  58. return 0;
  59. }
  60. /// Call all the registered callbacks.
  61. template <class T> // , class... Args>
  62. uint8_t call_call_2map(const CALLBACK_TYPE type, T& val, T& xal) const // Args&&... args
  63. {
  64. if (call_map.find(type) == call_map.end()) {
  65. // std::cout << "Index not found: " << index << std::endl;
  66. return 1;
  67. }
  68. const auto& cb = call_map.find(type)->second;
  69. return (cb)(val,xal);
  70. // return 0;
  71. }
  72. #endif
  73. // private:
  74. // The map that stores the callbacks.
  75. std::map<CALLBACK_TYPE, std::function<uint8_t(myData&)>> callmap;
  76. std::map<CALLBACK_TYPE, std::function<uint8_t(myData&, myData&)>> call_map;
  77. std::map<CALLBACK_TYPE, std::function<uint8_t(void*, void*)>> m_call_map;
  78. };