2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-04-06 13:16:33 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 11:41:39 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-04-06 13:16:33 +04:00
|
|
|
|
|
|
|
/* A number of structures to simplify scope-based RAII management. */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_Scoped_h
|
|
|
|
#define mozilla_Scoped_h
|
2012-04-06 13:16:33 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Resource Acquisition Is Initialization is a programming idiom used
|
|
|
|
* to write robust code that is able to deallocate resources properly,
|
|
|
|
* even in presence of execution errors or exceptions that need to be
|
|
|
|
* propagated. The Scoped* classes defined in this header perform the
|
|
|
|
* deallocation of the resource they hold once program execution
|
|
|
|
* reaches the end of the scope for which they have been defined.
|
|
|
|
*
|
|
|
|
* This header provides the following RAII classes:
|
|
|
|
*
|
|
|
|
* - |ScopedFreePtr| - a container for a pointer, that automatically calls
|
|
|
|
* |free()| at the end of the scope;
|
|
|
|
* - |ScopedDeletePtr| - a container for a pointer, that automatically calls
|
|
|
|
* |delete| at the end of the scope;
|
|
|
|
* - |ScopedDeleteArray| - a container for a pointer to an array, that
|
|
|
|
* automatically calls |delete[]| at the end of the scope.
|
|
|
|
*
|
|
|
|
* The general scenario for each of the RAII classes is the following:
|
|
|
|
*
|
|
|
|
* ScopedClass foo(create_value());
|
|
|
|
* // ... In this scope, |foo| is defined. Use |foo.get()| or |foo.rwget()|
|
|
|
|
* to access the value.
|
|
|
|
* // ... In case of |return| or |throw|, |foo| is deallocated automatically.
|
|
|
|
* // ... If |foo| needs to be returned or stored, use |foo.forget()|
|
|
|
|
*
|
|
|
|
* Note that the RAII classes defined in this header do _not_ perform any form
|
|
|
|
* of reference-counting or garbage-collection. These classes have exactly two
|
|
|
|
* behaviors:
|
2012-06-12 03:16:47 +04:00
|
|
|
*
|
2012-04-06 13:16:33 +04:00
|
|
|
* - if |forget()| has not been called, the resource is always deallocated at
|
|
|
|
* the end of the scope;
|
|
|
|
* - if |forget()| has been called, any control on the resource is unbound
|
|
|
|
* and the resource is not deallocated by the class.
|
|
|
|
*
|
|
|
|
* Extension:
|
|
|
|
*
|
2012-07-25 02:36:06 +04:00
|
|
|
* In addition, this header provides class |Scoped| and macros |SCOPED_TEMPLATE|
|
|
|
|
* and |MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE| to simplify the definition
|
|
|
|
* of RAII classes for other scenarios. These macros have been used to
|
|
|
|
* automatically close file descriptors/file handles when reaching the end of
|
|
|
|
* the scope, graphics contexts, etc.
|
2012-04-06 13:16:33 +04:00
|
|
|
*/
|
|
|
|
|
2014-03-27 20:52:48 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-04-06 13:16:33 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/GuardObjects.h"
|
2014-03-27 20:52:48 +04:00
|
|
|
#include "mozilla/Move.h"
|
2013-07-26 03:31:48 +04:00
|
|
|
#include "mozilla/NullPtr.h"
|
2012-04-06 13:16:33 +04:00
|
|
|
|
2012-06-12 03:16:47 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-04-06 13:16:33 +04:00
|
|
|
/*
|
|
|
|
* Scoped is a helper to create RAII wrappers
|
|
|
|
* Type argument |Traits| is expected to have the following structure:
|
|
|
|
*
|
|
|
|
* struct Traits {
|
|
|
|
* // Define the type of the value stored in the wrapper
|
|
|
|
* typedef value_type type;
|
|
|
|
* // Returns the value corresponding to the uninitialized or freed state
|
|
|
|
* const static type empty();
|
|
|
|
* // Release resources corresponding to the wrapped value
|
|
|
|
* // This function is responsible for not releasing an |empty| value
|
|
|
|
* const static void release(type);
|
|
|
|
* }
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename Traits>
|
2012-04-06 13:16:33 +04:00
|
|
|
class Scoped
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
public:
|
|
|
|
typedef typename Traits::type Resource;
|
|
|
|
|
|
|
|
explicit Scoped(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM)
|
|
|
|
: value(Traits::empty())
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
2013-02-18 10:56:32 +04:00
|
|
|
explicit Scoped(const Resource& v
|
2012-06-04 07:36:43 +04:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2013-02-18 10:56:32 +04:00
|
|
|
: value(v)
|
2012-06-04 07:36:43 +04:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
2014-03-27 20:52:48 +04:00
|
|
|
|
|
|
|
/* Move constructor. */
|
|
|
|
explicit Scoped(Scoped&& v
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: value(Move(v.value))
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
v.value = Traits::empty();
|
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
~Scoped() {
|
|
|
|
Traits::release(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constant getter
|
|
|
|
operator const Resource&() const { return value; }
|
|
|
|
const Resource& operator->() const { return value; }
|
|
|
|
const Resource& get() const { return value; }
|
|
|
|
// Non-constant getter.
|
|
|
|
Resource& rwget() { return value; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forget the resource.
|
|
|
|
*
|
|
|
|
* Once |forget| has been called, the |Scoped| is neutralized, i.e. it will
|
|
|
|
* have no effect at destruction (unless it is reset to another resource by
|
|
|
|
* |operator=|).
|
|
|
|
*
|
|
|
|
* @return The original resource.
|
|
|
|
*/
|
|
|
|
Resource forget() {
|
|
|
|
Resource tmp = value;
|
|
|
|
value = Traits::empty();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform immediate clean-up of this |Scoped|.
|
|
|
|
*
|
|
|
|
* If this |Scoped| is currently empty, this method has no effect.
|
|
|
|
*/
|
|
|
|
void dispose() {
|
|
|
|
Traits::release(value);
|
|
|
|
value = Traits::empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Resource& other) const {
|
|
|
|
return value == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace the resource with another resource.
|
|
|
|
*
|
|
|
|
* Calling |operator=| has the side-effect of triggering clean-up. If you do
|
|
|
|
* not want to trigger clean-up, you should first invoke |forget|.
|
|
|
|
*
|
|
|
|
* @return this
|
|
|
|
*/
|
2014-03-27 20:52:43 +04:00
|
|
|
Scoped& operator=(const Resource& other) {
|
2012-06-04 07:36:43 +04:00
|
|
|
return reset(other);
|
|
|
|
}
|
2014-03-27 20:52:43 +04:00
|
|
|
Scoped& reset(const Resource& other) {
|
2012-06-04 07:36:43 +04:00
|
|
|
Traits::release(value);
|
|
|
|
value = other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:52:48 +04:00
|
|
|
/* Move assignment operator. */
|
|
|
|
Scoped& operator=(Scoped&& rhs) {
|
|
|
|
MOZ_ASSERT(&rhs != this, "self-move-assignment not allowed");
|
|
|
|
this->~Scoped();
|
|
|
|
new(this) Scoped(Move(rhs));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
private:
|
2014-03-27 20:52:43 +04:00
|
|
|
explicit Scoped(const Scoped& value) MOZ_DELETE;
|
|
|
|
Scoped& operator=(const Scoped& value) MOZ_DELETE;
|
2012-06-04 07:36:43 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Resource value;
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SCOPED_TEMPLATE defines a templated class derived from Scoped
|
|
|
|
* This allows to implement templates such as ScopedFreePtr.
|
|
|
|
*
|
|
|
|
* @param name The name of the class to define.
|
|
|
|
* @param Traits A struct implementing clean-up. See the implementations
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
#define SCOPED_TEMPLATE(name, Traits) \
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename Type> \
|
2012-06-12 03:16:47 +04:00
|
|
|
struct name : public mozilla::Scoped<Traits<Type> > \
|
2012-04-06 13:16:33 +04:00
|
|
|
{ \
|
2012-06-12 03:16:47 +04:00
|
|
|
typedef mozilla::Scoped<Traits<Type> > Super; \
|
2012-06-04 07:36:43 +04:00
|
|
|
typedef typename Super::Resource Resource; \
|
2014-03-27 20:52:48 +04:00
|
|
|
name& operator=(Resource rhs) { \
|
|
|
|
Super::operator=(rhs); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
name& operator=(name&& rhs) { \
|
|
|
|
Super::operator=(Move(rhs)); \
|
2012-06-04 07:36:43 +04:00
|
|
|
return *this; \
|
|
|
|
} \
|
|
|
|
explicit name(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) \
|
|
|
|
: Super(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) \
|
|
|
|
{} \
|
2014-03-27 20:52:48 +04:00
|
|
|
explicit name(Resource rhs \
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
|
|
|
|
: Super(rhs \
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \
|
|
|
|
{} \
|
|
|
|
explicit name(name&& rhs \
|
2012-06-04 07:36:43 +04:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \
|
2014-03-27 20:52:48 +04:00
|
|
|
: Super(Move(rhs) \
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \
|
2012-06-04 07:36:43 +04:00
|
|
|
{} \
|
|
|
|
private: \
|
2014-03-27 20:52:48 +04:00
|
|
|
explicit name(name&) MOZ_DELETE; \
|
|
|
|
name& operator=(name&) MOZ_DELETE; \
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ScopedFreePtr is a RAII wrapper for pointers that need to be free()d.
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* ScopedFreePtr<S> foo = malloc(sizeof(S));
|
|
|
|
* ScopedFreePtr<char> bar = strdup(str);
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-04-06 13:16:33 +04:00
|
|
|
struct ScopedFreePtrTraits
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
typedef T* type;
|
2013-07-26 03:31:48 +04:00
|
|
|
static T* empty() { return nullptr; }
|
2012-06-04 07:36:43 +04:00
|
|
|
static void release(T* ptr) { free(ptr); }
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
SCOPED_TEMPLATE(ScopedFreePtr, ScopedFreePtrTraits)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ScopedDeletePtr is a RAII wrapper for pointers that need to be deleted.
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* ScopedDeletePtr<S> foo = new S();
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
|
|
|
struct ScopedDeletePtrTraits : public ScopedFreePtrTraits<T>
|
|
|
|
{
|
|
|
|
static void release(T* ptr) { delete ptr; }
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
SCOPED_TEMPLATE(ScopedDeletePtr, ScopedDeletePtrTraits)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ScopedDeleteArray is a RAII wrapper for pointers that need to be delete[]ed.
|
|
|
|
*
|
|
|
|
* struct S { ... };
|
|
|
|
* ScopedDeleteArray<S> foo = new S[42];
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-04-06 13:16:33 +04:00
|
|
|
struct ScopedDeleteArrayTraits : public ScopedFreePtrTraits<T>
|
|
|
|
{
|
2012-06-04 07:36:43 +04:00
|
|
|
static void release(T* ptr) { delete [] ptr; }
|
2012-04-06 13:16:33 +04:00
|
|
|
};
|
|
|
|
SCOPED_TEMPLATE(ScopedDeleteArray, ScopedDeleteArrayTraits)
|
|
|
|
|
2012-07-25 02:36:06 +04:00
|
|
|
/*
|
|
|
|
* MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE makes it easy to create scoped
|
|
|
|
* pointers for types with custom deleters; just overload
|
|
|
|
* TypeSpecificDelete(T*) in the same namespace as T to call the deleter for
|
|
|
|
* type T.
|
|
|
|
*
|
|
|
|
* @param name The name of the class to define.
|
|
|
|
* @param Type A struct implementing clean-up. See the implementations
|
|
|
|
* for more details.
|
|
|
|
* *param Deleter The function that is used to delete/destroy/free a
|
|
|
|
* non-null value of Type*.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedPRFileDesc, PRFileDesc, \
|
|
|
|
* PR_Close)
|
|
|
|
* ...
|
|
|
|
* {
|
|
|
|
* ScopedPRFileDesc file(PR_OpenFile(...));
|
|
|
|
* ...
|
|
|
|
* } // file is closed with PR_Close here
|
|
|
|
*/
|
|
|
|
#define MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(name, Type, Deleter) \
|
2012-11-16 05:02:11 +04:00
|
|
|
template <> inline void TypeSpecificDelete(Type * value) { Deleter(value); } \
|
2012-07-25 02:36:06 +04:00
|
|
|
typedef ::mozilla::TypeSpecificScopedPointer<Type> name;
|
|
|
|
|
2012-11-16 05:02:11 +04:00
|
|
|
template <typename T> void TypeSpecificDelete(T * value);
|
|
|
|
|
2012-07-25 02:36:06 +04:00
|
|
|
template <typename T>
|
|
|
|
struct TypeSpecificScopedPointerTraits
|
|
|
|
{
|
|
|
|
typedef T* type;
|
2013-07-26 03:31:48 +04:00
|
|
|
const static type empty() { return nullptr; }
|
2012-07-25 02:36:06 +04:00
|
|
|
const static void release(type value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
TypeSpecificDelete(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SCOPED_TEMPLATE(TypeSpecificScopedPointer, TypeSpecificScopedPointerTraits)
|
|
|
|
|
2012-06-12 03:16:47 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_Scoped_h */
|