TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / common / scope_ptr.h
1 // Copyright 2014 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11
12 #pragma once
13
14 #include "anbox/common/type_traits.h"
15
16 #include <memory>
17 #include <type_traits>
18 #include <utility>
19
20 #include <stdlib.h>
21
22 namespace anbox {
23 namespace common {
24
25 struct FreeDelete {
26   template <class T>
27   void operator()(T ptr) const {
28     free(ptr);
29   }
30 };
31
32 template <class Func>
33 struct FuncDelete {
34   explicit FuncDelete(Func f = {}) : mF(f) {}
35
36   FuncDelete(const FuncDelete& other) = default;
37   FuncDelete(FuncDelete&& other) = default;
38   FuncDelete& operator=(const FuncDelete& other) = default;
39   FuncDelete& operator=(FuncDelete&& other) = default;
40
41   // To be able to copy/move from all compatible template instantiations.
42   template <class U>
43   friend struct FuncDelete;
44
45   // Template constructors and move assignment from compatible instantiations.
46   template <class U>
47   FuncDelete(const FuncDelete<U>& other) : mF(other.mF) {}
48   template <class U>
49   FuncDelete(FuncDelete<U>&& other) : mF(std::move(other.mF)) {}
50   template <class U>
51   FuncDelete& operator=(const FuncDelete<U>& other) {
52     mF = other.mF;
53     return *this;
54   }
55   template <class U>
56   FuncDelete& operator=(FuncDelete<U>&& other) {
57     mF = std::move(other.mF);
58     return *this;
59   }
60
61   // This is the actual deleter call.
62   template <class T>
63   void operator()(T t) const {
64     mF(t);
65   }
66
67  private:
68   Func mF;
69 };
70
71 template <class T, class Deleter = std::default_delete<T>>
72 using ScopedPtr = std::unique_ptr<T, Deleter>;
73
74 template <class T>
75 using ScopedCPtr = std::unique_ptr<T, FreeDelete>;
76
77 template <class T, class Func>
78 using ScopedCustomPtr = std::unique_ptr<T, FuncDelete<Func>>;
79
80 // A factory function that creates a scoped pointer with |deleter|
81 // function used as a deleter - it is called at the scope exit.
82 // Note: enable_if<> limits the scope of allowed arguments to pointers and
83 //  std::nullptr_t (to allow makeCustomScopedPtr(nullptr, ...) calls).
84 template <class T,
85           class Func,
86           class = enable_if_c<std::is_same<T, std::nullptr_t>::value ||
87                               std::is_pointer<T>::value>>
88 ScopedCustomPtr<
89     typename std::decay<typename std::remove_pointer<T>::type>::type,
90     typename std::decay<Func>::type>
91 makeCustomScopedPtr(T data, Func deleter) {
92   return ScopedCustomPtr<
93       typename std::decay<typename std::remove_pointer<T>::type>::type,
94       typename std::decay<Func>::type>(
95       data, FuncDelete<typename std::decay<Func>::type>(deleter));
96 }
97
98 }  // namespace common
99 }  // namespace anbox