1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 
27 #ifndef kludge_cxx11_h
28 #define kludge_cxx11_h
29 
30 //
31 // This file contains kludge implementation of C++11 features needed to build
32 // jpackage until Open JDK moves forward from C++98 standard.
33 //
34 
35 #ifdef __GNUG__
36 #ifndef __clang__
37 #if __cplusplus < 201103L
38 #define JP_WITH_KLUDGE_CXX11
39 #endif
40 #endif
41 #endif
42 
43 #ifdef JP_WITH_KLUDGE_CXX11
44 
45 #include <algorithm>
46 
47 
48 namespace std {
49 
50 namespace impl {
51 
52 template <typename Tp, typename Dp>
53 class unique_ptr_impl {
54 public:
55     typedef typename Dp::pointer pointer;
56     typedef Tp element_type;
57     typedef Dp deleter_type;
58 
unique_ptr_impl()59     unique_ptr_impl(): value(0) {
60     }
61 
unique_ptr_impl(pointer p)62     unique_ptr_impl(pointer p): value(p) {
63     }
64 
release()65     pointer release() {
66         const pointer retValue = value;
67         value = 0;
68         return retValue;
69     }
70 
swap(unique_ptr_impl & other)71     void swap(unique_ptr_impl& other) {
72         std::swap(value, other.value);
73     }
74 
get()75     pointer get() const {
76         return value;
77     }
78 
79 private:
80     unique_ptr_impl(const unique_ptr_impl&);
81     unique_ptr_impl& operator= (const unique_ptr_impl&);
82 
83 private:
84     pointer value;
85 };
86 
87 } // namespace impl
88 
89 
90 template <typename Tp>
91 struct default_delete {
92     typedef Tp* pointer;
93 
operatordefault_delete94     void operator()(Tp* ptr) const {
95         delete ptr;
96     }
97 };
98 
99 
100 template <typename Tp, typename Dp = default_delete<Tp> >
101 class unique_ptr {
102     typedef impl::unique_ptr_impl<Tp, Dp> impl_type;
103 public:
104     typedef typename impl_type::pointer pointer;
105     typedef typename impl_type::element_type element_type;
106     typedef typename impl_type::deleter_type deleter_type;
107 
unique_ptr()108     unique_ptr() {
109     }
110 
unique_ptr(pointer p)111     unique_ptr(pointer p): impl(p) {
112     }
113 
~unique_ptr()114     ~unique_ptr() {
115         if (get() != 0) {
116             impl_type tmp;
117             tmp.swap(impl);
118             Dp()(tmp.get());
119         }
120     }
121 
release()122     pointer release() {
123         return impl.release();
124     }
125 
swap(unique_ptr & other)126     void swap(unique_ptr& other) {
127         impl.swap(other.impl);
128     }
129 
get()130     pointer get() const {
131         return impl.get();
132     }
133 
134     element_type& operator *() const {
135         return *impl.get();
136     }
137 
138     pointer operator ->() const {
139         return impl.get();
140     }
141 
142 private:
143     impl_type impl;
144 };
145 
146 template <class Ctnr>
begin(const Ctnr & ctnr)147 typename Ctnr::const_iterator begin(const Ctnr& ctnr) {
148     return ctnr.begin();
149 }
150 
151 template <class Ctnr>
begin(Ctnr & ctnr)152 typename Ctnr::iterator begin(Ctnr& ctnr) {
153     return ctnr.begin();
154 }
155 
156 template <class Ctnr>
end(const Ctnr & ctnr)157 typename Ctnr::const_iterator end(const Ctnr& ctnr) {
158     return ctnr.end();
159 }
160 
161 template <class Ctnr>
end(Ctnr & ctnr)162 typename Ctnr::iterator end(Ctnr& ctnr) {
163     return ctnr.end();
164 }
165 
166 } // namespace std
167 
168 #endif // #ifdef JP_WITH_KLUDGE_CXX11
169 
170 #endif // #ifndef kludge_cxx11_h
171