diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 550a9fb..b96d53a 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -32,6 +32,50 @@ 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. /// Used to implement loadable module support. class Module @@ -55,6 +99,17 @@ namespace chaiscript 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 Module &eval(const std::string &str) @@ -76,11 +131,13 @@ namespace chaiscript apply(m_funcs.begin(), m_funcs.end(), t_engine); apply_eval(m_evals.begin(), m_evals.end(), t_eval); apply_single(m_conversions.begin(), m_conversions.end(), t_engine); + apply_globals(m_globals.begin(), m_globals.end(), t_engine); } private: std::vector > m_typeinfos; std::vector > m_funcs; + std::vector > m_globals; std::vector m_evals; std::vector m_conversions; @@ -94,6 +151,16 @@ namespace chaiscript } } + template + void apply_globals(InItr begin, InItr end, T &t) const + { + while (begin != end) + { + t.add_global_const(begin->first, begin->second); + ++begin; + } + } + template 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 { diff --git a/src/test_module.cpp b/src/test_module.cpp index bb0f3a7..633b9de 100644 --- a/src/test_module.cpp +++ b/src/test_module.cpp @@ -13,6 +13,16 @@ class TestBaseType }; +enum TestEnum +{ + TestValue1 = 1 +}; + +int to_int(TestEnum t) +{ + return t; +} + class TestDerivedType : public TestBaseType { 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_global_const(chaiscript::const_var(TestValue1), "TestValue1"); + + m->add(chaiscript::user_type(), "TestEnum"); + + m->add(chaiscript::fun(&to_int), "to_int"); + + return m; }