rippled
temp_dir.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_UTILITY_TEMP_DIR_H_INCLUDED
21 #define BEAST_UTILITY_TEMP_DIR_H_INCLUDED
22 
23 #include <boost/filesystem.hpp>
24 #include <string>
25 
26 namespace beast {
27 
33 class temp_dir
34 {
35  boost::filesystem::path path_;
36 
37 public:
38 #if !GENERATING_DOCS
39  temp_dir(const temp_dir&) = delete;
40  temp_dir&
41  operator=(const temp_dir&) = delete;
42 #endif
43 
46  {
47  auto const dir = boost::filesystem::temp_directory_path();
48  do
49  {
50  path_ = dir / boost::filesystem::unique_path();
51  } while (boost::filesystem::exists(path_));
52  boost::filesystem::create_directory(path_);
53  }
54 
57  {
58  // use non-throwing calls in the destructor
59  boost::system::error_code ec;
60  boost::filesystem::remove_all(path_, ec);
61  // TODO: warn/notify if ec set ?
62  }
63 
66  path() const
67  {
68  return path_.string();
69  }
70 
76  file(std::string const& name) const
77  {
78  return (path_ / name).string();
79  }
80 };
81 
82 } // namespace beast
83 
84 #endif
beast::temp_dir::path_
boost::filesystem::path path_
Definition: temp_dir.h:35
std::string
STL class.
beast::temp_dir::~temp_dir
~temp_dir()
Destroy a temporary directory.
Definition: temp_dir.h:56
beast::temp_dir::path
std::string path() const
Get the native path for the temporary directory.
Definition: temp_dir.h:66
beast::temp_dir::temp_dir
temp_dir()
Construct a temporary directory.
Definition: temp_dir.h:45
beast::temp_dir::file
std::string file(std::string const &name) const
Get the native path for the a file.
Definition: temp_dir.h:76
beast::temp_dir
RAII temporary directory.
Definition: temp_dir.h:33
beast
Definition: base_uint.h:641
string