security/manager/ssl/src/nsNSSCleaner.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/src/nsNSSCleaner.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef _INC_NSSCleaner_H
     1.9 +#define _INC_NSSCleaner_H
    1.10 +
    1.11 +/*
    1.12 +  NSSCleanupAutoPtrClass(CERTCertificate, CERT_DestroyCertificate)
    1.13 +
    1.14 +  will produce:
    1.15 +
    1.16 +  class CERTCertificateCleaner
    1.17 +  {
    1.18 +  private:
    1.19 +    CERTCertificateCleaner(const CERTCertificateCleaner&);
    1.20 +    CERTCertificateCleaner();
    1.21 +    void operator=(const CERTCertificateCleaner&);
    1.22 +    CERTCertificate *&object;
    1.23 +  public:
    1.24 +    CERTCertificateCleaner(CERTCertificate *&a_object)
    1.25 +      :object(a_object) {}
    1.26 +    ~CERTCertificateCleaner() {
    1.27 +      if (object) {
    1.28 +        CERT_DestroyCertificate(object);
    1.29 +        object = nullptr;
    1.30 +      }
    1.31 +    }
    1.32 +  };
    1.33 +  
    1.34 +  By making default and copy constructor, and assignment operator
    1.35 +  private, we will make sure nobody will be able to use it.
    1.36 +  Not defining bodies for them is an additional safeguard.
    1.37 +  
    1.38 +  This class is not designed to allow being passed around.
    1.39 +  It's just for automatic cleanup of a local variable.
    1.40 +  
    1.41 +  
    1.42 +  By storing a reference to the underlying pointer,
    1.43 +  we will zero out the given pointer variable,
    1.44 +  making sure it will not be used after it has been freed.
    1.45 +  
    1.46 +  Even better, in case the underlying pointer variable gets
    1.47 +  assigned another value, this will be recognized, and
    1.48 +  the latest value stored in the pointer will be freed.
    1.49 +  
    1.50 +  
    1.51 +  In order to not require everybody to have all the NSS
    1.52 +  includes in their implementation files,
    1.53 +  we don't declare the classes here.
    1.54 +  
    1.55 +*/
    1.56 +
    1.57 +#define NSSCleanupAutoPtrClass(nsstype, cleanfunc) \
    1.58 +class nsstype##Cleaner                             \
    1.59 +{                                                  \
    1.60 +private:                                           \
    1.61 +  nsstype##Cleaner(const nsstype##Cleaner&);       \
    1.62 +  nsstype##Cleaner();                              \
    1.63 +  void operator=(const nsstype##Cleaner&);         \
    1.64 +  nsstype *&object;                                \
    1.65 +public:                                            \
    1.66 +  nsstype##Cleaner(nsstype *&a_object)             \
    1.67 +    :object(a_object) {}                           \
    1.68 +  ~nsstype##Cleaner() {                            \
    1.69 +    if (object) {                                  \
    1.70 +      cleanfunc(object);                           \
    1.71 +      object = nullptr;                             \
    1.72 +    }                                              \
    1.73 +  }                                                \
    1.74 +  void detach() {object=nullptr;}                   \
    1.75 +};
    1.76 +
    1.77 +#define NSSCleanupAutoPtrClass_WithParam(nsstype, cleanfunc, namesuffix, paramvalue) \
    1.78 +class nsstype##Cleaner##namesuffix                 \
    1.79 +{                                                  \
    1.80 +private:                                           \
    1.81 +  nsstype##Cleaner##namesuffix(const nsstype##Cleaner##namesuffix &); \
    1.82 +  nsstype##Cleaner##namesuffix();                                     \
    1.83 +  void operator=(const nsstype##Cleaner##namesuffix &);               \
    1.84 +  nsstype *&object;                                \
    1.85 +public:                                            \
    1.86 +  nsstype##Cleaner##namesuffix(nsstype *&a_object) \
    1.87 +    :object(a_object) {}                           \
    1.88 +  ~nsstype##Cleaner##namesuffix() {                \
    1.89 +    if (object) {                                  \
    1.90 +      cleanfunc(object, paramvalue);               \
    1.91 +      object = nullptr;                             \
    1.92 +    }                                              \
    1.93 +  }                                                \
    1.94 +  void detach() {object=nullptr;}                   \
    1.95 +};
    1.96 +
    1.97 +#include "certt.h"
    1.98 +
    1.99 +class CERTVerifyLogContentsCleaner
   1.100 +{
   1.101 +public:
   1.102 +  CERTVerifyLogContentsCleaner(CERTVerifyLog *&cvl);
   1.103 +  ~CERTVerifyLogContentsCleaner();
   1.104 +private:
   1.105 +  CERTVerifyLog *&m_cvl;
   1.106 +};
   1.107 +
   1.108 +#endif

mercurial