ptr.h
Go to the documentation of this file.
1 #ifndef TTG_PTR_H
2 #define TTG_PTR_H
3 
4 #include "ttg/fwd.h"
5 
6 #include "ttg/util/meta.h"
7 
8 namespace ttg {
9 
10 template<typename T>
11 using Ptr = TTG_IMPL_NS::Ptr<T>;
12 
13 template<typename T, typename... Args>
14 inline Ptr<T> make_ptr(Args&&... args) {
15  return TTG_IMPL_NS::make_ptr(std::forward<Args>(args)...);
16 }
17 
18 template<typename T>
19 inline Ptr<std::decay_t<T>> get_ptr(T&& obj) {
20  return TTG_IMPL_NS::get_ptr(std::forward<T>(obj));
21 }
22 
23 namespace meta {
24 
25  /* specialize some traits */
26 
27  template<typename T>
28  struct is_ptr<ttg::Ptr<T>> : std::true_type
29  { };
30 
31 } // namespace ptr
32 
33 #if 0
34 namespace detail {
35 
36  /* awaiter for ttg::get_ptr with multiple arguments
37  * operator co_wait will return the tuple of ttg::Ptr
38  */
39  template<typename... Ts>
40  struct get_ptr_tpl_t {
41  private:
42  std::tuple<ttg::Ptr<Ts>...> m_ptr_tuple;
43  bool m_is_ready = false;
44  public:
45  get_ptr_tpl_t(bool is_ready, std::tuple<ttg::ptr<Ts>...>&& ptrs)
46  : m_ptr_tuple(std::forward<std::tuple<ttg::Ptr<Ts>...>>(ptrs))
47  , m_is_ready(is_ready)
48  { }
49 
50  bool await_ready() const noexcept {
51  return m_is_ready;
52  }
53 
54  constexpr void await_suspend( std::coroutine_handle<> ) const noexcept {
55  /* TODO: anything to be done here? */
56  }
57 
58  auto await_resume() const noexcept {
59  return std::move(m_ptr_tuple);
60  }
61  };
62 
63 
64  /* awaiter for ttg::get_ptr for a single argument */
65  template<typename T>
66  struct get_ptr_t {
67  private:
68  ttg::Ptr<T> m_ptr;
69  bool m_is_ready = false;
70  public:
71  get_ptr_t(bool is_ready, ttg::Ptr<T>&& ptr)
72  : m_ptr(std::forward<ttg::Ptr<T>>(ptr))
73  , m_is_ready(is_ready)
74  { }
75 
76  bool await_ready() const noexcept {
77  return m_is_ready;
78  }
79 
80  constexpr void await_suspend( std::coroutine_handle<> ) const noexcept {
81  /* TODO: anything to be done here? */
82  }
83 
84  auto await_resume() const noexcept {
85  return std::move(m_ptr);
86  }
87  };
88  } // namespace detail
89 
101  template<typename Arg, typename... Args>
102  auto get_ptr(Arg&& arg, Args&&... args) {
103  bool is_ready;
104  using tpl_type = std::tuple<ttg::Ptr<std::decay_t<Arg>, std::decay<Args>...>>;
105  using result_type = std::pair<bool, tpl_type>;
106  result_type p = TTG_IMPL_NS::get_ptr(std::forward<Arg>(arg), std::forward<Args>(args)...);
107  if constexpr (sizeof...(Args) > 0) {
108  return detail::get_ptr_tpl_t<std::decay_t<Arg>, std::decay_t<Args>...>(p.first, std::move(p.second));
109  } else if constexpr (sizeof...(Args) == 0) {
110  return detail::get_ptr_t<std::decay_t<Arg>>(p.first, std::move(std::get<0>(p.second)));
111  }
112  }
113 #endif // 0
114 } // namespace ttg
115 
116 #endif // TTG_PTR_H
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8
Ptr< std::decay_t< T > > get_ptr(T &&obj)
Definition: ptr.h:19
TTG_IMPL_NS::Ptr< T > Ptr
Definition: ptr.h:11
Ptr< T > make_ptr(Args &&... args)
Definition: ptr.h:14