aboutsummaryrefslogtreecommitdiff
path: root/libcutl/fs/auto-remove.hxx
blob: 1badfa7dcb480a9aaf4fdf9b33ceafdd53a50ff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// file      : libcutl/fs/auto-remove.hxx
// license   : MIT; see accompanying LICENSE file

#ifndef LIBCUTL_FS_AUTO_REMOVE_HXX
#define LIBCUTL_FS_AUTO_REMOVE_HXX

#include <vector>
#include <utility>

#include <libcutl/fs/path.hxx>
#include <libcutl/fs/exception.hxx>

#include <libcutl/export.hxx>

namespace cutl
{
  namespace fs
  {
    // Remove a file or an empty directory on destruction unless canceled.
    //
    struct LIBCUTL_EXPORT auto_remove
    {
      auto_remove ()
          : canceled_ (true)
      {
      }

      explicit
      auto_remove (path const& p)
          : path_ (p), canceled_ (false)
      {
      }

      ~auto_remove ();

      void
      cancel ()
      {
        canceled_ = true;
      }

      // Movable-only type. Move-assignment cancels the lhs object.
      //
      auto_remove (auto_remove&& x)
          : path_ (std::move (x.path_)), canceled_ (x.canceled_)
      {
        x.canceled_ = true;
      }

      auto_remove&
      operator= (auto_remove&& x)
      {
        if (this != &x)
        {
          path_ = std::move (x.path_);
          canceled_ = x.canceled_;
          x.canceled_ = true;
        }

        return *this;
      }

      auto_remove (auto_remove const&) = delete;
      auto_remove& operator= (auto_remove const&) = delete;

    private:
      path path_;
      bool canceled_;
    };

    // Remove a list of file or aempty directories on destruction unless
    // canceled.
    //
    struct LIBCUTL_EXPORT auto_removes
    {
      auto_removes (): canceled_ (false) {}
      ~auto_removes ();

      void
      add (path const& p)
      {
        paths_.push_back (p);
      }

      void
      cancel ()
      {
        canceled_ = true;
      }

    private:
      auto_removes (auto_removes const&);

      auto_removes&
      operator= (auto_removes const&);

    private:
      typedef std::vector<path> paths;

      paths paths_;
      bool canceled_;
    };
  }
}

#endif // LIBCUTL_FS_AUTO_REMOVE_HXX