独立的shared_ptr;执行?

发布于 2024-12-02 06:41:25 字数 260 浏览 0 评论 0 原文

有谁知道我可以在程序中使用 TR1 shared_ptr (也许还有其他智能指针)的开源独立实现?


注意:

shared_ptr 的独立实现”表示shared_ptr本身需要是独立的。
不仅仅是包含库。

所以请不要提升

Does anyone know of an open-source, standalone implementation of TR1 shared_ptr (and maybe other smart pointers) that I can use in my programs?


Note:

"Standalone implementation of shared_ptr" means shared_ptr itself needs to be standalone.
Not just the including library.

So please, no Boost!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情愿 2024-12-09 06:41:25

shared_ptr 的 boost 实现完全是头文件,因此安装 boost 来使用它就像下载 boost 并将其添加到编译器的头文件搜索路径一样简单。使用 boost 实现这一点并不比任何其他独立实现更难。如果您只想提取 shared_ptr 组件来进行单独的分发,那么您可以使用 增强 BCP

The boost implementation of shared_ptr is entirely header-only, so installing boost to use it is as simple as downloading boost and adding it to your compiler's header search paths. This is no harder with boost than with any other stand-alone implementation. If you want to extract just the shared_ptr component to make a separate distribution, then you can use Boost BCP.

扶醉桌前 2024-12-09 06:41:25

你可以很容易地修改 wxWidgets 中的sharedptr.h 头来删除一些宏依赖项(assert、“explicit”关键字等)。然后,您需要替换或删除内部引用计数变量的原子递增/递减。重命名模板并将其粘贴到命名空间中。然后您将拥有一个单文件独立版本的shared_ptr。

这样做的优点是您正在修改的源代码已经得到了广泛的使用和同行评审。

You can hack the sharedptr.h header in wxWidgets pretty easily to remove the few macro dependencies (assert, "explicit" keyword, etc.). You then need to replace or remove the atomic inc/dec of the internal reference counting variable. Rename the template and stick it in a namespace. Then you'll have a single-file stand-along version of shared_ptr.

This has the advantage that the source you are modifying has had some wide usage and peer review.

放我走吧 2024-12-09 06:41:25

您可以使用 Boost BCP 从 Boost 中提取组件。

You can use Boost BCP to extract components from Boost.

む无字情书 2024-12-09 06:41:25

呵呵,我想我自己做的东西可能比 shared_ptr 更好:

template<typename T>
class auto_
{
    T *pValue;
    mutable const auto_<T> *pPrev, *pNext;

public:
    auto_()           : pValue(new T()),  pPrev(NULL), pNext(NULL) { }
    auto_(T *pValue)  : pValue(pValue),   pPrev(NULL), pNext(NULL) { }
    auto_(const T &v) : pValue(new T(v)), pPrev(NULL), pNext(NULL) { }

    auto_(const auto_<T> &o) : pValue(o.pValue), pPrev(&o), pNext(NULL)
    { o.pNext = this; }

    virtual ~auto_()
    {
        const auto_<T> *const pPrev = this->pPrev, *const pNext = this->pNext;
        if (pPrev != NULL) { pPrev->pNext = pNext; }
        if (pNext != NULL) { pNext->pPrev = pPrev; }
        if (pPrev == NULL && pNext == NULL) { delete this->pValue; }
        this->pPrev = this->pNext = NULL;
        this->pValue = NULL;
    }

    auto_<T>& operator=(const auto_<T>& other)
    {
        if (this != &other)
        {
            this->~auto_();
            this->pValue = other.pValue;
            this->pPrev = &other;
            this->pNext = other.pNext;
            if (other.pNext != NULL) { other.pNext->pPrev = this; }
            other.pNext = this;
        }
        return *this;
    }

    operator   T&() { return *this->pValue; }
    operator   T*() { return  this->pValue; }
    T* operator->() { return  this->pValue; }
    T& operator *() { return *this->pValue; }

    operator   const T&() const { return *this->pValue; }
    operator   const T*() const { return  this->pValue; }
    const T* operator->() const { return  this->pValue; }
    const T& operator *() const { return *this->pValue; }

};

示例用法:

template<typename T>
T recurse(T value, int depth)
{
    if (depth > 0) { T result = recurse(value, depth - 1); return result; }
    else { return value; }
}

auto_<int> test()
{
    printf("Value: %d\n", *recurse(auto_<int>(10), 3));
    auto_<int> p1 = recurse<auto_<int> >(5, 3);
    printf("Value: %d\n", *p1);
    auto_<int> p2 = 3;
    p1 = p2;
    p2 = p1;
    return p2;
}

它看起来比 shared_ptr 更容易使用,恕我直言。

它是否有任何我错过的陷阱(除了明显的线程不安全之外)?

任何(建设性的)批评表示赞赏。

Huh, I guess something I made myself might be even better than shared_ptr:

template<typename T>
class auto_
{
    T *pValue;
    mutable const auto_<T> *pPrev, *pNext;

public:
    auto_()           : pValue(new T()),  pPrev(NULL), pNext(NULL) { }
    auto_(T *pValue)  : pValue(pValue),   pPrev(NULL), pNext(NULL) { }
    auto_(const T &v) : pValue(new T(v)), pPrev(NULL), pNext(NULL) { }

    auto_(const auto_<T> &o) : pValue(o.pValue), pPrev(&o), pNext(NULL)
    { o.pNext = this; }

    virtual ~auto_()
    {
        const auto_<T> *const pPrev = this->pPrev, *const pNext = this->pNext;
        if (pPrev != NULL) { pPrev->pNext = pNext; }
        if (pNext != NULL) { pNext->pPrev = pPrev; }
        if (pPrev == NULL && pNext == NULL) { delete this->pValue; }
        this->pPrev = this->pNext = NULL;
        this->pValue = NULL;
    }

    auto_<T>& operator=(const auto_<T>& other)
    {
        if (this != &other)
        {
            this->~auto_();
            this->pValue = other.pValue;
            this->pPrev = &other;
            this->pNext = other.pNext;
            if (other.pNext != NULL) { other.pNext->pPrev = this; }
            other.pNext = this;
        }
        return *this;
    }

    operator   T&() { return *this->pValue; }
    operator   T*() { return  this->pValue; }
    T* operator->() { return  this->pValue; }
    T& operator *() { return *this->pValue; }

    operator   const T&() const { return *this->pValue; }
    operator   const T*() const { return  this->pValue; }
    const T* operator->() const { return  this->pValue; }
    const T& operator *() const { return *this->pValue; }

};

Sample usage:

template<typename T>
T recurse(T value, int depth)
{
    if (depth > 0) { T result = recurse(value, depth - 1); return result; }
    else { return value; }
}

auto_<int> test()
{
    printf("Value: %d\n", *recurse(auto_<int>(10), 3));
    auto_<int> p1 = recurse<auto_<int> >(5, 3);
    printf("Value: %d\n", *p1);
    auto_<int> p2 = 3;
    p1 = p2;
    p2 = p1;
    return p2;
}

It looks easier to use than shared_ptr, IMHO.

Does it have any pitfalls which I missed (aside from the obvious thread-unsafety)?

Any (constructive) criticism appreciated.

两人的回忆 2024-12-09 06:41:25

我自己一直在寻找这样的东西——像你一样,我有一个项目,其中包含大量的 Boost 是完全不可接受的。

我发现了这个:

http://www. lri.fr/~marc/EO/eo/doc/html/shared__ptr_8h-source.html

我不知道代码质量,因为它是 GPL2,这意味着我不能在我的专有代码中使用它,但它似乎没有依赖性。但这似乎确实是你问题的答案。

I've been looking for such a thing myself --- like you, I have a project where including huge wads of Boost is totally unacceptable.

I've found this:

http://www.lri.fr/~marc/EO/eo/doc/html/shared__ptr_8h-source.html

I have no idea of the code quality, as it's GPL2 which means I can't use it in my proprietary code, but it seems to have no dependencies. But it does seem to be an answer to your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文