From 4846920c4c42615ecca40b5d2daed276e1de3805 Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Fri, 1 Oct 2010 14:14:01 +0100 Subject: Create QScopedResource class This has similar purpose to QScopedPointer, and is implemented using it. Symbian has many resource classes, e.g. RFile, RSocket, which behave as value types, but require the Close() member function to be called to release the server side resource owned by the handle. This class can be used to give exception safety, for example when trying to add a resource class to a container std::bad_alloc could be thrown. By assigning the resource to QScopedResource when it is opened, and calling take() after it is added to the container, it is ensured that the resource will be closed if an exception is thrown. Equivalent to the symbian pattern: CleanupClosePushL(file); container.AppendL(file); //transfers ownership, may leave CleanupStack::Pop(file); With STL exceptions we can do: QScopedResource ptr(file); container.append(file); //transfers ownership, may throw ptr.take; Reviewed-By: mread --- src/corelib/kernel/qcore_symbian_p.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/corelib/kernel/qcore_symbian_p.h b/src/corelib/kernel/qcore_symbian_p.h index 3019e05..a8f576d 100644 --- a/src/corelib/kernel/qcore_symbian_p.h +++ b/src/corelib/kernel/qcore_symbian_p.h @@ -59,6 +59,7 @@ #include #include #include +#include #include #include @@ -262,6 +263,29 @@ private: RConnection *iDefaultConnection; }; +template class QScopedPointerResourceCloser +{ +public: + static inline void cleanup(T* pointer) + { + if (pointer) + pointer->Close(); + } +}; + +/*typical use: + RFile file; + file.Open(...); + QScopedResource ptr(file); + container.append(file); //this may throw std::bad_alloc, in which case file.Close() is called by destructor + ptr.take(); //if we reach this line, ownership is transferred to the container + */ +template class QScopedResource : public QScopedPointer > +{ +public: + inline QScopedResource(T& resource) : QScopedPointer >(&resource) {} +}; + QT_END_NAMESPACE QT_END_HEADER -- cgit v0.12