security/manager/ssl/src/nsNSSCleaner.h

branch
TOR_BUG_9701
changeset 3
141e0f1194b1
equal deleted inserted replaced
-1:000000000000 0:a08d73d0f05f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #ifndef _INC_NSSCleaner_H
6 #define _INC_NSSCleaner_H
7
8 /*
9 NSSCleanupAutoPtrClass(CERTCertificate, CERT_DestroyCertificate)
10
11 will produce:
12
13 class CERTCertificateCleaner
14 {
15 private:
16 CERTCertificateCleaner(const CERTCertificateCleaner&);
17 CERTCertificateCleaner();
18 void operator=(const CERTCertificateCleaner&);
19 CERTCertificate *&object;
20 public:
21 CERTCertificateCleaner(CERTCertificate *&a_object)
22 :object(a_object) {}
23 ~CERTCertificateCleaner() {
24 if (object) {
25 CERT_DestroyCertificate(object);
26 object = nullptr;
27 }
28 }
29 };
30
31 By making default and copy constructor, and assignment operator
32 private, we will make sure nobody will be able to use it.
33 Not defining bodies for them is an additional safeguard.
34
35 This class is not designed to allow being passed around.
36 It's just for automatic cleanup of a local variable.
37
38
39 By storing a reference to the underlying pointer,
40 we will zero out the given pointer variable,
41 making sure it will not be used after it has been freed.
42
43 Even better, in case the underlying pointer variable gets
44 assigned another value, this will be recognized, and
45 the latest value stored in the pointer will be freed.
46
47
48 In order to not require everybody to have all the NSS
49 includes in their implementation files,
50 we don't declare the classes here.
51
52 */
53
54 #define NSSCleanupAutoPtrClass(nsstype, cleanfunc) \
55 class nsstype##Cleaner \
56 { \
57 private: \
58 nsstype##Cleaner(const nsstype##Cleaner&); \
59 nsstype##Cleaner(); \
60 void operator=(const nsstype##Cleaner&); \
61 nsstype *&object; \
62 public: \
63 nsstype##Cleaner(nsstype *&a_object) \
64 :object(a_object) {} \
65 ~nsstype##Cleaner() { \
66 if (object) { \
67 cleanfunc(object); \
68 object = nullptr; \
69 } \
70 } \
71 void detach() {object=nullptr;} \
72 };
73
74 #define NSSCleanupAutoPtrClass_WithParam(nsstype, cleanfunc, namesuffix, paramvalue) \
75 class nsstype##Cleaner##namesuffix \
76 { \
77 private: \
78 nsstype##Cleaner##namesuffix(const nsstype##Cleaner##namesuffix &); \
79 nsstype##Cleaner##namesuffix(); \
80 void operator=(const nsstype##Cleaner##namesuffix &); \
81 nsstype *&object; \
82 public: \
83 nsstype##Cleaner##namesuffix(nsstype *&a_object) \
84 :object(a_object) {} \
85 ~nsstype##Cleaner##namesuffix() { \
86 if (object) { \
87 cleanfunc(object, paramvalue); \
88 object = nullptr; \
89 } \
90 } \
91 void detach() {object=nullptr;} \
92 };
93
94 #include "certt.h"
95
96 class CERTVerifyLogContentsCleaner
97 {
98 public:
99 CERTVerifyLogContentsCleaner(CERTVerifyLog *&cvl);
100 ~CERTVerifyLogContentsCleaner();
101 private:
102 CERTVerifyLog *&m_cvl;
103 };
104
105 #endif

mercurial