Major updates to the C++ API. Please see trunk/src/example.cpp to follow along

This commit is contained in:
Jason Turner
2009-07-18 18:05:54 +00:00
parent ac817ff33a
commit 4d4c26bf73
14 changed files with 451 additions and 416 deletions

View File

@@ -23,22 +23,22 @@
#include "proxy_functions.hpp"
#include "proxy_constructors.hpp"
namespace dispatchkit
namespace chaiscript
{
/**
* A Proxy_Function implementation that is able to take
* a vector of Proxy_Functions and perform a dispatch on them. It is
* used specifically in the case of dealing with Function object variables
*/
class Dispatch_Function : public Proxy_Function
class Dispatch_Function : public Proxy_Function_Base
{
public:
Dispatch_Function(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs)
Dispatch_Function(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs)
: m_funcs(t_funcs)
{
}
virtual bool operator==(const Proxy_Function &) const
virtual bool operator==(const Proxy_Function_Base &) const
{
return false;
}
@@ -57,7 +57,7 @@ namespace dispatchkit
virtual bool types_match(const std::vector<Boxed_Value> &types) const
{
typedef std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > function_vec;
typedef std::vector<std::pair<std::string, Proxy_Function > > function_vec;
function_vec::const_iterator begin = m_funcs.begin();
function_vec::const_iterator end = m_funcs.end();
@@ -81,7 +81,7 @@ namespace dispatchkit
}
private:
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > m_funcs;
std::vector<std::pair<std::string, Proxy_Function > > m_funcs;
};
@@ -92,7 +92,7 @@ namespace dispatchkit
class Dispatch_Engine
{
public:
typedef std::map<std::string, dispatchkit::Type_Info> Type_Name_Map;
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope;
typedef std::deque<Scope> Stack;
@@ -105,48 +105,37 @@ namespace dispatchkit
/**
* Add a new named Proxy_Function to the system
*/
bool register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
bool add(const Proxy_Function &f, const std::string &name)
{
return add_function(f, name);
}
/**
* Add a generic, named boost::function() to the system
*/
template<typename Function>
bool register_function(const Function &func, const std::string &name)
{
return add_function(boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)), name);
}
/**
* Set the value of an object, by name. If the object
* is not available in the current scope it is created
*/
template<typename Class>
void set_object(const std::string &name, const Class &obj)
void add(const Boxed_Value &obj, const std::string &name)
{
for (int i = m_scopes.size()-1; i >= 0; --i)
{
for (int i = m_scopes.size()-1; i >= 0; --i)
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
if (itr != m_scopes[i].end())
{
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
if (itr != m_scopes[i].end())
{
m_scopes[i][name] = Boxed_Value(obj);
return;
}
m_scopes[i][name] = Boxed_Value(obj);
return;
}
add_object(name, obj);
}
add_object(name, obj);
}
/**
* Adds a named object to the current scope
*/
template<typename Class>
void add_object(const std::string &name, const Class &obj)
{
m_scopes.back()[name] = Boxed_Value(obj);
}
void add_object(const std::string &name, const Boxed_Value &obj)
{
m_scopes.back()[name] = Boxed_Value(obj);
}
/**
* Adds a new scope to the stack
@@ -209,24 +198,23 @@ namespace dispatchkit
}
}
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs = get_function_impl(name, false);
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs = get_function_impl(name, false);
if (funcs.empty())
{
throw std::range_error("Object not known: " + name);
} else {
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Dispatch_Function(funcs)));
return Boxed_Value(Proxy_Function(new Dispatch_Function(funcs)));
}
}
/**
* Registers a new named type
*/
template<typename Type>
void register_type(const std::string &name)
{
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get()));
}
void add(const Type_Info &ti, const std::string &name)
{
m_types.insert(std::make_pair(name, ti));
}
/**
* Returns the type info for a named type
@@ -274,7 +262,7 @@ namespace dispatchkit
/**
* Return a function by name
*/
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function(const std::string &t_name) const
{
return get_function_impl(t_name, true);
@@ -283,9 +271,9 @@ namespace dispatchkit
/**
* Get a vector of all registered functions
*/
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > get_functions() const
std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{
return std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >(m_functions.begin(), m_functions.end());
return std::vector<std::pair<std::string, Proxy_Function > >(m_functions.begin(), m_functions.end());
}
private:
@@ -294,10 +282,10 @@ namespace dispatchkit
* Looks for all registered global functions and optionally for an object
* in scope with the same name
*/
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function_impl(const std::string &t_name, bool include_objects) const
{
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs;
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs;
if (include_objects)
{
@@ -305,14 +293,14 @@ namespace dispatchkit
funcs.insert(funcs.end(),
std::make_pair(
t_name,
boxed_cast<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type>(get_object(t_name)))
boxed_cast<std::multimap<std::string, Proxy_Function >::mapped_type>(get_object(t_name)))
);
} catch (const bad_boxed_cast &) {
} catch (const std::range_error &) {
}
}
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name);
funcs.insert(funcs.end(), range.first, range.second);
@@ -324,9 +312,9 @@ namespace dispatchkit
* true if the function was added, false if a function with the
* same signature and name already exists.
*/
bool add_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &t_name)
bool add_function(const Proxy_Function &f, const std::string &t_name)
{
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name);
while (range.first != range.second)
@@ -344,7 +332,7 @@ namespace dispatchkit
std::deque<Scope> m_scopes;
std::multimap<std::string, boost::shared_ptr<Proxy_Function> > m_functions;
std::multimap<std::string, Proxy_Function > m_functions;
Type_Name_Map m_types;
Boxed_Value m_place_holder;
};
@@ -368,7 +356,7 @@ namespace dispatchkit
/**
* Dump function to stdout
*/
void dump_function(const std::pair<const std::string, boost::shared_ptr<Proxy_Function> > &f, const Dispatch_Engine &e)
void dump_function(const std::pair<const std::string, Proxy_Function > &f, const Dispatch_Engine &e)
{
std::vector<Type_Info> params = f.second->get_param_types();
std::string annotation = f.second->annotation();
@@ -412,10 +400,10 @@ namespace dispatchkit
}
std::cout << std::endl;
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs = s.get_functions();
std::vector<std::pair<std::string, Proxy_Function > > funcs = s.get_functions();
std::cout << "Functions: " << std::endl;
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
{