Skip to content

Commit

Permalink
Resolve the duplicate registration key issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjian526 committed Jul 24, 2024
1 parent aa02a75 commit b4daa23
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions include/rest_rpc/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,34 @@ class router : asio::noncopyable {
template <bool is_pub = false, typename Function>
void register_handler(std::string const &name, Function f, bool pub = false) {
uint32_t key = MD5::MD5Hash32(name.data());
key2func_name_.emplace(key, name);
return register_nonmember_func<is_pub>(key, std::move(f));
try {
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_nonmember_func<is_pub>(key, std::move(f));
}
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
throw;
}
}

template <bool is_pub = false, typename Function, typename Self>
void register_handler(std::string const &name, const Function &f,
Self *self) {
uint32_t key = MD5::MD5Hash32(name.data());
key2func_name_.emplace(key, name);
return register_member_func<is_pub>(key, f, self);
try {
if (key2func_name_.find(key) != key2func_name_.end()) {
throw std::invalid_argument("duplicate registration key !");
} else {
key2func_name_.emplace(key, name);
return register_member_func<is_pub>(key, f, self);
}
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
throw;
}
}

void remove_handler(std::string const &name) {
Expand Down

0 comments on commit b4daa23

Please sign in to comment.