gpt4 book ai didi

c++-cli - native 智能指针类变量的 c++/cli 包装器

转载 作者:行者123 更新时间:2023-12-04 16:48:29 26 4
gpt4 key购买 nike

我是 C# 和 C++/CLI 的新手,对它们不是很熟悉。我进行了搜索,但找不到一个非常简单的事情的现有解决方案(我想这应该很常见)。

我正在编写一个简单的 C++/CLI 项目(C# 中使用的 C++ native dll 的包装器)。有很多示例如何为 native 类编写包装器:

class CNativeClass { 
CNativeClass() {}
void Foo() {}
}

public ref class CNativeClassWrapper {
public:
CNativeClassWrapper() {
_Wrapper = new CNativeClass;
}
~CNativeClassWrapper() {
this->!CNativeClassWrapper();
}
!CNativeClassWrapper() {
delete _Wrapper;
_Wrapper = nullptr;
}
void Foo() {
_Wrapper->Foo();
}
private:
CNativeClass* _Wrapper;
}

这是简化的示例代码。可能有更多的原生类,可能有延迟初始化......很多次要但必要的代码。

有没有隐藏所有这些细节的 native 类的变量包装器?就像纯 C++ 中的智能指针模板。

此外,如果一个本地类实际上是一个智能指针(我的 90% 的情况下,我们使用 boost::shared_ptr<>),代码就会变得一团糟:

  • 检查可以是 if ( _Wrapper )if ( *_Wrapper ) 甚至 if ( _Wrapper && *_Wrapper ),
  • 访问方法(*_Wrapper)->Foo();,
  • 如何重置? (*_Wrapper).reset();_Wrapper->reset(); 都是正确的,但太困惑了。

有人知道这个的解决方案吗?

最佳答案

好吧,我终于来到了这样的包装类:

  • 用于举办一般母语类(class):

    template <class T>
    public ref class native_auto_ptr
    {
    public:
    native_auto_ptr()
    {
    _pNative = new T;
    }
    native_auto_ptr(const native_auto_ptr<T>% rhs)
    {
    _pNative = new T( *rhs._pNative );
    }
    ~native_auto_ptr()
    {
    reset();
    }
    void reset()
    {
    delete _pNative;
    _pNative = nullptr;
    }
    T* ptr()
    {
    return _pNative;
    }
    T* operator->()
    {
    return _pNative;
    }
    operator bool()
    {
    return _pNative;
    }
    void operator=(const native_auto_ptr<T>% rhs)
    {
    *_pNative = *rhs._pNative;
    }
    void operator=(const T% rhs)
    {
    *_pNative = rhs;
    }
    protected:
    !native_auto_ptr()
    {
    reset();
    }
    private:
    T* _pNative;
    };
  • 持有shared_ptr<> :

    template <class T>
    public ref class native_shared_ptr
    {
    public:
    native_shared_ptr()
    {
    _spNative = new boost::shared_ptr<T>;
    }
    native_shared_ptr(const native_shared_ptr<T>% rhs)
    {
    _spNative = new boost::shared_ptr<T>;
    *this = rhs;
    }
    native_shared_ptr(const boost::shared_ptr<T>% rhs)
    {
    _spNative = new boost::shared_ptr<T>( rhs );
    }
    ~native_shared_ptr()
    {
    reset();
    }
    void reset()
    {
    delete _spNative;
    _spNative = nullptr;
    }
    T* operator->()
    {
    return _spNative->get();
    }
    operator bool()
    {
    return _spNative && *_spNative;
    }
    void operator=(const native_shared_ptr<T>% rhs)
    {
    *_spNative = *rhs._spNative;
    }
    void operator=(const boost::shared_ptr<T>% rhs)
    {
    *_spNative = rhs;
    }
    protected:
    !native_shared_ptr()
    {
    reset();
    }
    private:
    boost::shared_ptr<T>* _spNative;
    };

我很高兴:)

  • 他们隐藏新的/删除的工作。
  • 他们很安全。
  • 它们的语法接近于普通的智能指针,很方便。

唯一的问题是带有参数的 ctors...(TBD)

关于c++-cli - native 智能指针类变量的 c++/cli 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33393718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com