Opened 15 years ago

Last modified 14 years ago

#1090 new Feature Requests

scoped_file class that deletes file at end of scope

Reported by: Marcus Lindblom <macke@…> Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Severity: Not Applicable
Keywords: Cc:

Description

I've found myself wanting this on a couple of occasions with 1.33.1, and I've done it with boost::shared_ptr:

boost::shared_ptr<fs::path> removeFile(&file, bind(&fs::remove, boost::cref(file)));

That didn't work with 1.34.1 (maybe due to compiler intricacies), so I did it properly:

class scoped_file {
public:
    explicit scoped_file(const std::string& file) : m_path(file, boost::filesystem::native) {}
    explicit scoped_file(const boost::filesystem::path& file) : m_path(file) {}

    ~scoped_file() { boost::filesystem::remove(m_path); }

private:
    boost::filesystem::path m_path;
};

That might be something to add?

Change History (4)

comment:1 by Marcus Lindblom <macke@…>, 15 years ago

Sorry! The shared_ptr impl should look like this:

boost::shared_ptr<fs::path> removeFile(&file, bind(&fs::remove, boost::cref(file)));

comment:2 by Marshall Clow, 15 years ago

Owner: set to Beman Dawes

comment:3 by Beman Dawes, 14 years ago

Type: PatchesFeature Requests

I'm considering #590 and #1090 as indications of interest and need, but feel that there are various details to be worked out first. mkstemp and similar functions seem pretty dated. The C++ approach suggested by #1090 is appealing, but more work needs to be done. Suggestions welcome.

--Beman

comment:4 by Marcus Lindblom <macke@…>, 14 years ago

What is lacking in the current proposal?

(Also, the c++ markup in the description seems to have failed. Could you edit that?)

Note: See TracTickets for help on using tickets.