/* Copyright 2017 The MathWorks, Inc. */ #ifndef ENGINE_FUTURE_HPP #define ENGINE_FUTURE_HPP #include #include #include #include namespace matlab { namespace execution { using namespace matlab::engine; /** * @class FutureResult> * This class is a specialization of FutureResult. * matlab::engine::startMATLABAsync and matlab::engine::connectMATLABAsync return instances of this type. */ template <> class FutureResult>: public std::future>{ public: /** * A constructor of FutureResult that accepts a standard future * * @param a_future - A standard future * * @throw none */ FutureResult(std::future>&& a_future); /** * Move constructor of FutureResult * * @param a_futureresult - A future result * * @throw none */ FutureResult(FutureResult>&& a_futureresult); /** * Assignment operator * * @param rhs - A future result * * @throw none */ FutureResult>& operator=(FutureResult>&& rhs); FutureResult(); ~FutureResult(); /** * Swap the contents of this FutureResult with another FutureResult * * @param a_futureresult - A FutureResult * @return void * * @throw none */ void swap(FutureResult>& a_futureresult); /** * Get the MATLAB Engine * * @return the MATLABEngine unique pointer * * @throw InterruptedException, EngineException */ std::unique_ptr get(); /** * Create a shared copy of this future result * * @return a SharedFutureResult * * @throw none */ SharedFutureResult> share(); /** * Check the future result is valid or not * * @return true if valid; false otherwise * * @throw none */ bool valid() const; /** * Wait until the result is ready * * @throw none */ void wait() const; /** * Wait until certain time point * * @param abs_time - A time point * @return the status of the future result * * @throw none */ template std::future_status wait_until(const std::chrono::time_point& abs_time) const; template /** * Wait for certain amount of time * * @param rel_time - Time during to wait * @return the status of the future result * * @throw none */ std::future_status wait_for(const std::chrono::duration& rel_time) const; /** * Cancel the launch or connect of MATLAB * In R2017b, the startAMTLAB and connectMATLAB cannot be canceled. * * @param allowInterrupt - Interrupt the command or not if it is being processed * @return the request is cancel-able or not * * @throw none */ bool cancel(bool allowInterrupt = true); private: FutureResult(std::future>&) = delete; FutureResult(FutureResult&) = delete; FutureResult& operator= (FutureResult&) = delete; std::future> future; friend SharedFutureResult>; }; /** * @class SharedFutureResult> * This class is a specialization of SharedFutureResult. * It can be created by calling FutureResult>.shared() */ template <> class SharedFutureResult>: public std::shared_future>{ public: SharedFutureResult(); ~SharedFutureResult(); /** * Swap the contents of this SharedFutureResult with another SharedFutureResult * * @param a_sharedfuture - A SharedFutureResult * @return void * * @throw none */ void swap(SharedFutureResult>& a_sharedfuture); /** * Copy constructor */ SharedFutureResult(const SharedFutureResult& a_sharedfuture); /** * Move constructor */ SharedFutureResult(SharedFutureResult&& a_sharedfuture); /** * Move constructor that accepts a FutureResult */ SharedFutureResult(FutureResult>&& a_futureresult); /** * Move assignment operator * * @param rhs - A shared future result * * @throw none */ SharedFutureResult>& operator=(SharedFutureResult>&& rhs); /** * Assignment operator * * @param rhs - A shared future result * * @throw none */ SharedFutureResult>& operator=(const SharedFutureResult>& rhs); /** * Get the MATLAB Engine * * @return the MATLABEngine unique pointer * * @throw InterruptedException, EngineException */ const std::unique_ptr& get() const; /** * Check the future result is valid or not * * @return true if valid; false otherwise * * @throw none */ bool valid() const; /** * Wait until the result is ready * * @throw none */ void wait() const; /** * Wait until certain time point * * @param abs_time - A time point * @return the status of the future result * * @throw none */ template std::future_status wait_until(const std::chrono::time_point& abs_time) const; /** * Wait for certain amount of time * * @param rel_time - Time during to wait * @return the status of the future result * * @throw none */ template std::future_status wait_for(const std::chrono::duration& rel_time) const; /** * Cancel the execution of a MATLAB command * In R2017b, the startAMTLAB and connectMATLAB cannot be canceled. * * @param allowInterrupt - Interrupt the command or not if it is being processed * @return the request is cancel-able or not * * @throw none */ bool cancel(bool allowInterrupt = true); private: std::shared_future> sharedFuture; }; } } #endif /* ENGINE_FUTURE_HPP */