替代方案,以提高:: shared_ptr的在嵌入式环境 [英] Alternative to boost::shared_ptr in an embedded environment
问题描述
我在里面有一个嵌入式Linux环境中使用C ++ GCC版本2.95
。
I'm using C++ in an embedded linux environment which has GCC version 2.95
.
我只是不能提取使用bcp 的boost :: shared_ptr的
文件,它仅仅是太重了。
I just can't extract boost::shared_ptr
files with bcp, it is just too heavy.
我想这将是一个简单的智能指针执行的boost :: shared_ptr的
,但没有全部升压
开销(如果它是可能的...)。
What I'd like would be a simple smart pointer implementation of boost::shared_ptr
but without all boost
overheads (if it is possible...).
我可以拿出我自己的版本的阅读刺激源,但我生怕漏掉一个或多个点,似乎容易使出现故障的智能指针,我不能有一个越野车的实现。
I could come up with my own version reading boost source but I fear missing one or more points, it seems easy to make a faulty smart pointer and I can't afford to have a buggy implementation.
那么,做的的boost :: shared_ptr的
(或等值计数智能指针引用)的存在,我可以使用,或者我可以一个简单的实现或实现的例子采取为灵感?
So, does a "simple" implementation or implementation example of boost::shared_ptr
(or any reference counting equivalent smart pointer) exists that I could use or that I could take as an inspiration?
推荐答案
如果你并不需要混合的共享的和的弱的PTR,并且不需要的 coustom deletors 的,你可以只用快速和肮脏的my_shared_ptr:
if you don't need mixing shared and weak ptr,and don't need coustom deletors, you can just use the quick and dirty my_shared_ptr:
template<class T>
class my_shared_ptr
{
template<class U>
friend class my_shared_ptr;
public:
my_shared_ptr() :p(), c() {}
explicit my_shared_ptr(T* s) :p(s), c(new unsigned(1)) {}
my_shared_ptr(const my_shared_ptr& s) :p(s.p), c(s.c) { if(c) ++*c; }
my_shared_ptr& operator=(const my_shared_ptr& s)
{ if(this!=&s) { clear(); p=s.p; c=s.c; if(c) ++*c; } return *this; }
template<class U>
my_shared_ptr(const my_shared_ptr<U>& s) :p(s.p), c(s.c) { if(c) ++*c; }
~my_shared_ptr() { clear(); }
void clear()
{
if(c)
{
if(*c==1) delete p;
if(!--*c) delete c;
}
c=0; p=0;
}
T* get() const { return (c)? p: 0; }
T* operator->() const { return get(); }
T& operator*() const { return *get(); }
private:
T* p;
unsigned* c;
}
这篇关于替代方案,以提高:: shared_ptr的在嵌入式环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!