holder.h 554 B

1234567891011121314151617181920212223242526272829303132
  1. #pragma once
  2. #include <util/generic/yexception.h>
  3. namespace NOpenSSL {
  4. template <typename TType, auto Create, auto Destroy, class... Args>
  5. class THolder {
  6. public:
  7. inline THolder(Args... args) {
  8. Ptr = Create(args...);
  9. if (!Ptr) {
  10. throw std::bad_alloc();
  11. }
  12. }
  13. THolder(const THolder&) = delete;
  14. THolder& operator=(const THolder&) = delete;
  15. inline ~THolder() noexcept {
  16. Destroy(Ptr);
  17. }
  18. inline operator TType* () noexcept {
  19. return Ptr;
  20. }
  21. private:
  22. TType* Ptr;
  23. };
  24. }