OpenWalnut  1.5.0dev
WScriptEngine.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <memory>
26 #include <string>
27 
28 #include "WScriptEngine.h"
29 #include "python/WScriptInterpreterPython.h"
30 
31 WScriptEngine::WScriptEngine( std::shared_ptr<WModuleContainer> const& rootContainer )
32 {
33 #ifdef PYTHON_FOUND // this is defined in the CMake scripts
34  m_interpreters.push_back( std::shared_ptr< WScriptInterpreter >( new WScriptInterpreterPython( rootContainer ) ) );
35 #else
36  ( void ) rootContainer; // avoid the compiler warning: unused parameter ‘rootContainer’
37 #endif
38  for( std::size_t k = 0; k < m_interpreters.size(); ++k )
39  {
40  m_interpreters[ k ]->initBindings();
41  }
42 }
43 
45 {
46 }
47 
48 std::shared_ptr< WScriptInterpreter > WScriptEngine::getInterpreterByFileExtension( std::string const& ext )
49 {
50  for( std::size_t k = 0; k < m_interpreters.size(); ++k )
51  {
52  if( ext == m_interpreters[ k ]->getExtension() )
53  {
54  return m_interpreters[ k ];
55  }
56  }
57  return std::shared_ptr< WScriptInterpreter >();
58 }
59 
60 std::shared_ptr< WScriptInterpreter > WScriptEngine::getInterpreter( std::string const& name )
61 {
62  for( std::size_t k = 0; k < m_interpreters.size(); ++k )
63  {
64  if( name == m_interpreters[ k ]->getName() )
65  {
66  return m_interpreters[ k ];
67  }
68  }
69  return std::shared_ptr< WScriptInterpreter >();
70 }
71 
73 {
74  return m_interpreters.size();
75 }
76 
77 std::shared_ptr< WScriptInterpreter > WScriptEngine::getInterpreter( std::size_t index )
78 {
79  if( index < m_interpreters.size() )
80  {
81  return m_interpreters[ index ];
82  }
83  return std::shared_ptr< WScriptInterpreter >();
84 }
std::shared_ptr< WScriptInterpreter > getInterpreter(std::string const &name)
This finds an interpreter by script language name.
virtual ~WScriptEngine()
Destructor.
std::shared_ptr< WScriptInterpreter > getInterpreterByFileExtension(std::string const &ext)
This finds an interpreter suitable for executing script files ending with the given extension.
std::size_t getNumInterpreters() const
Get the number of script interpreters available.
std::vector< std::shared_ptr< WScriptInterpreter > > m_interpreters
The list of available script interpreters.
Definition: WScriptEngine.h:91
WScriptEngine(std::shared_ptr< WModuleContainer > const &rootContainer)
Constructs a new script engine.