1 /**
2  * @copyright
3  * ====================================================================
4  *    Licensed to the Apache Software Foundation (ASF) under one
5  *    or more contributor license agreements.  See the NOTICE file
6  *    distributed with this work for additional information
7  *    regarding copyright ownership.  The ASF licenses this file
8  *    to you under the Apache License, Version 2.0 (the
9  *    "License"); you may not use this file except in compliance
10  *    with the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *    Unless required by applicable law or agreed to in writing,
15  *    software distributed under the License is distributed on an
16  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  *    KIND, either express or implied.  See the License for the
18  *    specific language governing permissions and limitations
19  *    under the License.
20  * ====================================================================
21  * @endcopyright
22  */
23 
24 #ifndef SVN_JAVAHL_CXX_COMPAT_HPP
25 #define SVN_JAVAHL_CXX_COMPAT_HPP
26 
27 #include <memory>
28 
29 namespace JavaHL {
30 namespace cxx {
31 
32 #if defined(__cplusplus) && __cplusplus >= 201103L
33 /*
34  * C++11 and later
35  */
36 
37 // Use std::unique_ptr for exclusive ownership.
38 template<
39   typename T,
40   typename D = ::std::default_delete<T>
41   >
42 using owned_ptr = ::std::unique_ptr<T, D>;
43 
44 // Use std::move for transfering ownership of owned pointers.
45 using ::std::move;
46 
47 #else
48 /*
49  * C++03 and earlier
50  */
51 
52 // Use a dumbed-down std::auto_ptr for exclusive ownership.
53 template<class T>
54 class owned_ptr : public ::std::auto_ptr<T>
55 {
56   typedef ::std::auto_ptr<T> auto_ptr;
57   typedef ::std::auto_ptr_ref<T> auto_ptr_ref;
58 
59 public:
60   explicit owned_ptr(T *p = 0) throw()
61     : auto_ptr(p)
62     {}
63 
64   owned_ptr(owned_ptr& r) throw()
65     : auto_ptr(r)
66     {}
67 
68   owned_ptr(auto_ptr_ref r) throw()
69     : auto_ptr(r)
70     {}
71 
72   owned_ptr& operator=(owned_ptr& r) throw()
73     {
74       static_cast<auto_ptr&>(*this) = r;
75       return *this;
76     }
77 
78   owned_ptr& operator=(auto_ptr_ref r) throw()
79     {
80       static_cast<auto_ptr&>(*this) = r;
81       return *this;
82     }
83 };
84 
85 // Fake std::move since there are no rvalue references.
86 template<class T>
87 T& move(T& t)
88 {
89   return t;
90 }
91 
92 #endif
93 
94 } // namespace cxx
95 } // namespace JavaHL
96 
97 #endif // SVN_JAVAHL_CXX_COMPAT_HPP
98