/* Copyright 2017 The MathWorks, Inc. */ #ifndef ENGINE_FACTORY_IMPL_HPP #define ENGINE_FACTORY_IMPL_HPP #include #include #include #include #include #include "../cpp_engine_api.hpp" #include "../matlab_engine.hpp" #include "../engine_factory.hpp" #include "../engine_future.hpp" #include "../engine_exception.hpp" namespace { void initSession() { cpp_engine_create_session(); } bool cancelFind(uintptr_t, bool allowInterrupt) { return false; } } namespace matlab { namespace engine { using namespace matlab::execution; inline std::unique_ptr startMATLAB(const std::vector& options) { return startMATLABAsync(options).get(); } inline FutureResult> startMATLABAsync(const std::vector& options) { initSession(); auto startMATLABType = [options]() { std::vector options_v(options.size()); std::transform(options.begin(), options.end(), options_v.begin(), [](const std::u16string& option){ return const_cast(option.c_str()); }); bool errFlag = false; uint64_t matlab = cpp_engine_create_out_of_process_matlab(options_v.data(), options_v.size(), &errFlag); if (errFlag) { throw EngineException("MATLAB process cannot be created."); } return std::unique_ptr(new MATLABEngine(matlab)); }; std::future> stdF = std::async(std::launch::async, startMATLABType); FutureResult> future(std::move(stdF)); return future; } inline std::vector findMATLAB() { initSession(); char16_t** names; size_t size = cpp_engine_find_shared_matlab(&names); std::vector names_v(size); for (size_t i = 0; i < size; i++) { names_v[i] = names[i]; } cpp_engine_destroy_names(names, size); return names_v; } inline FutureResult> findMATLABAsync() { std::future > stdFuture = std::async(std::launch::async, findMATLAB); FutureResult> future(std::move(stdFuture), std::make_shared(&cancelFind)); return future; } inline std::unique_ptr connectMATLAB() { return connectMATLABAsync().get(); } inline std::unique_ptr connectMATLAB(const std::u16string& name) { return connectMATLABAsync(name).get(); } inline FutureResult > connectMATLABAsync() { std::vector engines = findMATLAB(); if (!engines.empty()) { return connectMATLABAsync(engines[0]); } else { std::vector options; std::u16string option1 = convertUTF8StringToUTF16String("-r"); std::u16string option2 = convertUTF8StringToUTF16String("matlab.engine.shareEngine"); options.push_back(option1); options.push_back(option2); return startMATLABAsync(options); } } inline FutureResult> connectMATLABAsync(const std::u16string& name) { initSession(); auto connectMATLABType = [name]() { bool errFlag = false; uint64_t matlab = cpp_engine_attach_shared_matlab(name.c_str(), &errFlag); if (errFlag) { throw EngineException("MATLAB session cannot be connected."); } return std::unique_ptr(new MATLABEngine(matlab)); }; std::future> stdF = std::async(std::launch::async, connectMATLABType); FutureResult> future(std::move(stdF)); return future; } inline void terminateEngineClient() { //initialize the session and load the library so the call to engine_terminate_session will not crash. initSession(); cpp_engine_terminate_session(); } } } #endif //ENGINE_FACTORY_IMPL_HPP