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 29 #ifndef HB_BLOB_HH 30 #define HB_BLOB_HH 31 32 #include "hb.hh" 33 34 35 /* 36 * hb_blob_t 37 */ 38 39 struct hb_blob_t 40 { ~hb_blob_thb_blob_t41 ~hb_blob_t () { destroy_user_data (); } 42 destroy_user_datahb_blob_t43 void destroy_user_data () 44 { 45 if (destroy) 46 { 47 destroy (user_data); 48 user_data = nullptr; 49 destroy = nullptr; 50 } 51 } 52 53 HB_INTERNAL bool try_make_writable (); 54 HB_INTERNAL bool try_make_writable_inplace (); 55 HB_INTERNAL bool try_make_writable_inplace_unix (); 56 as_byteshb_blob_t57 hb_bytes_t as_bytes () const { return hb_bytes_t (data, length); } 58 template <typename Type> ashb_blob_t59 const Type* as () const { return as_bytes ().as<Type> (); } 60 61 public: 62 hb_object_header_t header; 63 64 const char *data = nullptr; 65 unsigned int length = 0; 66 hb_memory_mode_t mode = (hb_memory_mode_t) 0; 67 68 void *user_data = nullptr; 69 hb_destroy_func_t destroy = nullptr; 70 }; 71 72 73 /* 74 * hb_blob_ptr_t 75 */ 76 77 template <typename P> 78 struct hb_blob_ptr_t 79 { 80 typedef hb_remove_pointer<P> T; 81 hb_blob_ptr_thb_blob_ptr_t82 hb_blob_ptr_t (hb_blob_t *b_ = nullptr) : b (b_) {} operator =hb_blob_ptr_t83 hb_blob_t * operator = (hb_blob_t *b_) { return b = b_; } operator ->hb_blob_ptr_t84 const T * operator -> () const { return get (); } operator *hb_blob_ptr_t85 const T & operator * () const { return *get (); } operator const C*hb_blob_ptr_t86 template <typename C> operator const C * () const { return get (); } operator const char*hb_blob_ptr_t87 operator const char * () const { return (const char *) get (); } gethb_blob_ptr_t88 const T * get () const { return b->as<T> (); } get_blobhb_blob_ptr_t89 hb_blob_t * get_blob () const { return b.get_raw (); } get_lengthhb_blob_ptr_t90 unsigned int get_length () const { return b.get ()->length; } destroyhb_blob_ptr_t91 void destroy () { hb_blob_destroy (b.get_raw ()); b = nullptr; } 92 93 private: 94 hb_nonnull_ptr_t<hb_blob_t> b; 95 }; 96 97 98 #endif /* HB_BLOB_HH */ 99