hb-blob.hh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright © 2009 Red Hat, Inc.
  3. * Copyright © 2018 Google, Inc.
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Red Hat Author(s): Behdad Esfahbod
  26. * Google Author(s): Behdad Esfahbod
  27. */
  28. #ifndef HB_BLOB_HH
  29. #define HB_BLOB_HH
  30. #include "hb.hh"
  31. /*
  32. * hb_blob_t
  33. */
  34. struct hb_blob_t
  35. {
  36. ~hb_blob_t () { destroy_user_data (); }
  37. void destroy_user_data ()
  38. {
  39. if (destroy)
  40. {
  41. destroy (user_data);
  42. user_data = nullptr;
  43. destroy = nullptr;
  44. }
  45. }
  46. HB_INTERNAL bool try_make_writable ();
  47. HB_INTERNAL bool try_make_writable_inplace ();
  48. HB_INTERNAL bool try_make_writable_inplace_unix ();
  49. hb_bytes_t as_bytes () const { return hb_bytes_t (data, length); }
  50. template <typename Type>
  51. const Type* as () const { return as_bytes ().as<Type> (); }
  52. public:
  53. hb_object_header_t header;
  54. const char *data = nullptr;
  55. unsigned int length = 0;
  56. hb_memory_mode_t mode = (hb_memory_mode_t) 0;
  57. void *user_data = nullptr;
  58. hb_destroy_func_t destroy = nullptr;
  59. };
  60. /*
  61. * hb_blob_ptr_t
  62. */
  63. template <typename P>
  64. struct hb_blob_ptr_t
  65. {
  66. typedef hb_remove_pointer<P> T;
  67. hb_blob_ptr_t (hb_blob_t *b_ = nullptr) : b (b_) {}
  68. hb_blob_t * operator = (hb_blob_t *b_) { return b = b_; }
  69. const T * operator -> () const { return get (); }
  70. const T & operator * () const { return *get (); }
  71. template <typename C> operator const C * () const { return get (); }
  72. operator const char * () const { return (const char *) get (); }
  73. const T * get () const { return b->as<T> (); }
  74. hb_blob_t * get_blob () const { return b.get_raw (); }
  75. unsigned int get_length () const { return b.get ()->length; }
  76. void destroy () { hb_blob_destroy (b.get_raw ()); b = nullptr; }
  77. private:
  78. hb_nonnull_ptr_t<hb_blob_t> b;
  79. };
  80. #endif /* HB_BLOB_HH */