xref: /netbsd-src/external/bsd/atf/dist/atf-c++/utils.hpp (revision 1ca06f9c9235889e2ff6dc77279d01d151d70a9a)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #if !defined(_ATF_CXX_UTILS_HPP_)
31 #define _ATF_CXX_UTILS_HPP_
32 
33 #include <cstddef>
34 
35 namespace atf {
36 namespace utils {
37 
38 // ------------------------------------------------------------------------
39 // The "auto_array" class.
40 // ------------------------------------------------------------------------
41 
42 template< class T >
43 class auto_array {
44     T* m_ptr;
45 
46 public:
47     auto_array(T* = NULL) throw();
48     auto_array(auto_array< T >&) throw();
49     ~auto_array(void) throw();
50 
51     T* get(void) throw();
52     T* release(void) throw();
53     void reset(T* = NULL) throw();
54 
55     auto_array< T >& operator=(auto_array< T >&) throw();
56     T& operator[](int) throw();
57 };
58 
59 template< class T >
60 auto_array< T >::auto_array(T* ptr)
61     throw() :
62     m_ptr(ptr)
63 {
64 }
65 
66 template< class T >
67 auto_array< T >::auto_array(auto_array< T >& ptr)
68     throw() :
69     m_ptr(ptr.release())
70 {
71 }
72 
73 template< class T >
74 auto_array< T >::~auto_array(void)
75     throw()
76 {
77     if (m_ptr != NULL)
78         delete [] m_ptr;
79 }
80 
81 template< class T >
82 T*
83 auto_array< T >::get(void)
84     throw()
85 {
86     return m_ptr;
87 }
88 
89 template< class T >
90 T*
91 auto_array< T >::release(void)
92     throw()
93 {
94     T* ptr = m_ptr;
95     m_ptr = NULL;
96     return ptr;
97 }
98 
99 template< class T >
100 void
101 auto_array< T >::reset(T* ptr)
102     throw()
103 {
104     if (m_ptr != NULL)
105         delete [] m_ptr;
106     m_ptr = ptr;
107 }
108 
109 template< class T >
110 auto_array< T >&
111 auto_array< T >::operator=(auto_array< T >& ptr)
112     throw()
113 {
114     reset(ptr.release());
115     return *this;
116 }
117 
118 template< class T >
119 T&
120 auto_array< T >::operator[](int pos)
121     throw()
122 {
123     return m_ptr[pos];
124 }
125 
126 // ------------------------------------------------------------------------
127 // The "noncopyable" class.
128 // ------------------------------------------------------------------------
129 
130 class noncopyable {
131     // The class cannot be empty; otherwise we get ABI-stability warnings
132     // during the build, which will break it due to strict checking.
133     int m_noncopyable_dummy;
134 
135     noncopyable(const noncopyable& nc);
136     noncopyable& operator=(const noncopyable& nc);
137 
138 protected:
139     // Explicitly needed to provide some non-private functions.  Otherwise
140     // we also get some warnings during the build.
141     noncopyable(void) {}
142     ~noncopyable(void) {}
143 };
144 
145 } // namespace utils
146 } // namespace atf
147 
148 #endif // !defined(_ATF_CXX_UTILS_HPP_)
149