LoginSignup
1
1

More than 5 years have passed since last update.

D言語でComPtr

Last updated at Posted at 2015-04-30

comの開発を進めるにあたって、コピーコンストラクタでAddRefして、デストラクタでReleaseするスマートポインタ的な入れ物が必要であろう。ということで作ってみた。

struct ComPtr(T)
{
    T ptr;
    alias ptr this;
    this(this) { if (ptr) ptr.AddRef(); } // post-blit
    ~this() { if(ptr)ptr.Release(); } // destructor
    bool opCast(V)() const if (is(V==bool))
    {
        return ptr ? true : false;
    }
}

当初opDispatchで9引数のテンプレート展開までを手書きしていたのだが、返り値のvoid・非voidの呼び分けができずに頓挫していた。運よくalias thisを発見。まさにこれこそ必要なものだ。素晴らしい。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1