From 8a32c9eecbdc907415195eca9ebad47c8acf2df5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 2 May 2019 16:20:18 -0700 Subject: [PATCH] [serialize] Misc getting copy() to work --- src/hb-open-type.hh | 16 +++++++++------- src/hb-serialize.hh | 26 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 6fd6fa652..1deb63a35 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -288,7 +288,7 @@ struct OffsetTo : Offset bool serialize_subset (hb_subset_context_t *c, const Type &src, const void *base, Ts &&...ds) { *this = 0; - if (has_null && &src == &Null (Type)) + if (has_null && &src == _hb_has_null::get_null ()) return false; auto *s = c->serializer; @@ -309,14 +309,16 @@ struct OffsetTo : Offset bool serialize_copy (hb_serialize_context_t *c, const Type &src, const void *base, Ts &&...ds) { *this = 0; - if (has_null && &src == &Null (Type)) + if (has_null && &src == _hb_has_null::get_null ()) return false; c->push (); - c->copy (src, hb_forward (ds)...); + bool ret = c->copy (src, hb_forward (ds)...); c->add_link (*this, c->pop_pack (), base); + + return ret; } bool sanitize_shallow (hb_sanitize_context_t *c, const void *base) const @@ -429,10 +431,10 @@ struct UnsizedArrayOf return_trace (true); } - UnsizedArrayOf* copy (hb_serialize_context_t *c, unsigned count) + UnsizedArrayOf* copy (hb_serialize_context_t *c, unsigned count) const { TRACE_SERIALIZE (this); - auto *out = c->start_embed (*this); + auto *out = c->start_embed (this); if (unlikely (!out->serialize (c, count))) return_trace (nullptr); for (unsigned i = 0; i < count; i++) out->arrayZ[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */ @@ -610,10 +612,10 @@ struct ArrayOf return_trace (true); } - ArrayOf* copy (hb_serialize_context_t *c) + ArrayOf* copy (hb_serialize_context_t *c) const { TRACE_SERIALIZE (this); - auto *out = c->start_embed (*this); + auto *out = c->start_embed (this); unsigned count = len; if (unlikely (!out->serialize (c, count))) return_trace (nullptr); for (unsigned i = 0; i < count; i++) diff --git a/src/hb-serialize.hh b/src/hb-serialize.hh index e7c6445d4..12ba95445 100644 --- a/src/hb-serialize.hh +++ b/src/hb-serialize.hh @@ -71,7 +71,7 @@ struct hb_serialize_context_t { bool is_wide: 1; unsigned position : 31; - int bias; + unsigned bias; objidx_t objidx; }; @@ -323,6 +323,9 @@ struct hb_serialize_context_t allocate_size (alignment - l); } + template + Type *start_embed (const Type &_ HB_UNUSED) const + { return start_embed (); } template Type *start_embed (const Type *_ HB_UNUSED = nullptr) const { @@ -358,33 +361,36 @@ struct hb_serialize_context_t } template - Type *embed (const Type &obj) + Type *embed (const Type *obj) { - unsigned int size = obj.get_size (); + unsigned int size = obj->get_size (); Type *ret = this->allocate_size (size); if (unlikely (!ret)) return nullptr; - memcpy (ret, &obj, size); + memcpy (ret, obj, size); return ret; } + template + Type *embed (const Type &obj) + { return embed (&obj); } template auto - _copy (const Type &obj, hb_priority<1>, Ts &&...ds) const HB_RETURN - (Type *, obj.copy (this, hb_forward (ds)...)) + _copy (const Type &src, hb_priority<1>, Ts &&...ds) HB_RETURN + (Type *, src.copy (this, hb_forward (ds)...)) template auto - _copy (const Type &obj, hb_priority<0>) const -> decltype (&(obj = obj)) + _copy (const Type &src, hb_priority<0>) -> decltype (&(src = src)) { Type *ret = this->allocate_size (sizeof (Type)); if (unlikely (!ret)) return nullptr; - *ret = obj; + *ret = src; return ret; } /* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data * instead of memcpy(). */ template - Type *copy (const Type &obj, Ts &&...ds) - { return _copy (obj, hb_prioritize, hb_forward (ds)...); } + Type *copy (const Type &src, Ts &&...ds) + { return _copy (src, hb_prioritize, hb_forward (ds)...); } template hb_serialize_context_t &operator << (const Type &obj) { embed (obj); return *this; }