scoped_ptrs_util.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef scoped_ptrs_util_h__
  7. #define scoped_ptrs_util_h__
  8. #include <memory>
  9. #include "pkcs11uri.h"
  10. #include "secoid.h"
  11. struct ScopedDelete {
  12. void operator()(SECAlgorithmID* id) { SECOID_DestroyAlgorithmID(id, true); }
  13. void operator()(SECItem* item) { SECITEM_FreeItem(item, true); }
  14. void operator()(PK11URI* uri) { PK11URI_DestroyURI(uri); }
  15. void operator()(PLArenaPool* arena) { PORT_FreeArena(arena, PR_FALSE); }
  16. };
  17. template <class T>
  18. struct ScopedMaybeDelete {
  19. void operator()(T* ptr) {
  20. if (ptr) {
  21. ScopedDelete del;
  22. del(ptr);
  23. }
  24. }
  25. };
  26. #define SCOPED(x) typedef std::unique_ptr<x, ScopedMaybeDelete<x> > Scoped##x
  27. SCOPED(SECAlgorithmID);
  28. SCOPED(SECItem);
  29. SCOPED(PK11URI);
  30. SCOPED(PLArenaPool);
  31. #undef SCOPED
  32. struct StackSECItem : public SECItem {
  33. StackSECItem() : SECItem({siBuffer, nullptr, 0}) {}
  34. ~StackSECItem() { SECITEM_FreeItem(this, PR_FALSE); }
  35. };
  36. #endif // scoped_ptrs_util_h__