New Cycript versions do not toll-free bridge well.
[cydget.git] / Handle.hpp
1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #ifndef HANDLE_HPP
39 #define HANDLE_HPP
40
41 template <typename Type_>
42 class _H {
43 typedef _H<Type_> This_;
44
45 private:
46 Type_ *value_;
47
48 _finline void Retain_() {
49 if (value_ != nil)
50 CFRetain((CFTypeRef) value_);
51 }
52
53 _finline void Clear_() {
54 if (value_ != nil)
55 CFRelease((CFTypeRef) value_);
56 }
57
58 public:
59 _finline _H(const This_ &rhs) :
60 value_(rhs.value_ == nil ? nil : (Type_ *) CFRetain((CFTypeRef) rhs.value_))
61 {
62 }
63
64 _finline _H(Type_ *value = NULL, bool mended = false) :
65 value_(value)
66 {
67 if (!mended)
68 Retain_();
69 }
70
71 _finline ~_H() {
72 Clear_();
73 }
74
75 _finline operator Type_ *() const {
76 return value_;
77 }
78
79 _finline This_ &operator =(Type_ *value) {
80 if (value_ != value) {
81 Type_ *old(value_);
82 value_ = value;
83 Retain_();
84 if (old != nil)
85 CFRelease((CFTypeRef) old);
86 } return *this;
87 }
88 };
89
90 #endif//HANDLE_HPP