From 4bb66255ef723ad94b053003705267a9d13c145d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 17 Jun 2009 02:17:27 +0000 Subject: [PATCH] Forbid adding of two functions with the exact same signature --- dispatchkit/bootstrap_stl.hpp | 8 +++++++- dispatchkit/dispatchkit.hpp | 34 +++++++++++++++++++++++++++++---- dispatchkit/proxy_functions.hpp | 18 +++++++++++++++-- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/dispatchkit/bootstrap_stl.hpp b/dispatchkit/bootstrap_stl.hpp index 0874cbf8..e99a70e2 100644 --- a/dispatchkit/bootstrap_stl.hpp +++ b/dispatchkit/bootstrap_stl.hpp @@ -138,6 +138,11 @@ namespace dispatchkit bootstrap_default_constructible(system, type); } + template + void bootstrap_pair_associative_container(Dispatch_Engine &system, const std::string &type) + { + bootstrap_associative_container(system, type); + } template void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type) @@ -164,7 +169,8 @@ namespace dispatchkit system.register_type(type); register_function(system, &MapType::operator[], "[]"); bootstrap_unique_sorted_associative_container(system, type); - } + bootstrap_pair_associative_container(system, type); + } } #endif diff --git a/dispatchkit/dispatchkit.hpp b/dispatchkit/dispatchkit.hpp index f6002930..bc3d0a1a 100644 --- a/dispatchkit/dispatchkit.hpp +++ b/dispatchkit/dispatchkit.hpp @@ -33,17 +33,25 @@ namespace dispatchkit void register_function(const boost::shared_ptr &f, const std::string &name) { - m_functions.insert(std::make_pair(name, f)); - } + if (!add_function(f, name)) + { + std::cout << "Unable to add function: " << name + << " another function with the exact signature exists." << std::endl; + } + } template void register_function(const Function &func, const std::string &name) { - m_functions.insert(std::make_pair(name, boost::shared_ptr(new Proxy_Function_Impl(func)))); + if (!add_function(boost::shared_ptr(new Proxy_Function_Impl(func)), name)) + { + std::cout << "Unable to add function: " << name + << " another function with the exact signature exists." << std::endl; + } } - + template void set_object(const std::string &name, const Class &obj) { @@ -146,6 +154,24 @@ namespace dispatchkit } private: + bool add_function(const boost::shared_ptr &f, const std::string &t_name) + { + std::pair range + = m_functions.equal_range(t_name); + + while (range.first != range.second) + { + if ((*f) == *(range.first->second)) + { + return false; + } + ++range.first; + } + + m_functions.insert(std::make_pair(t_name, f)); + return true; + } + std::deque m_scopes; Function_Map m_functions; diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index 302ad536..ab223110 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -102,7 +102,7 @@ namespace dispatchkit virtual ~Proxy_Function() {} virtual Boxed_Value operator()(const std::vector ¶ms) = 0; virtual std::vector get_param_types() = 0; - + virtual bool operator==(const Proxy_Function &) const = 0; }; class Dynamic_Proxy_Function : public Proxy_Function @@ -113,6 +113,11 @@ namespace dispatchkit { } + bool operator==(const Proxy_Function &f) const + { + return false; + } + virtual ~Dynamic_Proxy_Function() {} virtual Boxed_Value operator()(const std::vector ¶ms) @@ -146,7 +151,16 @@ namespace dispatchkit virtual ~Proxy_Function_Impl() {} - + virtual bool operator==(const Proxy_Function &t_func) const + { + try { + dynamic_cast &>(t_func); + return true; + } catch (const std::bad_cast &) { + return false; + } + } + virtual Boxed_Value operator()(const std::vector ¶ms) { return call_func(m_f, params);