/* Copyright 2017 The MathWorks, Inc. */ #ifndef VALUE_FUTURE_HPP #define VALUE_FUTURE_HPP #include #include #include #include namespace matlab { namespace engine { template class FutureResult; template class SharedFutureResult; template class FutureResult: public std::future { public: 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); /** * A constructor of FutureResult that accepts a standard future * * @param a_future - A standard future * * @throw none */ FutureResult(std::future&& a_future); /** * A constructor of FutureResult that accepts a standard future and a task reference * * @param a_future - A standard future * @param a_taskreference - A shared pointer of task reference * * @throw none */ FutureResult(std::future&& a_future, std::shared_ptr a_taskreference); /** * A constructor of FutureResult that accepts a standard future, a task reference, and buffers * * @param a_future - A standard future * @param a_taskreference - A shared pointer of task reference * @param outputBuffer - A shared pointer of StreamBuffer used to store standard output * @param errorBuffer - A shared pointer of StreamBuffer used to store standard error * * @throw none */ FutureResult(std::future&& a_future, const std::shared_ptr& a_taskreference, const std::shared_ptr& outputBuffer, const std::shared_ptr& errorBuffer ); /** * 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); /** * Create a shared copy of this future result * * @return a SharedFutureResult * * @throw none */ SharedFutureResult share(); /** * Get the result of MATLAB function call * * @return the result * * @throw MatlabSyntaxException, MatlabExecutionException, TypeConversionException, MatlabNotAvailableException, CancelledException, InterruptedException */ T get(); /** * 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 execution of a MATLAB command * * @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); /** * Get the task reference * * @return the shared pointer of the task reference * * @throw none */ std::shared_ptr getTaskReference(); private: FutureResult(std::future&) = delete; FutureResult(const FutureResult&) = delete; FutureResult& operator= (FutureResult&) = delete; std::future future; std::shared_ptr taskReference; std::weak_ptr output; std::weak_ptr error; friend SharedFutureResult; }; 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 result of MATLAB function call * * @return the result * * @throw MatlabSyntaxException, MatlabExecutionException, TypeConversionException, MatlabNotAvailableException, CancelledException, InterruptedException */ decltype(std::declval>().get()) 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; 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 execution of a MATLAB command * * @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; std::shared_ptr taskReference; }; } } #endif /* VALUE_FUTURE_HPP */