| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #pragma once
- #include <iostream>
- #include <vector>
- #include <functional>
- #include <map>
- #include <unordered_map>
- #include "cDataStruct.h"
- /***********************************************************************************************************************
- ***********************************************************************************************************************/
- enum class CALLBACK_TYPE { A, B, C, D };
- class cCallbackHdl {
- public:
- cCallbackHdl() {};
- template <class T>
- uint8_t CallBack_register(const CALLBACK_TYPE type, const T& cb)
- {
- // add callback to end of callback list
- callmap.insert(std::make_pair(type, std::move(cb)));
- return 0;
- }
- /// Call all the registered callbacks.
- template <class T>
- uint8_t CallBack_call(const CALLBACK_TYPE type, T& val) const
- {
- if (callmap.find(type) == callmap.end()) {
- // std::cout << "Index not found: " << index << std::endl;
- return 1;
- }
- const auto& cb = callmap.find(type)->second;
- return (cb)(val);
- // return 0;
- }
- template <class T>
- uint8_t CallBack_V_register(const CALLBACK_TYPE type, const T& cb)
- {
- // add callback to end of callback list
- m_call_map.insert(std::make_pair(type, std::move(cb)));
- return 0;
- }
- /// Call all the registered callbacks.
- // template <class T>
- uint8_t CallBack_V_call(const CALLBACK_TYPE type, void* val, void* xal) const
- {
- if (m_call_map.find(type) == m_call_map.end()) {
- // std::cout << "Index not found: " << index << std::endl;
- return 1;
- }
- const auto& cb = m_call_map.find(type)->second;
- return (cb)(val,xal);
- // return 0;
- }
- #if 0
- template <class T>
- uint8_t register_call_2map(const CALLBACK_TYPE type, const T& cb)
- {
- // add callback to end of callback list
- call_map.insert(std::make_pair(type, std::move(cb)));
- return 0;
- }
- /// Call all the registered callbacks.
- template <class T> // , class... Args>
- uint8_t call_call_2map(const CALLBACK_TYPE type, T& val, T& xal) const // Args&&... args
- {
- if (call_map.find(type) == call_map.end()) {
- // std::cout << "Index not found: " << index << std::endl;
- return 1;
- }
- const auto& cb = call_map.find(type)->second;
- return (cb)(val,xal);
- // return 0;
- }
- #endif
- // private:
- // The map that stores the callbacks.
- std::map<CALLBACK_TYPE, std::function<uint8_t(myData&)>> callmap;
- std::map<CALLBACK_TYPE, std::function<uint8_t(myData&, myData&)>> call_map;
- std::map<CALLBACK_TYPE, std::function<uint8_t(void*, void*)>> m_call_map;
- };
|