~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to include/shared/mir/raii.h

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
Import upstream version 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2013 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU Lesser General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU Lesser General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
17
 
 */
18
 
 
19
 
#ifndef MIR_RAII_H_
20
 
#define MIR_RAII_H_
21
 
 
22
 
#include <memory>
23
 
#include <type_traits>
24
 
 
25
 
namespace mir
26
 
{
27
 
/// Utilities for exception safe use of paired function calls
28
 
namespace raii
29
 
{
30
 
template <typename Creator, typename Deleter>
31
 
struct PairedCalls
32
 
{
33
 
    PairedCalls(Creator&& creator, Deleter&& deleter) : deleter(std::move(deleter)), owner(true) { creator(); }
34
 
    PairedCalls(PairedCalls&& that) : deleter(that.deleter), owner(that.owner) { that.owner = false; }
35
 
    ~PairedCalls()  { if (owner) deleter(); }
36
 
private:
37
 
    PairedCalls(PairedCalls const& that) = delete;
38
 
    PairedCalls& operator=(PairedCalls const& that) = delete;
39
 
    Deleter const deleter;
40
 
    bool owner;
41
 
};
42
 
 
43
 
/**
44
 
 * Creates an RAII object from a creator and deleter.
45
 
 * If creator returns a pointer type then the returned object
46
 
 * is a std::unique_ptr initialized with the pointer and deleter.
47
 
 * Otherwise, the returned object calls creator on construction and deleter on destruction
48
 
 *
49
 
 * \param creator called to initialize the returned object
50
 
 * \param deleter called to finalize the returned object
51
 
 */
52
 
template <typename Creator, typename Deleter>
53
 
inline auto paired_calls(Creator&& creator, Deleter&& deleter)
54
 
-> std::unique_ptr<typename std::remove_reference<decltype(*creator())>::type, Deleter>
55
 
{
56
 
    return {creator(), deleter};
57
 
}
58
 
 
59
 
///\overload
60
 
template <typename Creator, typename Deleter>
61
 
inline auto paired_calls(Creator&& creator, Deleter&& deleter)
62
 
-> typename std::enable_if<
63
 
    std::is_void<decltype(creator())>::value,
64
 
    PairedCalls<Creator, Deleter>>::type
65
 
{
66
 
    return {std::move(creator), std::move(deleter)};
67
 
}
68
 
 
69
 
/**
70
 
 * Creates an RAII object from an owning pointer and deleter.
71
 
 * The returned object is a std::unique_ptr initialized with the pointer and deleter.
72
 
 *
73
 
 * \param owned   the object to take ownership of
74
 
 * \param deleter called to finalize the owned object
75
 
 */
76
 
template <typename Owned, typename Deleter>
77
 
inline auto deleter_for(Owned* owned, Deleter&& deleter)
78
 
-> std::unique_ptr<Owned, Deleter>
79
 
{
80
 
    return {owned, deleter};
81
 
}
82
 
}
83
 
}
84
 
 
85
 
#endif /* MIR_RAII_H_ */