1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/AutoClose.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_net_AutoClose_h 1.11 +#define mozilla_net_AutoClose_h 1.12 + 1.13 +#include "nsCOMPtr.h" 1.14 + 1.15 +namespace mozilla { namespace net { 1.16 + 1.17 +// Like an nsAutoPtr for XPCOM streams (e.g. nsIAsyncInputStream) and other 1.18 +// refcounted classes that need to have the Close() method called explicitly 1.19 +// before they are destroyed. 1.20 +template <typename T> 1.21 +class AutoClose 1.22 +{ 1.23 +public: 1.24 + AutoClose() { } 1.25 + ~AutoClose(){ 1.26 + Close(); 1.27 + } 1.28 + 1.29 + operator bool() const 1.30 + { 1.31 + return mPtr; 1.32 + } 1.33 + 1.34 + already_AddRefed<T> forget() 1.35 + { 1.36 + return mPtr.forget(); 1.37 + } 1.38 + 1.39 + void takeOver(nsCOMPtr<T> & rhs) 1.40 + { 1.41 + Close(); 1.42 + mPtr = rhs.forget(); 1.43 + } 1.44 + 1.45 + void takeOver(AutoClose<T> & rhs) 1.46 + { 1.47 + Close(); 1.48 + mPtr = rhs.mPtr.forget(); 1.49 + } 1.50 + 1.51 + void CloseAndRelease() 1.52 + { 1.53 + Close(); 1.54 + mPtr = nullptr; 1.55 + } 1.56 + 1.57 + T* operator->() const 1.58 + { 1.59 + return mPtr.operator->(); 1.60 + } 1.61 + 1.62 +private: 1.63 + void Close() 1.64 + { 1.65 + if (mPtr) { 1.66 + mPtr->Close(); 1.67 + } 1.68 + } 1.69 + 1.70 + void operator=(const AutoClose<T> &) MOZ_DELETE; 1.71 + AutoClose(const AutoClose<T> &) MOZ_DELETE; 1.72 + 1.73 + nsCOMPtr<T> mPtr; 1.74 +}; 1.75 + 1.76 +} } // namespace mozilla::net 1.77 + 1.78 +#endif // mozilla_net_AutoClose_h