From 13f98fa8ce20d09fa8b83b3ae7d2666400252814 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 30 Jan 2012 11:26:46 -0700 Subject: [PATCH] Move "use" function to be public --- .../chaiscript/language/chaiscript_engine.hpp | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 86246db48..5a6102a52 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -285,33 +285,6 @@ namespace chaiscript return do_eval(t_e, "__EVAL__", true); } - void use(const std::string &t_filename) - { - for (size_t i = 0; i < m_usepaths.size(); ++i) - { - - try { - const std::string appendedpath = m_usepaths[i] + t_filename; - - chaiscript::detail::threading::lock_guard l(m_use_mutex); - chaiscript::detail::threading::shared_lock l2(m_mutex); - - if (m_used_files.count(appendedpath) == 0) - { - m_used_files.insert(appendedpath); - l2.unlock(); - eval_file(appendedpath); - } - } catch (const exception::file_not_found_error &) { - if (i == m_usepaths.size() - 1) - { - throw exception::file_not_found_error(t_filename); - } - - // failed to load, try the next path - } - } - } /** * Returns the current evaluation m_engine @@ -417,6 +390,40 @@ namespace chaiscript build_eval_system(); } + /// \brief Loads and parses a file. If the file is already, it is not reloaded + /// The use paths specified at ChaiScript construction time are searched for the + /// requested file. + /// + /// \param[in] t_filename Filename to load and evaluate + void use(const std::string &t_filename) + { + for (size_t i = 0; i < m_usepaths.size(); ++i) + { + try { + const std::string appendedpath = m_usepaths[i] + t_filename; + + chaiscript::detail::threading::lock_guard l(m_use_mutex); + chaiscript::detail::threading::shared_lock l2(m_mutex); + + if (m_used_files.count(appendedpath) == 0) + { + m_used_files.insert(appendedpath); + l2.unlock(); + eval_file(appendedpath); + } + + return; // return, we loaded it, or it was already loaded + } catch (const exception::file_not_found_error &) { + if (i == m_usepaths.size() - 1) + { + throw exception::file_not_found_error(t_filename); + } + + // failed to load, try the next path + } + } + } + /// \brief Adds a constant object that is available in all contexts and to all threads /// \param[in] t_bv Boxed_Value to add as a global /// \param[in] t_name Name of the value to add