Re: [Boost-users] Suggestion for boost`s smart pointers

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of mark cox Sent: Tuesday, November 28, 2006 2:37 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Suggestion for boost`s smart pointers
wow, this seems like a great idea, a scoped_ptr with deleter would be v.useful. mark
I've been thinking something like (just something I hacked together just
now). Not sure if there is a nicer way.
#include <string>
#include <cstdio>
#include <iostream>
template<typename T>
struct DeletingDeleter
{
DeletingDeleter(T * p)
{
delete p;
std::cout << "DeletingDeleter" << std::endl;
}
};
struct FileCloser
{
FileCloser(FILE * f)
{
std::cout << "Closing file" << std::endl;
fclose(f);
}
};
template

Sohail Somani wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of mark cox Sent: Tuesday, November 28, 2006 2:37 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Suggestion for boost`s smart pointers
wow, this seems like a great idea, a scoped_ptr with deleter would be v.useful. mark
I've been thinking something like (just something I hacked together just now). Not sure if there is a nicer way.
#include <string> #include <cstdio> #include <iostream>
template<typename T> struct DeletingDeleter { DeletingDeleter(T * p) { delete p; std::cout << "DeletingDeleter" << std::endl; } };
struct FileCloser { FileCloser(FILE * f) { std::cout << "Closing file" << std::endl; fclose(f); } };
template
struct scoped_ptr { scoped_ptr():p_(0){} scoped_ptr(T * p):p_(p){} ~scoped_ptr() { Deleter d(p_); } T * get() {return p_;} // regular stuff private: T * p_; }; typedef scoped_ptr
file_ptr; int main() { scoped_ptr<int> a; // maintain old interface scoped_ptr<int> b(new int(5)); file_ptr p(fopen("myfile.txt","w")); fprintf(p.get(),"Hello world!\n"); } _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
+1 vote for this idea :) i actually kind of did this for my current project, because i needed a custom deleter for scoped_ptr, when shared_ptr was to much overhead... -- regards dave

Sohail Somani
[...] template<typename T> struct DeletingDeleter { [...] };
struct FileCloser { [...] };
template
struct scoped_ptr { [...] }; typedef scoped_ptr
file_ptr;
I also tried to make my own raii strategy, and as in your example above, I put the deleter as part of the template parameters, directly in the smart pointer interface. The side effect of it is that if you use a boost binder that on its turn uses one or more template functions or classes, you end up with many lines to describe the template specializations (or call it "template prototyping"), or as in your case, many lines of extra structure and classes. (I wished the new "auto" feature in C++0x existed!) Still, the method used by shared_ptr<> is very useful: it hides the deleter in a template class the derives from a normal base class that is a member of the shared_ptr<>. So when you defines the smart pointer with a deleter, you avoid describing the full specialization. That is the reason why I suggested to reuse the shared_ptr<> astuce in scoped_ptr<>.

On Nov 29, 2006, at 11:45 AM, Maitre Bart wrote:
Still, the method used by shared_ptr<> is very useful: it hides the deleter in a template class the derives from a normal base class that is a member of the shared_ptr<>. So when you defines the smart pointer with a deleter, you avoid describing the full specialization.
That is the reason why I suggested to reuse the shared_ptr<> astuce in scoped_ptr<>.
You might want to check out Jonathan Turkanis' dynamic_move_ptr: http://home.comcast.net/~jturkanis/move_ptr/ which does this. It costs an associated size swelling, but can be handy if you're willing to pay for it. Fwiw, here's a poor man's version of that using unique_ptr (or Jonathan's static_move_ptr): typedef unique_ptr PtrType; Now you can use PtrType with any function pointer. You just have to supply the function pointer in the constructor. Template aliases would make this a little nicer (or a templated wrapper class). It's a compromise between a fully dynamic deleter (like shared_ptr) and a fully static deleter (like unique_ptr). The cost is sizeof(PtrType) is two words, but there's no extra internal allocation. -Howard

Howard Hinnant
On Nov 29, 2006, at 11:45 AM, Maitre Bart wrote: [...] You might want to check out Jonathan Turkanis' dynamic_move_ptr: http://home.comcast.net/~jturkanis/move_ptr/ [...]
Looks promising! Thanks for all the info.

Sohail Somani wrote:
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of mark cox Sent: Tuesday, November 28, 2006 2:37 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Suggestion for boost`s smart pointers
wow, this seems like a great idea, a scoped_ptr with deleter would be v.useful. mark
I've been thinking something like (just something I hacked together just now). Not sure if there is a nicer way.
#include <string> #include <cstdio> #include <iostream>
template<typename T> struct DeletingDeleter { DeletingDeleter(T * p) { delete p; std::cout << "DeletingDeleter" << std::endl; } };
struct FileCloser { FileCloser(FILE * f) { std::cout << "Closing file" << std::endl; fclose(f); } };
template
struct scoped_ptr { scoped_ptr():p_(0){} scoped_ptr(T * p):p_(p){} ~scoped_ptr() { Deleter d(p_); } T * get() {return p_;} // regular stuff private: T * p_; }; typedef scoped_ptr
file_ptr; int main() { scoped_ptr<int> a; // maintain old interface scoped_ptr<int> b(new int(5)); file_ptr p(fopen("myfile.txt","w")); fprintf(p.get(),"Hello world!\n"); } sorry for such late response, i think you should also consider Andrei and Petru's ScopeGuard from http://www.ddj.com/dept/cpp/184403758
participants (5)
-
David Klein
-
Howard Hinnant
-
Ilya Sokolov
-
Maitre Bart
-
Sohail Somani