michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _INC_NSSCleaner_H michael@0: #define _INC_NSSCleaner_H michael@0: michael@0: /* michael@0: NSSCleanupAutoPtrClass(CERTCertificate, CERT_DestroyCertificate) michael@0: michael@0: will produce: michael@0: michael@0: class CERTCertificateCleaner michael@0: { michael@0: private: michael@0: CERTCertificateCleaner(const CERTCertificateCleaner&); michael@0: CERTCertificateCleaner(); michael@0: void operator=(const CERTCertificateCleaner&); michael@0: CERTCertificate *&object; michael@0: public: michael@0: CERTCertificateCleaner(CERTCertificate *&a_object) michael@0: :object(a_object) {} michael@0: ~CERTCertificateCleaner() { michael@0: if (object) { michael@0: CERT_DestroyCertificate(object); michael@0: object = nullptr; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: By making default and copy constructor, and assignment operator michael@0: private, we will make sure nobody will be able to use it. michael@0: Not defining bodies for them is an additional safeguard. michael@0: michael@0: This class is not designed to allow being passed around. michael@0: It's just for automatic cleanup of a local variable. michael@0: michael@0: michael@0: By storing a reference to the underlying pointer, michael@0: we will zero out the given pointer variable, michael@0: making sure it will not be used after it has been freed. michael@0: michael@0: Even better, in case the underlying pointer variable gets michael@0: assigned another value, this will be recognized, and michael@0: the latest value stored in the pointer will be freed. michael@0: michael@0: michael@0: In order to not require everybody to have all the NSS michael@0: includes in their implementation files, michael@0: we don't declare the classes here. michael@0: michael@0: */ michael@0: michael@0: #define NSSCleanupAutoPtrClass(nsstype, cleanfunc) \ michael@0: class nsstype##Cleaner \ michael@0: { \ michael@0: private: \ michael@0: nsstype##Cleaner(const nsstype##Cleaner&); \ michael@0: nsstype##Cleaner(); \ michael@0: void operator=(const nsstype##Cleaner&); \ michael@0: nsstype *&object; \ michael@0: public: \ michael@0: nsstype##Cleaner(nsstype *&a_object) \ michael@0: :object(a_object) {} \ michael@0: ~nsstype##Cleaner() { \ michael@0: if (object) { \ michael@0: cleanfunc(object); \ michael@0: object = nullptr; \ michael@0: } \ michael@0: } \ michael@0: void detach() {object=nullptr;} \ michael@0: }; michael@0: michael@0: #define NSSCleanupAutoPtrClass_WithParam(nsstype, cleanfunc, namesuffix, paramvalue) \ michael@0: class nsstype##Cleaner##namesuffix \ michael@0: { \ michael@0: private: \ michael@0: nsstype##Cleaner##namesuffix(const nsstype##Cleaner##namesuffix &); \ michael@0: nsstype##Cleaner##namesuffix(); \ michael@0: void operator=(const nsstype##Cleaner##namesuffix &); \ michael@0: nsstype *&object; \ michael@0: public: \ michael@0: nsstype##Cleaner##namesuffix(nsstype *&a_object) \ michael@0: :object(a_object) {} \ michael@0: ~nsstype##Cleaner##namesuffix() { \ michael@0: if (object) { \ michael@0: cleanfunc(object, paramvalue); \ michael@0: object = nullptr; \ michael@0: } \ michael@0: } \ michael@0: void detach() {object=nullptr;} \ michael@0: }; michael@0: michael@0: #include "certt.h" michael@0: michael@0: class CERTVerifyLogContentsCleaner michael@0: { michael@0: public: michael@0: CERTVerifyLogContentsCleaner(CERTVerifyLog *&cvl); michael@0: ~CERTVerifyLogContentsCleaner(); michael@0: private: michael@0: CERTVerifyLog *&m_cvl; michael@0: }; michael@0: michael@0: #endif