File: raii.hpp

package info (click to toggle)
libfplus 0.2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 27,543; javascript: 634; sh: 105; python: 103; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (3)
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
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <fplus/shared_ref.hpp>

namespace fplus
{

// A generic RAII class.
// It is recommended to use make_raii for constructing an instance.
template <typename INIT, typename QUIT>
class raii
{
public:
    raii(INIT init, QUIT quit) :
        quit_(quit)
    {
        init();
    }
    ~raii()
    {
        quit_();
    }
    raii(const raii&) = delete;
    raii(raii&&) = default;
    raii& operator=(const raii&) = delete;
    raii& operator=(raii&&) = default;
private:
    QUIT quit_;
};

template <typename INIT, typename QUIT>
shared_ref<raii<INIT, QUIT>> make_raii(INIT init, QUIT quit)
{
    return make_shared_ref<raii<INIT, QUIT>>(init, quit);
}


} // namespace fplus