Add files missing from last checkin

This commit is contained in:
Jason Turner
2011-09-09 17:08:51 -06:00
parent 0293762904
commit d9727973c1
2 changed files with 84 additions and 42 deletions

View File

@@ -32,6 +32,50 @@
namespace chaiscript namespace chaiscript
{ {
namespace exception
{
/**
* Exception thrown in the case that a multi method dispatch fails
* because no matching function was found
* at runtime due to either an arity_error, a guard_error or a bad_boxed_cast
* exception
*/
class reserved_word_error : public std::runtime_error
{
public:
reserved_word_error(const std::string &t_word) throw()
: std::runtime_error("Reserved word not allowed in object name: " + t_word), m_word(t_word)
{
}
virtual ~reserved_word_error() throw() {}
std::string word() const
{
return m_word;
}
private:
std::string m_word;
};
/**
* Exception thrown in the case that a non-const object was added as a shared object
*/
class global_non_const : public std::runtime_error
{
public:
global_non_const() throw()
: std::runtime_error("a global object must be const")
{
}
virtual ~global_non_const() throw() {}
};
}
/// \brief Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime. /// \brief Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime.
/// Used to implement loadable module support. /// Used to implement loadable module support.
class Module class Module
@@ -55,6 +99,17 @@ namespace chaiscript
return *this; return *this;
} }
Module &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
{
if (!t_bv.is_const())
{
throw exception::global_non_const();
}
m_globals.push_back(std::make_pair(t_bv, t_name));
return *this;
}
//Add a bit of chaiscript to eval during module implementation //Add a bit of chaiscript to eval during module implementation
Module &eval(const std::string &str) Module &eval(const std::string &str)
@@ -76,11 +131,13 @@ namespace chaiscript
apply(m_funcs.begin(), m_funcs.end(), t_engine); apply(m_funcs.begin(), m_funcs.end(), t_engine);
apply_eval(m_evals.begin(), m_evals.end(), t_eval); apply_eval(m_evals.begin(), m_evals.end(), t_eval);
apply_single(m_conversions.begin(), m_conversions.end(), t_engine); apply_single(m_conversions.begin(), m_conversions.end(), t_engine);
apply_globals(m_globals.begin(), m_globals.end(), t_engine);
} }
private: private:
std::vector<std::pair<Type_Info, std::string> > m_typeinfos; std::vector<std::pair<Type_Info, std::string> > m_typeinfos;
std::vector<std::pair<Proxy_Function, std::string> > m_funcs; std::vector<std::pair<Proxy_Function, std::string> > m_funcs;
std::vector<std::pair<Boxed_Value, std::string> > m_globals;
std::vector<std::string> m_evals; std::vector<std::string> m_evals;
std::vector<Dynamic_Cast_Conversion> m_conversions; std::vector<Dynamic_Cast_Conversion> m_conversions;
@@ -94,6 +151,16 @@ namespace chaiscript
} }
} }
template<typename T, typename InItr>
void apply_globals(InItr begin, InItr end, T &t) const
{
while (begin != end)
{
t.add_global_const(begin->first, begin->second);
++begin;
}
}
template<typename T, typename InItr> template<typename T, typename InItr>
void apply_single(InItr begin, InItr end, T &t) const void apply_single(InItr begin, InItr end, T &t) const
{ {
@@ -266,48 +333,6 @@ namespace chaiscript
}; };
} }
namespace exception
{
/**
* Exception thrown in the case that a multi method dispatch fails
* because no matching function was found
* at runtime due to either an arity_error, a guard_error or a bad_boxed_cast
* exception
*/
class reserved_word_error : public std::runtime_error
{
public:
reserved_word_error(const std::string &t_word) throw()
: std::runtime_error("Reserved word not allowed in object name: " + t_word), m_word(t_word)
{
}
virtual ~reserved_word_error() throw() {}
std::string word() const
{
return m_word;
}
private:
std::string m_word;
};
/**
* Exception thrown in the case that a non-const object was added as a shared object
*/
class global_non_const : public std::runtime_error
{
public:
global_non_const() throw()
: std::runtime_error("a global object must be const")
{
}
virtual ~global_non_const() throw() {}
};
}
namespace detail namespace detail
{ {

View File

@@ -13,6 +13,16 @@ class TestBaseType
}; };
enum TestEnum
{
TestValue1 = 1
};
int to_int(TestEnum t)
{
return t;
}
class TestDerivedType : public TestBaseType class TestDerivedType : public TestBaseType
{ {
public: public:
@@ -61,6 +71,13 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::fun(&get_new_int), "get_new_int"); m->add(chaiscript::fun(&get_new_int), "get_new_int");
m->add_global_const(chaiscript::const_var(TestValue1), "TestValue1");
m->add(chaiscript::user_type<TestEnum>(), "TestEnum");
m->add(chaiscript::fun(&to_int), "to_int");
return m; return m;
} }