1 /* 2 * This file is part of yacas. 3 * Yacas is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU Lesset General Public License as 5 * published by the Free Software Foundation, either version 2.1 6 * of the License, or (at your option) any later version. 7 * 8 * Yacas is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public 14 * License along with yacas. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 /* 19 * File: yacas_engine.hpp 20 * Author: mazur 21 * 22 * Created on November 7, 2015, 12:52 PM 23 */ 24 25 #ifndef YACAS_ENGINE_HPP 26 #define YACAS_ENGINE_HPP 27 28 #include "yacas/yacas.h" 29 30 #include <zmqpp/zmqpp.hpp> 31 32 #include <atomic> 33 #include <condition_variable> 34 #include <deque> 35 #include <mutex> 36 #include <sstream> 37 #include <string> 38 #include <thread> 39 40 class YacasEngine { 41 public: 42 YacasEngine(const std::string& scripts_path, 43 const zmqpp::context& ctx, 44 const std::string& endpoint = "inproc://engine"); 45 46 ~YacasEngine(); 47 48 void submit(unsigned long id, const std::string& expr); 49 50 private: 51 void _worker(); 52 53 std::ostringstream _side_effects; 54 CYacas _yacas; 55 56 struct TaskInfo { 57 unsigned long id; 58 std::string expr; 59 }; 60 61 std::deque<TaskInfo> _tasks; 62 63 std::mutex _mtx; 64 std::condition_variable _cv; 65 66 std::thread* _worker_thread; 67 68 zmqpp::socket _socket; 69 70 std::atomic<bool> _shutdown; 71 }; 72 73 #endif 74