3 #include "telnetpp/detail/return_default.hpp"
6 #include <unordered_map>
9 namespace telnetpp::detail {
49 template <
class Key,
class Message,
class Function,
class KeyFromMessagePolicy>
54 using message_type = Message;
55 using function_type = std::function<Function>;
56 using result_type =
typename function_type::result_type;
57 using registered_functions_map_type =
58 std::unordered_map<key_type, function_type>;
66 template <
typename KeyType,
typename Continuation>
67 void register_route(KeyType &&key, Continuation &&cont)
69 registered_functions_[std::forward<KeyType>(key)] =
70 std::forward<Continuation>(cont);
79 template <
typename KeyType>
80 void unregister_route(KeyType &&key)
82 registered_functions_.erase(std::forward<KeyType>(key));
91 template <
typename Continuation>
92 void set_unregistered_route(Continuation &&cont)
94 unregistered_route_ = std::forward<Continuation>(cont);
105 template <
typename MessageType,
typename... Args>
106 result_type operator()(MessageType &&message, Args &&...args)
const
108 if (
auto iter = registered_functions_.find(
109 KeyFromMessagePolicy::key_from_message(message));
110 iter != registered_functions_.end())
113 std::forward<MessageType>(message),
114 std::forward<Args>(args)...);
116 else if (unregistered_route_)
118 return unregistered_route_(
119 std::forward<MessageType>(message),
120 std::forward<Args>(args)...);
125 return detail::return_default_constructed<result_type>{}();
129 registered_functions_map_type registered_functions_;
130 function_type unregistered_route_;