From 5b70074edf1c12a9442037d54c03d3025bd93995 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 20 Dec 2018 15:38:59 -0500 Subject: [PATCH] Add hb_assign(obj, value) --- src/hb-machinery.hh | 2 +- src/hb-null.hh | 24 +++++++++++++++++++++++- src/hb-open-type.hh | 5 +++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/hb-machinery.hh b/src/hb-machinery.hh index 1205b1fea..2070fd838 100644 --- a/src/hb-machinery.hh +++ b/src/hb-machinery.hh @@ -390,7 +390,7 @@ struct hb_sanitize_context_t : { if (this->may_edit (obj, hb_static_size (Type))) { - const_cast (obj)->set (v); + hb_assign (* const_cast (obj), v); return true; } return false; diff --git a/src/hb-null.hh b/src/hb-null.hh index 7ea2bab16..8a3e0d405 100644 --- a/src/hb-null.hh +++ b/src/hb-null.hh @@ -59,7 +59,10 @@ struct hb_null_size { enum { value = _hb_null_size >::value }; }; #define hb_null_size(T) hb_null_size::value -/* This doesn't belong here, but since is copy/paste from above, put it here. */ +/* These doesn't belong here, but since is copy/paste from above, put it here. */ + +/* hb_static_size (T) + * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */ template struct _hb_static_size @@ -74,6 +77,25 @@ struct hb_static_size #define hb_static_size(T) hb_static_size::value +/* hb_assign (obj, value) + * Calls obj.set (value) if obj.min_size is defined and value has different type + * from obj, or obj = v otherwise. */ + +template +struct _hb_assign +{ static inline void value (T &o, const V v) { o = v; } }; +template +struct _hb_assign > +{ static inline void value (T &o, const V v) { o.set (v); } }; +template +struct _hb_assign > +{ static inline void value (T &o, const T v) { o = v; } }; + +template +static inline void hb_assign (T &o, const V v) +{ _hb_assign >::value (o, v); }; + + /* * Null() */ diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 6517c6bfe..7f88279a7 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -553,12 +553,13 @@ struct ArrayOf if (unlikely (!c->extend (*this))) return_trace (false); return_trace (true); } - bool serialize (hb_serialize_context_t *c, hb_array_t items) + template + bool serialize (hb_serialize_context_t *c, hb_array_t items) { TRACE_SERIALIZE (this); if (unlikely (!serialize (c, items.len))) return_trace (false); for (unsigned int i = 0; i < items.len; i++) - arrayZ[i] = items[i]; + hb_assign (arrayZ[i], items[i]); return_trace (true); }