Skip to content
Snippets Groups Projects
Commit 61911bf3 authored by Thomas Moulard's avatar Thomas Moulard
Browse files

Enhance import to avoid importing a module twice.

parent 5a0d07d0
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,8 @@ namespace dynamicgraph
template<class Time>
class SignalArray;
class Interpreter;
} // end of namespace dynamicgraph.
#endif //! DYNAMIC_GRAPH_FWD_HH
......@@ -23,9 +23,10 @@
# include <boost/filesystem/path.hpp>
# include <dynamic-graph/fwd.hh>
namespace dynamicgraph
{
class Interpreter;
namespace command
{
namespace
......@@ -36,7 +37,26 @@ namespace dynamicgraph
/// will look for scripts or plug-ins.
typedef std::vector<boost::filesystem::path> paths_t;
/// \brief Import paths list.
///
/// This vector of string is similar to Unix variables such as
/// PATH. It contains all paths that are used to search when
/// importing a script.
///
/// The look-up is made from right to left:
///
/// On Unix:
/// importPaths = A:B:C
/// On Microsoft Windows:
/// importPaths = A;B;C
///
/// When typing "import foo", C will be searched first then B
/// and A. The search stops when the file is found.
extern paths_t importPaths;
/// \brief Already imported paths to avoid multiple inclusion.
extern paths_t alreadyImportedPaths;
} // end of anonymous namespace.
/// \brief Implement sot interpretor import command.
......
......@@ -68,22 +68,8 @@ namespace dynamicgraph
/// Initialize import paths list (called during static initialization).
paths_t initializePaths ();
/// \brief Import paths list.
///
/// This vector of string is similar to Unix variables such as
/// PATH. It contains all paths that are used to search when
/// importing a script.
///
/// The look-up is made from right to left:
///
/// On Unix:
/// importPaths = A:B:C
/// On Microsoft Windows:
/// importPaths = A;B;C
///
/// When typing ``import foo'', C will be searched first then B
/// and A. The search stops when the file is found.
paths_t importPaths = initializePaths ();
paths_t alreadyImportedPaths;
/// Search for a module.
///
......@@ -268,6 +254,7 @@ namespace dynamicgraph
// Get the absolute path of the module.
boost::filesystem::path path = searchModule (module);
// Check that the module can be opened.
std::ifstream file (path.file_string ().c_str ());
if (!boost::filesystem::is_regular_file (path)
|| !file.is_open () || !file.good ())
......@@ -296,11 +283,20 @@ namespace dynamicgraph
return;
}
// If the module has already been imported, do not import it
// again.
if (std::find (alreadyImportedPaths.begin (),
alreadyImportedPaths.end (),
path) != alreadyImportedPaths.end ())
return;
if (path.extension () != SHARED_LIBRARY_EXT)
importScript (interpreter, path, file, os);
else
importPlugin (interpreter, path, os);
alreadyImportedPaths.push_back (path);
dgDEBUGOUT(15);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment