blocxx
AutoResource.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Quest Software, Inc.,
15 * nor Novell, Inc.,
16 * nor Network Associates,
17 * nor the names of its contributors or employees may be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33 
34 
35 #ifndef BLOCXX_AUTO_RESOURCE_HPP_INCLUDE_GUARD_
36 #define BLOCXX_AUTO_RESOURCE_HPP_INCLUDE_GUARD_
37 
42 #include "blocxx/BLOCXX_config.h"
43 #include "blocxx/SafeBool.hpp"
44 
45 namespace BLOCXX_NAMESPACE
46 {
47 
51  template <typename Policy>
53  {
54  typedef typename Policy::handle_type ref_type;
56 
58  : hdl(h)
59  {
60  }
61  };
62 
96  template <typename Policy>
98  {
99  typedef typename Policy::handle_type handle_type;
101 
102  public:
103 
108  AutoResource() // throw()
109  : hdl(Policy::null())
110  {
111  }
112 
116  explicit AutoResource(handle_type h) // throw()
117  : hdl(h)
118  {
119  }
120 
121 #if defined __HP_aCC
122 
123  //
124  AutoResource(const AutoResource & x) // throw()
125  : hdl(const_cast<AutoResource &>(x).release())
126  {
127  }
128 #else
129 
130  //
131  AutoResource(AutoResource & x) // throw()
132  : hdl(x.release())
133  {
134  }
135 #endif
136 
137 #if defined __HP_aCC
138 
139  //
140  AutoResource & operator=(const AutoResource & x) // throw()
141  {
142  reset(const_cast<AutoResource &>(x).release());
143  return *this;
144  }
145 #else
146 
147  //
149  {
150  reset(x.release());
151  return *this;
152  }
153 #endif
154 
155  //
156  ~AutoResource() // throw()
157  {
158  Policy::free(hdl);
159  }
160 
162 
164  //
165  handle_type get() const // throw()
166  {
167  return hdl;
168  }
169 
171  //
172  handle_type release() // throw()
173  {
174  handle_type h = hdl;
175  hdl = Policy::null();
176  return h;
177  }
178 
185  void reset(handle_type h) // throw()
186  {
187  if (!Policy::equal(h, hdl)) {
188  Policy::free(hdl);
189  hdl = h;
190  }
191  }
192 
193  void reset() // throw()
194  {
195  reset(Policy::null());
196  }
197 
203  : hdl(href.hdl)
204  {
205  }
206 
211  operator AutoResourceRef<Policy>() // throw()
212  {
213  return AutoResourceRef<Policy>(this->release());
214  }
215 
224  {
225  if (!Policy::equal(href.hdl, this->get()))
226  {
227  Policy::free(hdl);
228  hdl = href.hdl;
229  }
230  return *this;
231  }
232 
233  };
234 
235 } // namespace BLOCXX_NAMESPACE
236 
237 #endif