Unmanaged mode in RefPtr



Hi,
I've got following problem;                                               
I've got a class Query, which is base class for QueryAllocation. Passing \
QueryAllocation object as Query to a function is pretty simple because of automatic cas\
ting:

void do_sth(const RefPtr<Query>& query);                                  
...
RefPtr<QueryAllocation> alloc_query = QueryAllocation::create ();         
do_sth (alloc_query);                                                     

But... in do_sth method, refcount of my object equals 2 (because casting \
increases ref count, it's obvious). Unfortunately, I have to have refcoun\
t=1 in do_sth method, so that's what I'm doing now:

RefPtr<QueryAllocation> alloc_query = QueryAllocation::create();          
RefPtr<Query> query = alloc_query;                                        
query->unreference();                                                     
do_sth(query);                                                            
query->reference();                                                       
query.reset();                                                            

It's workaround.. that would be great, if I can do something like "unmana\
ged RefPtr". It might look like this:

RefPtr<QueryAllocation> alloc_query = QueryAllocation::create();          
do_sth(RefPtr<Query>(alloc_query, false));                                

And constructor declaration:
template<typename T>
RefPtr(const RefPtr<T>& src, bool manage = true);                         

If 'manage' equals false, RefPtr doesn't increase/decrease refcount durin\
g construction and destruction. I know, it's unsafe, and might lead to a \
problems, if somebody uses it wrong, but it allows me to remove a lot of \
magic from my code.

Of course, it's only conceptual solution, maybe it would be better to do \
single function instead of constructor, ideas are welcome.

Best regards,
Marcin Kolny



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]