libcopp  2.2.0
poller.h
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
3 #pragma once
4 
5 #include <libcopp/utils/config/libcopp_build_features.h>
6 
8 
9 LIBCOPP_COPP_NAMESPACE_BEGIN
10 namespace future {
11 template <class T, class TPTR = typename poll_storage_ptr_selector<T>::type>
12 class LIBCOPP_COPP_API_HEAD_ONLY poller {
13  public:
15  // If T is trivial and is smaller than size of four pointer, using small object optimization
18  using ptr_type = typename poll_storage::ptr_type;
20 
21  public:
22  inline poller() LIBCOPP_MACRO_NOEXCEPT { poll_storage::construct_default_storage(storage_data_); }
23 
24  template <class... U>
25  inline poller(U &&...in) LIBCOPP_MACRO_NOEXCEPT {
26  setup_from(std::forward<U>(in)...);
27  }
28 
29  inline poller(self_type &&other) LIBCOPP_MACRO_NOEXCEPT { setup_from(std::move(other)); }
30 
31  inline poller &operator=(self_type &&other) LIBCOPP_MACRO_NOEXCEPT {
32  setup_from(std::move(other));
33  return *this;
34  }
35 
36  template <class U>
37  inline poller &operator=(U &&in) LIBCOPP_MACRO_NOEXCEPT {
38  setup_from(std::forward<U>(in));
39  return *this;
40  }
41 
42  UTIL_FORCEINLINE bool is_ready() const LIBCOPP_MACRO_NOEXCEPT { return !!poll_storage::unwrap(storage_data_); }
43 
44  UTIL_FORCEINLINE bool is_pending() const LIBCOPP_MACRO_NOEXCEPT { return !poll_storage::unwrap(storage_data_); }
45 
46  UTIL_FORCEINLINE const value_type *data() const LIBCOPP_MACRO_NOEXCEPT {
47  return poll_storage::unwrap(storage_data_).get();
48  }
49  UTIL_FORCEINLINE value_type *data() LIBCOPP_MACRO_NOEXCEPT { return poll_storage::unwrap(storage_data_).get(); }
50 
51  UTIL_FORCEINLINE const ptr_type &raw_ptr() const LIBCOPP_MACRO_NOEXCEPT {
52  return poll_storage::unwrap(storage_data_);
53  }
54  UTIL_FORCEINLINE ptr_type &raw_ptr() LIBCOPP_MACRO_NOEXCEPT { return poll_storage::unwrap(storage_data_); }
55 
56  UTIL_FORCEINLINE void reset() { poll_storage::reset(storage_data_); }
57  UTIL_FORCEINLINE void swap(self_type &other) LIBCOPP_MACRO_NOEXCEPT {
58  poll_storage::swap(storage_data_, other.storage_data_);
59  }
60  UTIL_FORCEINLINE friend void swap(self_type &l, self_type &r) LIBCOPP_MACRO_NOEXCEPT { l.swap(r); }
61 
62  private:
63  template <class U, class UDELETER,
64  typename std::enable_if<std::is_base_of<T, typename std::decay<U>::type>::value, bool>::type = false>
65  inline void setup_from(std::unique_ptr<U, UDELETER> &&in) {
66  poll_storage::construct_storage(storage_data_, std::move(in));
67  }
68 
69  template <class... TARGS>
70  inline void setup_from(TARGS &&...args) {
71  poll_storage::construct_storage(storage_data_, std::forward<TARGS>(args)...);
72  }
73 
74  inline void setup_from(self_type &&other) {
75  poll_storage::move_storage(storage_data_, std::move(other.storage_data_));
76  }
77 
78  private:
80 };
81 } // namespace future
82 LIBCOPP_COPP_NAMESPACE_END
void setup_from(TARGS &&...args)
Definition: poller.h:70
UTIL_FORCEINLINE const ptr_type & raw_ptr() const LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:51
void setup_from(self_type &&other)
Definition: poller.h:74
typename poll_storage::ptr_type ptr_type
Definition: poller.h:18
storage_type storage_data_
Definition: poller.h:79
UTIL_FORCEINLINE const value_type * data() const LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:46
typename poll_storage::storage_type storage_type
Definition: poller.h:17
UTIL_FORCEINLINE bool is_ready() const LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:42
void setup_from(std::unique_ptr< U, UDELETER > &&in)
Definition: poller.h:65
poller & operator=(self_type &&other) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:31
UTIL_FORCEINLINE friend void swap(self_type &l, self_type &r) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:60
UTIL_FORCEINLINE value_type * data() LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:49
UTIL_FORCEINLINE void swap(self_type &other) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:57
UTIL_FORCEINLINE void reset()
Definition: poller.h:56
poller() LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:22
poller(self_type &&other) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:29
typename poll_storage::value_type value_type
Definition: poller.h:19
poller(U &&...in) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:25
poller & operator=(U &&in) LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:37
UTIL_FORCEINLINE bool is_pending() const LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:44
UTIL_FORCEINLINE ptr_type & raw_ptr() LIBCOPP_MACRO_NOEXCEPT
Definition: poller.h:54
#define UTIL_FORCEINLINE
Definition: future.h:18
std::shared_ptr< cli::cmd_option_value > value_type
Definition: cmd_option.h:50
void swap(intrusive_ptr< T > &lhs, intrusive_ptr< T > &rhs)