[lazy] Make hb_lazy_loader_t<> more usable

This commit is contained in:
Behdad Esfahbod 2018-08-12 16:57:06 -07:00
parent bb905e9afc
commit 6901090945
2 changed files with 37 additions and 27 deletions

View File

@ -685,9 +685,8 @@ hb_ft_font_create_referenced (FT_Face ft_face)
static void free_static_ft_library (void); static void free_static_ft_library (void);
static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_ft_library_lazy_loader_t, static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_ptr_t<FT_Library>::value,
void, 0, hb_ft_library_lazy_loader_t>
hb_remove_ptr_t<FT_Library>::value>
{ {
static inline FT_Library create (void) static inline FT_Library create (void)
{ {
@ -705,7 +704,7 @@ static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_ft_library_lazy_
{ {
FT_Done_FreeType (l); FT_Done_FreeType (l);
} }
static inline const FT_Library get_null (void) static inline FT_Library get_null (void)
{ {
return nullptr; return nullptr;
} }

View File

@ -610,20 +610,27 @@ struct hb_data_wrapper_t
template <> template <>
struct hb_data_wrapper_t<void, 0> struct hb_data_wrapper_t<void, 0>
{ {
template <typename Stored, typename Subclass> template <typename Stored, typename Funcs>
inline Stored * call_create (void) const inline Stored * call_create (void) const
{ {
return Subclass::create (); return Funcs::create ();
} }
}; };
template <typename Subclass, template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
typename Data, template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
unsigned int WheresData,
typename Returned, template <typename Returned,
typename Subclass = void,
typename Data = void,
unsigned int WheresData = 0,
typename Stored = Returned> typename Stored = Returned>
struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData> struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
{ {
typedef typename hb_non_void_t<Subclass,
hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
>::value Funcs;
inline void init0 (void) {} /* Init, when memory is already set to 0. No-op for us. */ inline void init0 (void) {} /* Init, when memory is already set to 0. No-op for us. */
inline void init (void) { instance.set_relaxed (nullptr); } inline void init (void) { instance.set_relaxed (nullptr); }
inline void fini (void) inline void fini (void)
@ -641,15 +648,15 @@ struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
inline Stored * do_create (void) const inline Stored * do_create (void) const
{ {
Stored *p = this->template call_create<Stored, Subclass> (); Stored *p = this->template call_create<Stored, Funcs> ();
if (unlikely (!p)) if (unlikely (!p))
p = const_cast<Stored *> (Subclass::get_null ()); p = const_cast<Stored *> (Funcs::get_null ());
return p; return p;
} }
static inline void do_destroy (Stored *p) static inline void do_destroy (Stored *p)
{ {
if (p && p != Subclass::get_null ()) if (p && p != Funcs::get_null ())
Subclass::destroy (p); Funcs::destroy (p);
} }
inline const Returned * operator -> (void) const { return get (); } inline const Returned * operator -> (void) const { return get (); }
@ -687,8 +694,8 @@ struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
do_destroy (p); do_destroy (p);
} }
inline const Returned * get (void) const { return Subclass::convert (get_stored ()); } inline const Returned * get (void) const { return Funcs::convert (get_stored ()); }
inline Returned * get_unconst (void) const { return const_cast<Returned *> (Subclass::convert (get_stored ())); } inline Returned * get_unconst (void) const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
/* To be possibly overloaded by subclasses. */ /* To be possibly overloaded by subclasses. */
static inline const Returned* convert (const Stored *p) { return p; } static inline const Returned* convert (const Stored *p) { return p; }
@ -703,6 +710,13 @@ struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
p->init (data); p->init (data);
return p; return p;
} }
static inline Stored *create (void)
{
Stored *p = (Stored *) calloc (1, sizeof (Stored));
if (likely (p))
p->init ();
return p;
}
static inline void destroy (Stored *p) static inline void destroy (Stored *p)
{ {
p->fini (); p->fini ();
@ -717,14 +731,15 @@ struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
/* Specializations. */ /* Specializations. */
template <unsigned int WheresFace, typename T> template <unsigned int WheresFace, typename T>
struct hb_face_lazy_loader_t : hb_lazy_loader_t<hb_face_lazy_loader_t<WheresFace, T>, struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
hb_face_t, WheresFace, hb_face_lazy_loader_t<WheresFace, T>,
T> {}; hb_face_t, WheresFace> {};
template <typename T, unsigned int WheresFace> template <typename T, unsigned int WheresFace>
struct hb_table_lazy_loader_t : hb_lazy_loader_t<hb_table_lazy_loader_t<T, WheresFace>, struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
hb_table_lazy_loader_t<T, WheresFace>,
hb_face_t, WheresFace, hb_face_t, WheresFace,
T, hb_blob_t> hb_blob_t>
{ {
static inline hb_blob_t *create (hb_face_t *face) static inline hb_blob_t *create (hb_face_t *face)
{ {
@ -750,9 +765,7 @@ struct hb_table_lazy_loader_t : hb_lazy_loader_t<hb_table_lazy_loader_t<T, Where
}; };
template <typename Subclass> template <typename Subclass>
struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass, struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass>
void, 0,
hb_font_funcs_t>
{ {
static inline void destroy (hb_font_funcs_t *p) static inline void destroy (hb_font_funcs_t *p)
{ {
@ -764,9 +777,7 @@ struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass,
} }
}; };
template <typename Subclass> template <typename Subclass>
struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<Subclass, struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass>
void, 0,
hb_unicode_funcs_t>
{ {
static inline void destroy (hb_unicode_funcs_t *p) static inline void destroy (hb_unicode_funcs_t *p)
{ {