Imported Upstream version 2.4.0
[platform/upstream/harfbuzz.git] / src / hb-null.hh
1 /*
2  * Copyright © 2018  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_NULL_HH
28 #define HB_NULL_HH
29
30 #include "hb.hh"
31
32
33 /*
34  * Static pools
35  */
36
37 /* Global nul-content Null pool.  Enlarge as necessary. */
38
39 #define HB_NULL_POOL_SIZE 9880
40
41 /* Use SFINAE to sniff whether T has min_size; in which case return T::null_size,
42  * otherwise return sizeof(T). */
43
44 /* The hard way...
45  * https://stackoverflow.com/questions/7776448/sfinae-tried-with-bool-gives-compiler-error-template-argument-tvalue-invol
46  */
47
48 template<bool> struct _hb_bool_type {};
49
50 template <typename T, typename B>
51 struct _hb_null_size
52 { enum { value = sizeof (T) }; };
53 template <typename T>
54 struct _hb_null_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
55 { enum { value = T::null_size }; };
56
57 template <typename T>
58 struct hb_null_size
59 { enum { value = _hb_null_size<T, _hb_bool_type<true> >::value }; };
60 #define hb_null_size(T) hb_null_size<T>::value
61
62 /* These doesn't belong here, but since is copy/paste from above, put it here. */
63
64 /* hb_static_size (T)
65  * Returns T::static_size if T::min_size is defined, or sizeof (T) otherwise. */
66
67 template <typename T, typename B>
68 struct _hb_static_size
69 { enum { value = sizeof (T) }; };
70 template <typename T>
71 struct _hb_static_size<T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
72 { enum { value = T::static_size }; };
73
74 template <typename T>
75 struct hb_static_size
76 { enum { value = _hb_static_size<T, _hb_bool_type<true> >::value }; };
77 #define hb_static_size(T) hb_static_size<T>::value
78
79
80 /* hb_assign (obj, value)
81  * Calls obj.set (value) if obj.min_size is defined and value has different type
82  * from obj, or obj = v otherwise. */
83
84 template <typename T, typename V, typename B>
85 struct _hb_assign
86 { static inline void value (T &o, const V v) { o = v; } };
87 template <typename T, typename V>
88 struct _hb_assign<T, V, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
89 { static inline void value (T &o, const V v) { o.set (v); } };
90 template <typename T>
91 struct _hb_assign<T, T, _hb_bool_type<(bool) (1 + (unsigned int) T::min_size)> >
92 { static inline void value (T &o, const T v) { o = v; } };
93
94 template <typename T, typename V>
95 static inline void hb_assign (T &o, const V v)
96 { _hb_assign<T, V, _hb_bool_type<true> >::value (o, v); }
97
98
99 /*
100  * Null()
101  */
102
103 extern HB_INTERNAL
104 hb_vector_size_impl_t const _hb_NullPool[(HB_NULL_POOL_SIZE + sizeof (hb_vector_size_impl_t) - 1) / sizeof (hb_vector_size_impl_t)];
105
106 /* Generic nul-content Null objects. */
107 template <typename Type>
108 struct Null {
109   static Type const & get_null ()
110   {
111     static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
112     return *reinterpret_cast<Type const *> (_hb_NullPool);
113   }
114 };
115 template <typename QType>
116 struct NullHelper
117 {
118   typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
119   static const Type & get_null () { return Null<Type>::get_null (); }
120 };
121 #define Null(Type) NullHelper<Type>::get_null ()
122
123 /* Specializations for arbitrary-content Null objects expressed in bytes. */
124 #define DECLARE_NULL_NAMESPACE_BYTES(Namespace, Type) \
125         } /* Close namespace. */ \
126         extern HB_INTERNAL const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]; \
127         template <> \
128         struct Null<Namespace::Type> { \
129           static Namespace::Type const & get_null () { \
130             return *reinterpret_cast<const Namespace::Type *> (_hb_Null_##Namespace##_##Type); \
131           } \
132         }; \
133         namespace Namespace { \
134         static_assert (true, "Just so we take semicolon after.")
135 #define DEFINE_NULL_NAMESPACE_BYTES(Namespace, Type) \
136         const unsigned char _hb_Null_##Namespace##_##Type[Namespace::Type::null_size]
137
138 /* Specializations for arbitrary-content Null objects expressed as struct initializer. */
139 #define DECLARE_NULL_INSTANCE(Type) \
140         extern HB_INTERNAL const Type _hb_Null_##Type; \
141         template <> \
142         struct Null<Type> { \
143           static Type const & get_null () { \
144             return _hb_Null_##Type; \
145           } \
146         }; \
147         static_assert (true, "Just so we take semicolon after.")
148 #define DEFINE_NULL_INSTANCE(Type) \
149         const Type _hb_Null_##Type
150
151 /* Global writable pool.  Enlarge as necessary. */
152
153 /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
154  * for correct operation. It only exist to catch and divert program logic bugs instead of
155  * causing bad memory access. So, races there are not actually introducing incorrectness
156  * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
157 extern HB_INTERNAL
158 /*thread_local*/ hb_vector_size_impl_t _hb_CrapPool[(HB_NULL_POOL_SIZE + sizeof (hb_vector_size_impl_t) - 1) / sizeof (hb_vector_size_impl_t)];
159
160 /* CRAP pool: Common Region for Access Protection. */
161 template <typename Type>
162 static inline Type& Crap () {
163   static_assert (hb_null_size (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
164   Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
165   memcpy (obj, &Null(Type), sizeof (*obj));
166   return *obj;
167 }
168 template <typename QType>
169 struct CrapHelper
170 {
171   typedef typename hb_remove_const (typename hb_remove_reference (QType)) Type;
172   static Type & get_crap () { return Crap<Type> (); }
173 };
174 #define Crap(Type) CrapHelper<Type>::get_crap ()
175
176 template <typename Type>
177 struct CrapOrNullHelper {
178   static Type & get () { return Crap(Type); }
179 };
180 template <typename Type>
181 struct CrapOrNullHelper<const Type> {
182   static const Type & get () { return Null(Type); }
183 };
184 #define CrapOrNull(Type) CrapOrNullHelper<Type>::get ()
185
186
187 /*
188  * hb_nonnull_ptr_t
189  */
190
191 template <typename P>
192 struct hb_nonnull_ptr_t
193 {
194   typedef typename hb_remove_pointer (P) T;
195
196   hb_nonnull_ptr_t (T *v_ = nullptr) : v (v_) {}
197   T * operator = (T *v_)   { return v = v_; }
198   T * operator -> () const { return get (); }
199   T & operator * () const  { return *get (); }
200   T ** operator & () const { return &v; }
201   /* Only auto-cast to const types. */
202   template <typename C> operator const C * () const { return get (); }
203   operator const char * () const { return (const char *) get (); }
204   T * get () const { return v ? v : const_cast<T *> (&Null(T)); }
205   T * get_raw () const { return v; }
206
207   T *v;
208 };
209
210
211 #endif /* HB_NULL_HH */