Forbid adding of two functions with the exact same signature

This commit is contained in:
Jason Turner
2009-06-17 02:17:27 +00:00
parent e231cb4cf7
commit 4bb66255ef
3 changed files with 53 additions and 7 deletions

View File

@@ -138,6 +138,11 @@ namespace dispatchkit
bootstrap_default_constructible<ContainerType>(system, type); bootstrap_default_constructible<ContainerType>(system, type);
} }
template<typename ContainerType>
void bootstrap_pair_associative_container(Dispatch_Engine &system, const std::string &type)
{
bootstrap_associative_container<ContainerType>(system, type);
}
template<typename ContainerType> template<typename ContainerType>
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type) void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
@@ -164,6 +169,7 @@ namespace dispatchkit
system.register_type<MapType>(type); system.register_type<MapType>(type);
register_function(system, &MapType::operator[], "[]"); register_function(system, &MapType::operator[], "[]");
bootstrap_unique_sorted_associative_container<MapType>(system, type); bootstrap_unique_sorted_associative_container<MapType>(system, type);
bootstrap_pair_associative_container<MapType>(system, type);
} }
} }

View File

@@ -33,14 +33,22 @@ namespace dispatchkit
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name) void register_function(const boost::shared_ptr<Proxy_Function> &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<typename Function> template<typename Function>
void register_function(const Function &func, const std::string &name) void register_function(const Function &func, const std::string &name)
{ {
m_functions.insert(std::make_pair(name, boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)))); if (!add_function(boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)), name))
{
std::cout << "Unable to add function: " << name
<< " another function with the exact signature exists." << std::endl;
}
} }
@@ -146,6 +154,24 @@ namespace dispatchkit
} }
private: private:
bool add_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &t_name)
{
std::pair<Function_Map::const_iterator, Function_Map::const_iterator> 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<Scope> m_scopes; std::deque<Scope> m_scopes;
Function_Map m_functions; Function_Map m_functions;

View File

@@ -102,7 +102,7 @@ namespace dispatchkit
virtual ~Proxy_Function() {} virtual ~Proxy_Function() {}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) = 0; virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) = 0;
virtual std::vector<Type_Info> get_param_types() = 0; virtual std::vector<Type_Info> get_param_types() = 0;
virtual bool operator==(const Proxy_Function &) const = 0;
}; };
class Dynamic_Proxy_Function : public Proxy_Function 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 ~Dynamic_Proxy_Function() {}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params)
@@ -146,6 +151,15 @@ namespace dispatchkit
virtual ~Proxy_Function_Impl() {} virtual ~Proxy_Function_Impl() {}
virtual bool operator==(const Proxy_Function &t_func) const
{
try {
dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func);
return true;
} catch (const std::bad_cast &) {
return false;
}
}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params)
{ {