|
- #pragma once
-
- #include <variant>
-
- #include "result.pre.h"
-
- namespace asyncpp
- {
-
- namespace __impl
- {
-
- struct result_not_ready
- { };
-
- struct result_done
- { };
-
- template<typename T_value>
- struct result_ready
- {
- using value_type = T_value;
-
- value_type value;
-
- template<typename... T_args>
- inline result_ready(T_args&&... p_args);
- };
-
- template<bool for_stream, typename T_value>
- struct result
- {
- public:
- using value_type = T_value;
- using not_ready_type = result_not_ready;
- using ready_type = result_ready<value_type>;
- using done_type = result_done;
- using storage_type = std::conditional_t<
- for_stream,
- std::variant<not_ready_type, ready_type, done_type>,
- std::variant<not_ready_type, ready_type>>;
- using clean_value_type = std::remove_reference_t<value_type>;
- using pointer_type = clean_value_type*;
- using reference_type = clean_value_type&;
- using const_pointer_type = clean_value_type const *;
- using const_reference_type = clean_value_type const &;
-
- private:
- storage_type _storage; //!< Stores the actual result.
-
- private:
- /**
- * @brief Constructor.
- */
- inline result(storage_type&& p_storage);
-
- public:
- /**
- * @brief returns a result that is not ready.
- */
- static inline auto& not_ready();
-
- /**
- * @brief returns a result that is not ready.
- */
- template<typename... X_args>
- static inline auto ready(X_args&&... p_args);
-
- /**
- * @brief returns a result that is not ready.
- */
- template<
- bool X = for_stream,
- typename = std::enable_if_t<X>>
- static inline auto& done();
-
- public:
- /**
- * @brief Get the status of the result.
- */
- template<
- bool X = for_stream,
- typename = std::enable_if_t<X>>
- inline result_status status() const;
-
- /**
- * @brief Check if the result is not ready (is pending).
- */
- inline bool is_not_ready() const;
-
- /**
- * @brief Check if the result is ready (has a value).
- */
- inline bool is_ready() const;
-
- /**
- * @brief Check if the result is done (stream is finished).
- */
- template<
- bool X = for_stream,
- typename = std::enable_if_t<X>>
- inline bool is_done() const;
-
- /**
- * @brief Get the value of the result.
- */
- inline reference_type value();
-
- /**
- * @brief Get the value of the result.
- */
- inline const_reference_type value() const;
-
- public:
- inline operator bool() const;
- inline pointer_type operator-> ();
- inline reference_type operator* ();
- inline const_pointer_type operator-> () const;
- inline const_reference_type operator* () const;
- };
-
- template<bool for_stream>
- struct result<for_stream, void>;
-
- }
-
- }
|