基本上我需要对不直接等同于指针/地址语义的某些资源(如整数索引)进行引用计数;基本上我需要传递资源,并在计数达到零时调用某些自定义函数。此外,对资源的读/写访问不是简单的指针取消引用操作,而是更复杂的操作。我不认为 boost::shared_ptr 适合这里的账单,但也许我错过了我可能使用的其他一些 boost 等效类?
我需要做的例子:
struct NonPointerResource
{
NonPointerResource(int a) : rec(a) {}
int rec;
}
int createResource ()
{
data BasicResource("get/resource");
boost::shared_resource< MonPointerResource > r( BasicResource.getId() ,
boost::function< BasicResource::RemoveId >() );
TypicalUsage( r );
}
//when r goes out of scope, it will call BasicResource::RemoveId( NonPointerResource& ) or something similar
int TypicalUsage( boost::shared_resource< NonPointerResource > r )
{
data* d = access_object( r );
// do something with d
}