/usr/lib/llvm-3.4/include/lldb/Utility/CleanUp.h is in liblldb-3.4-dev 1:3.4.2-13.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | //===-- CleanUp.h -----------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_CleanUp_h_
#define liblldb_CleanUp_h_
#include "lldb/lldb-public.h"
namespace lldb_utility {
//----------------------------------------------------------------------
// Templated class that guarantees that a cleanup callback function will
// be called. The cleanup function will be called once under the
// following conditions:
// - when the object goes out of scope
// - when the user explicitly calls clean.
// - the current value will be cleaned up when a new value is set using
// set(T value) as long as the current value hasn't already been cleaned.
//
// This class is designed to be used with simple types for type T (like
// file descriptors, opaque handles, pointers, etc). If more complex
// type T objects are desired, we need to probably specialize this class
// to take "const T&" for all input T parameters. Yet if a type T is
// complex already it might be better to build the cleanup funcionality
// into T.
//
// The cleanup function must take one argument that is of type T.
// The calback function return type is R. The return value is currently
// needed for "CallbackType". If there is an easy way to get around the
// need for the return value we can change this class.
//
// The two template parameters are:
// T - The variable type of value that will be stored and used as the
// sole argument for the cleanup callback.
// R - The return type for the cleanup function.
//
// EXAMPLES
// // Use with file handles that get opened where you want to close
// // them. Below we use "int open(const char *path, int oflag, ...)"
// // which returns an integer file descriptor. -1 is the invalid file
// // descriptor so to make an object that will call "int close(int fd)"
// // automatically we can use:
//
// CleanUp <int, int> fd(open("/tmp/a.txt", O_RDONLY, 0), -1, close);
//
// // malloc/free example
// CleanUp <void *, void> malloced_bytes(malloc(32), NULL, free);
//----------------------------------------------------------------------
template <typename T, typename R = void>
class CleanUp
{
public:
typedef T value_type;
typedef R (*CallbackType)(value_type);
//----------------------------------------------------------------------
// Constructor that sets the current value only. No values are
// considered to be invalid and the cleanup function will be called
// regardless of the value of m_current_value.
//----------------------------------------------------------------------
CleanUp (value_type value, CallbackType callback) :
m_current_value (value),
m_invalid_value (),
m_callback (callback),
m_callback_called (false),
m_invalid_value_is_valid (false)
{
}
//----------------------------------------------------------------------
// Constructor that sets the current value and also the invalid value.
// The cleanup function will be called on "m_value" as long as it isn't
// equal to "m_invalid_value".
//----------------------------------------------------------------------
CleanUp (value_type value, value_type invalid, CallbackType callback) :
m_current_value (value),
m_invalid_value (invalid),
m_callback (callback),
m_callback_called (false),
m_invalid_value_is_valid (true)
{
}
//----------------------------------------------------------------------
// Automatically cleanup when this object goes out of scope.
//----------------------------------------------------------------------
~CleanUp ()
{
clean();
}
//----------------------------------------------------------------------
// Access the value stored in this class
//----------------------------------------------------------------------
value_type get()
{
return m_current_value;
}
//----------------------------------------------------------------------
// Access the value stored in this class
//----------------------------------------------------------------------
const value_type
get() const
{
return m_current_value;
}
//----------------------------------------------------------------------
// Reset the owned value to "value". If a current value is valid and
// the cleanup callback hasn't been called, the previous value will
// be cleaned up (see void CleanUp::clean()).
//----------------------------------------------------------------------
void
set (const value_type value)
{
// Cleanup the current value if needed
clean ();
// Now set the new value and mark our callback as not called
m_callback_called = false;
m_current_value = value;
}
//----------------------------------------------------------------------
// Checks is "m_current_value" is valid. The value is considered valid
// no invalid value was supplied during construction of this object or
// if an invalid value was supplied and "m_current_value" is not equal
// to "m_invalid_value".
//
// Returns true if "m_current_value" is valid, false otherwise.
//----------------------------------------------------------------------
bool
is_valid() const
{
if (m_invalid_value_is_valid)
return m_current_value != m_invalid_value;
return true;
}
//----------------------------------------------------------------------
// This function will call the cleanup callback provided in the
// constructor one time if the value is considered valid (See is_valid()).
// This function sets m_callback_called to true so we don't call the
// cleanup callback multiple times on the same value.
//----------------------------------------------------------------------
void
clean()
{
if (m_callback && !m_callback_called)
{
m_callback_called = true;
if (is_valid())
m_callback(m_current_value);
}
}
//----------------------------------------------------------------------
// Cancels the cleanup that would have been called on "m_current_value"
// if it was valid. This function can be used to release the value
// contained in this object so ownership can be transfered to the caller.
//----------------------------------------------------------------------
value_type
release ()
{
m_callback_called = true;
return m_current_value;
}
private:
value_type m_current_value;
const value_type m_invalid_value;
CallbackType m_callback;
bool m_callback_called;
bool m_invalid_value_is_valid;
// Outlaw default constructor, copy constructor and the assignment operator
DISALLOW_COPY_AND_ASSIGN (CleanUp);
};
template <typename T, typename R, typename A0>
class CleanUp2
{
public:
typedef T value_type;
typedef R (*CallbackType)(value_type, A0);
//----------------------------------------------------------------------
// Constructor that sets the current value only. No values are
// considered to be invalid and the cleanup function will be called
// regardless of the value of m_current_value.
//----------------------------------------------------------------------
CleanUp2 (value_type value, CallbackType callback, A0 arg) :
m_current_value (value),
m_invalid_value (),
m_callback (callback),
m_callback_called (false),
m_invalid_value_is_valid (false),
m_argument(arg)
{
}
//----------------------------------------------------------------------
// Constructor that sets the current value and also the invalid value.
// The cleanup function will be called on "m_value" as long as it isn't
// equal to "m_invalid_value".
//----------------------------------------------------------------------
CleanUp2 (value_type value, value_type invalid, CallbackType callback, A0 arg) :
m_current_value (value),
m_invalid_value (invalid),
m_callback (callback),
m_callback_called (false),
m_invalid_value_is_valid (true),
m_argument(arg)
{
}
//----------------------------------------------------------------------
// Automatically cleanup when this object goes out of scope.
//----------------------------------------------------------------------
~CleanUp2 ()
{
clean();
}
//----------------------------------------------------------------------
// Access the value stored in this class
//----------------------------------------------------------------------
value_type get()
{
return m_current_value;
}
//----------------------------------------------------------------------
// Access the value stored in this class
//----------------------------------------------------------------------
const value_type
get() const
{
return m_current_value;
}
//----------------------------------------------------------------------
// Reset the owned value to "value". If a current value is valid and
// the cleanup callback hasn't been called, the previous value will
// be cleaned up (see void CleanUp::clean()).
//----------------------------------------------------------------------
void
set (const value_type value)
{
// Cleanup the current value if needed
clean ();
// Now set the new value and mark our callback as not called
m_callback_called = false;
m_current_value = value;
}
//----------------------------------------------------------------------
// Checks is "m_current_value" is valid. The value is considered valid
// no invalid value was supplied during construction of this object or
// if an invalid value was supplied and "m_current_value" is not equal
// to "m_invalid_value".
//
// Returns true if "m_current_value" is valid, false otherwise.
//----------------------------------------------------------------------
bool
is_valid() const
{
if (m_invalid_value_is_valid)
return m_current_value != m_invalid_value;
return true;
}
//----------------------------------------------------------------------
// This function will call the cleanup callback provided in the
// constructor one time if the value is considered valid (See is_valid()).
// This function sets m_callback_called to true so we don't call the
// cleanup callback multiple times on the same value.
//----------------------------------------------------------------------
void
clean()
{
if (m_callback && !m_callback_called)
{
m_callback_called = true;
if (is_valid())
m_callback(m_current_value, m_argument);
}
}
//----------------------------------------------------------------------
// Cancels the cleanup that would have been called on "m_current_value"
// if it was valid. This function can be used to release the value
// contained in this object so ownership can be transfered to the caller.
//----------------------------------------------------------------------
value_type
release ()
{
m_callback_called = true;
return m_current_value;
}
private:
value_type m_current_value;
const value_type m_invalid_value;
CallbackType m_callback;
bool m_callback_called;
bool m_invalid_value_is_valid;
A0 m_argument;
// Outlaw default constructor, copy constructor and the assignment operator
DISALLOW_COPY_AND_ASSIGN (CleanUp2);
};
} // namespace lldb_utility
#endif // #ifndef liblldb_CleanUp_h_
|