The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scoped_resource.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * scoped_resource: class template, functions, helper policies etc.\ for
18  * resource management.
19  */
20 
21 #ifndef SCOPED_RESOURCE_H_INCLUDED
22 #define SCOPED_RESOURCE_H_INCLUDED
23 
24 #include "global.hpp"
25 #include <cstdio> //for FILE
26 
27 namespace util
28 {
29 /**
30 * A class template, scoped_resource, designed to implement
31 * the Resource Acquisition Is Initialization (RAII) approach
32 * to resource management.
33 * scoped_resource is designed to be used when a resource
34 * is initialized at the beginning or middle of a scope,
35 * and released at the end of the scope.
36 * The template argument ReleasePolicy is a functor
37 * which takes an argument of the type of the resource,
38 * and releases it.
39 *
40 * Usage example, for working with files:
41 *
42 * @code
43 * struct close_file { void operator()(int fd) const {close(fd);} };
44 * ...
45 * {
46 * const scoped_resource<int,close_file> file(open("file.txt",O_RDONLY));
47 * read(file, buf, 1000);
48 * } // file is automatically closed here
49 * @endcode
50 *
51 * Note that scoped_resource has an explicit constructor,
52 * and prohibits copy-construction, and thus the initialization syntax.
53 * The assignment syntax must be used when initializing.
54 *
55 * I.e. using scoped_resource<int,close_file> file = open("file.txt",O_RDONLY);
56 * in the above example is illegal.
57 *
58 */
59 template<typename T,typename ReleasePolicy>
61 {
63 
64  //prohibited operations
67 public:
68  typedef T resource_type;
69  typedef ReleasePolicy release_type;
70 
71  /**
72  * Constructor
73  *
74  * @param res This is the resource to be managed
75  */
76  scoped_resource(resource_type res = resource_type())
77  : resource(res) {}
78 
79  /**
80  * The destructor is the main point in this class.
81  * It takes care of proper deletion of the resource,
82  * using the provided release policy.
83  */
84  virtual ~scoped_resource()
85  {
87  }
88 
89  /**
90  * This operator makes sure you can access and use the scoped_resource
91  * just like you were using the resource itself.
92  *
93  * @return the underlying resource
94  */
95  operator resource_type() const { return resource; }
96 
97  /**
98  * This function provides explicit access to the resource.
99  * Its behavior is identical to operator resource_type()
100  *
101  * @return the underlying resource
102  */
103  resource_type get() const { return resource; }
104 
105  /**
106  * This function provides convenient direct access to the -> operator
107  * if the underlying resource is a pointer.
108  * Only call this function if resource_type is a pointer type.
109  */
110  resource_type operator->() const { return resource; }
111 
112  void assign(const resource_type& o) {
114  resource = o;
115  }
116 };
117 
118 /**
119 * A helper policy for scoped_ptr.
120 * It will call the delete operator on a pointer, and assign the pointer to nullptr
121 */
122 struct delete_item {
123  template<typename T>
124  void operator()(T*& p) const { delete p; p = nullptr; }
125 };
126 /**
127 * A helper policy for scoped_array.
128 * It will call the delete[] operator on a pointer, and assign the pointer to nullptr
129 */
130 struct delete_array {
131  template<typename T>
132  void operator()(T*& p) const { delete [] p; p = nullptr; }
133 };
134 
135 /**
136 * A class which implements an approximation of
137 * template<typename T>
138 * typedef scoped_resource<T*,delete_item> scoped_ptr<T>;
139 *
140 * It is a convenient synonym for a common usage of @ref scoped_resource.
141 * See scoped_resource for more details on how this class behaves.
142 *
143 * Usage example:
144 * @code
145 * {
146 * const scoped_ptr<Object> ptr(new Object);
147 * ...use ptr as you would a normal Object*...
148 * } // ptr is automatically deleted here
149 * @endcode
150 *
151 * NOTE: use this class only to manage a single object, *never* an array.
152 * Use scoped_array to manage arrays.
153 * This distinction is because you may call delete only
154 * on objects allocated with new,
155 * delete[] only on objects allocated with new[].
156 */
157 template<typename T>
158 struct scoped_ptr : public scoped_resource<T*,delete_item>
159 {
160  explicit scoped_ptr(T* p) : scoped_resource<T*,delete_item>(p) {}
161 };
162 
163 /**
164 * This class has identical behavior to @ref scoped_ptr, except it
165 * manages heap-allocated arrays instead of heap-allocated single objects
166 *
167 * Usage example:
168 * @code
169 * {
170 * const scoped_array<char> ptr(new char[n]);
171 * ...use ptr as you would a normal char*...
172 * } // ptr is automatically deleted here
173 * @endcode
174 *
175 */
176 template<typename T>
177 struct scoped_array : public scoped_resource<T*,delete_array>
178 {
179  explicit scoped_array(T* p) : scoped_resource<T*,delete_array>(p) {}
180 };
181 
182 /**
183  * This class specializes the scoped_resource to implement scoped FILEs.
184  * Not sure this is the best place to place such an utility, though.
185  */
187 {
188  void operator()(std::FILE* f) const { if(f != nullptr) { std::fclose(f); } }
189 };
191 
192 }
193 
194 #endif
scoped_resource(resource_type res=resource_type())
Constructor.
scoped_resource & operator=(const scoped_resource &)
void operator()(std::FILE *f) const
A helper policy for scoped_ptr.
void operator()(T *&p) const
This class has identical behavior to scoped_ptr, except it manages heap-allocated arrays instead of h...
scoped_resource< std::FILE *, close_FILE > scoped_FILE
void operator()(T *&p) const
A class template, scoped_resource, designed to implement the Resource Acquisition Is Initialization (...
GLfloat GLfloat p
Definition: glew.h:12766
virtual ~scoped_resource()
The destructor is the main point in this class.
A class which implements an approximation of template typedef scoped_resource scoped_ptr;.
GLuint res
Definition: glew.h:9258
resource_type operator->() const
This function provides convenient direct access to the -> operator if the underlying resource is a po...
This class specializes the scoped_resource to implement scoped FILEs.
A helper policy for scoped_array.
void assign(const resource_type &o)
ReleasePolicy release_type
scoped_resource(const scoped_resource &)
GLclampf f
Definition: glew.h:3024