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>
52  struct AutoResourceRef
53  {
54  typedef typename Policy::handle_type ref_type;
55  ref_type hdl;
56 
57  explicit AutoResourceRef(ref_type h)
58  : hdl(h)
59  {
60  }
61  };
62 
96  template <typename Policy>
97  class AutoResource
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  //
124  AutoResource(const AutoResource & x) // throw()
125  : hdl(const_cast<AutoResource &>(x).release())
126  {
127  }
128 #else
129  //
131  AutoResource(AutoResource & x) // throw()
132  : hdl(x.release())
133  {
134  }
135 #endif
136 
137 #if defined __HP_aCC
138  //
140  AutoResource & operator=(const AutoResource & x) // throw()
141  {
142  reset(const_cast<AutoResource &>(x).release());
143  return *this;
144  }
145 #else
146  //
148  AutoResource & operator=(AutoResource & x) // throw()
149  {
150  reset(x.release());
151  return *this;
152  }
153 #endif
154  //
156  ~AutoResource() // throw()
157  {
158  Policy::free(hdl);
159  }
160 
161  BLOCXX_SAFE_BOOL_IMPL(AutoResource<Policy>, handle_type, AutoResource<Policy>::hdl, (hdl != Policy::null()));
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 
202  AutoResource(AutoResourceRef<Policy> href) // throw()
203  : hdl(href.hdl)
204  {
205  }
206 
211  operator AutoResourceRef<Policy>() // throw()
212  {
213  return AutoResourceRef<Policy>(this->release());
214  }
215 
223  AutoResource & operator=(AutoResourceRef<Policy> href) // throw()
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
BLOCXX_SAFE_BOOL_IMPL
#define BLOCXX_SAFE_BOOL_IMPL(classname, type, variable, test)
Definition: SafeBool.hpp:88
BLOCXX_NAMESPACE::AutoResourceRef::hdl
ref_type hdl
Definition: AutoResource.hpp:117
SafeBool.hpp
BLOCXX_NAMESPACE
Taken from RFC 1321.
Definition: AppenderLogger.cpp:48
BLOCXX_NAMESPACE::AutoResource::AutoResource
AutoResource()
Default (no argument) ctor initializes with value indicating no resource currently owned.
Definition: AutoResource.hpp:139
BLOCXX_NAMESPACE::AutoResource
PURPOSE: The AutoResource class template is an analog of std::auto_ptr for managing arbitrary resourc...
Definition: AutoResource.hpp:129
BLOCXX_NAMESPACE::AutoResource::get
handle_type get() const
Return handle of resource, retaining ownership.
Definition: AutoResource.hpp:196
BLOCXX_NAMESPACE::AutoResourceRef::AutoResourceRef
AutoResourceRef(ref_type h)
Definition: AutoResource.hpp:119
BLOCXX_NAMESPACE::AutoResource::handle_type
Policy::handle_type handle_type
Definition: AutoResource.hpp:130
BLOCXX_NAMESPACE::AutoResource::~AutoResource
~AutoResource()
Free resource when AutoResource object reaches end of lifetime.
Definition: AutoResource.hpp:187
BLOCXX_NAMESPACE::AutoResource::operator=
AutoResource & operator=(AutoResource &x)
Assignment takes over ownership of resource owned by x.
Definition: AutoResource.hpp:179
BLOCXX_NAMESPACE::AutoResource::reset
void reset()
Definition: AutoResource.hpp:224
BLOCXX_NAMESPACE::AutoResource::release
handle_type release()
Relinquish ownership of resource and return its handle.
Definition: AutoResource.hpp:203
BLOCXX_NAMESPACE::AutoResource::hdl
handle_type hdl
Definition: AutoResource.hpp:131
BLOCXX_NAMESPACE::AutoResourceRef
Utility class used in implementing AutoResource operations.
Definition: AutoResource.hpp:84
BLOCXX_NAMESPACE::AutoResourceRef::ref_type
Policy::handle_type ref_type
Definition: AutoResource.hpp:116