hb_scoped.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef THIRD_PARTY_HARFBUZZ_NG_UTILS_HB_SCOPED_H_
  5. #define THIRD_PARTY_HARFBUZZ_NG_UTILS_HB_SCOPED_H_
  6. // clang-format off
  7. #include <hb.h>
  8. #include <hb-subset.h>
  9. // clang-format on
  10. #include <memory>
  11. #include <type_traits>
  12. template <typename T>
  13. struct always_false : std::false_type {};
  14. template <class T>
  15. struct HbSpecializedDeleter {
  16. inline void operator()(T* obj) {
  17. static_assert(always_false<T>::value,
  18. "HbScoped is only allowed for HarfBuzz types that have a "
  19. "deleter specialization.");
  20. }
  21. };
  22. // Defines a scoped pointer type HbScoped based on std::unique_ptr, using the
  23. // corresponsing HarfBuzz destructors to commonly used public HarfBuzz types.
  24. // The interface of HbScoped is the same as that of std::unique_ptr.
  25. //
  26. // void MyFunction() {
  27. // HbScoped<hb_blob_t> scoped_harfbuzz_blob(
  28. // hb_blob_create(mydata, mylength));
  29. //
  30. // DoSomethingWithBlob(scoped_harfbuzz_blob.get());
  31. // }
  32. //
  33. // When |scoped_harfbuzz_buffer| goes out of scope, hb_blob_destroy() is called
  34. // for the hb_blob_t* created from hb_blob_create().
  35. template <class T>
  36. using HbScoped = std::unique_ptr<T, HbSpecializedDeleter<T>>;
  37. #define SPECIALIZED_DELETER_FOR_HARFBUZZ_TYPE(TYPE, DESTRUCTOR) \
  38. template <> \
  39. struct HbSpecializedDeleter<TYPE> { \
  40. inline void operator()(TYPE* obj) { DESTRUCTOR(obj); } \
  41. };
  42. #define HB_TYPE_DESTRUCTOR_PAIRS_REPEAT(F) \
  43. F(hb_blob_t, hb_blob_destroy) \
  44. F(hb_buffer_t, hb_buffer_destroy) \
  45. F(hb_face_t, hb_face_destroy) \
  46. F(hb_font_t, hb_font_destroy) \
  47. F(hb_set_t, hb_set_destroy) \
  48. F(hb_subset_input_t, hb_subset_input_destroy)
  49. HB_TYPE_DESTRUCTOR_PAIRS_REPEAT(SPECIALIZED_DELETER_FOR_HARFBUZZ_TYPE)
  50. #endif // THIRD_PARTY_HARFBUZZ_NG_UTILS_HB_SCOPED_H_