ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

gcc里的coroutine_handle

2021-12-26 16:04:29  阅读:212  来源: 互联网

标签:__ gcc handle coroutine void fr ptr


真正干事的是__builtin_coro_done, __builtin_coro_resume和__builtin_coro_destroy, coroutine_handle只是在void*外面包了一层。它没有析构函数,得去调destroy(). 它重载了(), h()等于h.resume()等于__builtin_coro_resume(ptr);

示意:

template<> struct coroutine_handle<void> {
    void*    m_ptr;
    bool done() { return __builtin_coro_done(m_ptr); }
    void resume() { __builtin_coro_resume(m_ptr); }
    void operator()() { resume(); }
    void destroy() { __builtin_coro_destroy(m_ptr); }
    operator bool() { return bool(m_ptr); }
    void* address() { return m_ptr; }
    static coroutine_handle from_address(void*);
    coroutine_handle();
    // https://en.cppreference.com/w/cpp/types/nullptr_t
    // coroutine_handle h(0); coroutine_handle h(NULL); h = 0; h = NULL;
    // oneptr_t, twoptr_t ... _10000ptr_t?
    coroutine_handle(std::nullptr_t);
    coroutine_handle& operator=(std::nullptr_t);
};

D:\gcc\lib\gcc\mingw32\10.3.0\include\c++\coroutine: tab size不为8时更乱:

  template <> struct
    coroutine_handle<void>
    {
    public:
      // 17.12.3.1, construct/reset
      constexpr coroutine_handle() noexcept : _M_fr_ptr(0) {}

      constexpr coroutine_handle(std::nullptr_t __h) noexcept
    : _M_fr_ptr(__h)
      {}

      coroutine_handle& operator=(std::nullptr_t) noexcept
      {
    _M_fr_ptr = nullptr;
    return *this;
      }

    public:
      // 17.12.3.2, export/import
      constexpr void* address() const noexcept { return _M_fr_ptr; }

      constexpr static coroutine_handle from_address(void* __a) noexcept
      {
    coroutine_handle __self;
    __self._M_fr_ptr = __a;
    return __self;
      }

    public:
      // 17.12.3.3, observers
      constexpr explicit operator bool() const noexcept
      {
    return bool(_M_fr_ptr);
      }

      bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); }

      // 17.12.3.4, resumption
      void operator()() const { resume(); }

      void resume() const { __builtin_coro_resume(_M_fr_ptr); }

      void destroy() const { __builtin_coro_destroy(_M_fr_ptr); }

    protected:
      void* _M_fr_ptr;
  };

  // 17.12.3.6 Comparison operators
  /// [coroutine.handle.compare]
  constexpr bool operator==(coroutine_handle<> __a,
                coroutine_handle<> __b) noexcept
  {
    return __a.address() == __b.address();
  }
View Code

_M_fr_ptr咋就比m_ptr高档了?我倒是也觉得reinterpret_cast没鸟用。VC的coroutine也未必好看到哪里去,STL好像是SGI的相对最好看。封装是OOP的头一条,protected的成员变量用_打头有必要吗?struct默认public, class默认private,struct后紧跟public没必要。

标签:__,gcc,handle,coroutine,void,fr,ptr
来源: https://www.cnblogs.com/funwithwords/p/15733190.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有