Skip to content

Commit

Permalink
Convert wrapper for tempdirectory to singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsaenger committed Oct 11, 2024
1 parent 2add1fd commit 33cf77f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
41 changes: 24 additions & 17 deletions vipster/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@
using namespace Vipster;
namespace fs = std::filesystem;

detail::TempWrap::TempWrap()
: tmppath{fs::temp_directory_path()/"vipster"}
{
if(!fs::exists(tmppath)){
fs::create_directory(tmppath);
}else if(!fs::is_directory(tmppath)){
fs::remove(tmppath);
fs::create_directory(tmppath);
}
}

const fs::path& detail::TempWrap::getPath() const
{
return tmppath;
// RAII wrapper singleton for temp folder
namespace Vipster::detail {
class TempWrap{
public:
static const std::filesystem::path& getPath()
{
static TempWrap tw{};
return tw.tmppath;
}
private:
TempWrap()
: tmppath{fs::temp_directory_path()/"vipster"}
{
if(!fs::exists(tmppath)){
fs::create_directory(tmppath);
}else if(!fs::is_directory(tmppath)){
fs::remove(tmppath);
fs::create_directory(tmppath);
}
}
TempWrap(const TempWrap&) = delete;
std::filesystem::path tmppath;
};
}

const fs::path& Vipster::getTempPath()
{
return detail::tempwrap.getPath();
return detail::TempWrap::getPath();
}

const detail::TempWrap detail::tempwrap{};

const Plugin *Vipster::guessFmt(std::string fn, const PluginList &p)
{
auto pos = fn.find_last_of('.');
Expand Down
13 changes: 1 addition & 12 deletions vipster/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ namespace Vipster{
const std::optional<Parameter>& p=std::nullopt,
const std::optional<Preset>& c=std::nullopt);
const Plugin* guessFmt(std::string fn, const PluginList &p);
// RAII wrapper for temp folder
namespace detail {
class TempWrap{
public:
TempWrap();
const std::filesystem::path& getPath() const;
private:
TempWrap(const TempWrap&) = delete;
std::filesystem::path tmppath;
};
extern const TempWrap tempwrap;
}

const std::filesystem::path& getTempPath();
}

Expand Down

0 comments on commit 33cf77f

Please sign in to comment.