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 namespace ttg {
7 
8 template<typename T>
9 using Ptr = TTG_IMPL_NS::Ptr<T>;
10 
11 template<typename T, typename... Args>
12 inline Ptr<T> make_ptr(Args&&... args) {
13  return TTG_IMPL_NS::make_ptr(std::forward<Args>(args)...);
14 }
15 
16 template<typename T>
17 inline Ptr<std::decay_t<T>> get_ptr(T&& obj) {
18  return TTG_IMPL_NS::get_ptr(std::forward<T>(obj));
19 }
20 
21 #if 0
22 namespace detail {
23 
24  /* awaiter for ttg::get_ptr with multiple arguments
25  * operator co_wait will return the tuple of ttg::Ptr
26  */
27  template<typename... Ts>
28  struct get_ptr_tpl_t {
29  private:
30  std::tuple<ttg::Ptr<Ts>...> m_ptr_tuple;
31  bool m_is_ready = false;
32  public:
33  get_ptr_tpl_t(bool is_ready, std::tuple<ttg::ptr<Ts>...>&& ptrs)
34  : m_ptr_tuple(std::forward<std::tuple<ttg::Ptr<Ts>...>>(ptrs))
35  , m_is_ready(is_ready)
36  { }
37 
38  bool await_ready() const noexcept {
39  return m_is_ready;
40  }
41 
42  constexpr void await_suspend( std::coroutine_handle<> ) const noexcept {
43  /* TODO: anything to be done here? */
44  }
45 
46  auto await_resume() const noexcept {
47  return std::move(m_ptr_tuple);
48  }
49  };
50 
51 
52  /* awaiter for ttg::get_ptr for a single argument */
53  template<typename T>
54  struct get_ptr_t {
55  private:
56  ttg::Ptr<T> m_ptr;
57  bool m_is_ready = false;
58  public:
59  get_ptr_t(bool is_ready, ttg::Ptr<T>&& ptr)
60  : m_ptr(std::forward<ttg::Ptr<T>>(ptr))
61  , m_is_ready(is_ready)
62  { }
63 
64  bool await_ready() const noexcept {
65  return m_is_ready;
66  }
67 
68  constexpr void await_suspend( std::coroutine_handle<> ) const noexcept {
69  /* TODO: anything to be done here? */
70  }
71 
72  auto await_resume() const noexcept {
73  return std::move(m_ptr);
74  }
75  };
76  } // namespace detail
77 
89  template<typename Arg, typename... Args>
90  auto get_ptr(Arg&& arg, Args&&... args) {
91  bool is_ready;
92  using tpl_type = std::tuple<ttg::Ptr<std::decay_t<Arg>, std::decay<Args>...>>;
93  using result_type = std::pair<bool, tpl_type>;
94  result_type p = TTG_IMPL_NS::get_ptr(std::forward<Arg>(arg), std::forward<Args>(args)...);
95  if constexpr (sizeof...(Args) > 0) {
96  return detail::get_ptr_tpl_t<std::decay_t<Arg>, std::decay_t<Args>...>(p.first, std::move(p.second));
97  } else if constexpr (sizeof...(Args) == 0) {
98  return detail::get_ptr_t<std::decay_t<Arg>>(p.first, std::move(std::get<0>(p.second)));
99  }
100  }
101 #endif // 0
102 } // namespace ttg
103 
104 #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:17
TTG_IMPL_NS::Ptr< T > Ptr
Definition: ptr.h:9
Ptr< T > make_ptr(Args &&... args)
Definition: ptr.h:12