26 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT) 27 #error "Only <final/final.h> can be included directly." 30 #include <sys/types.h> 45 #include "final/eventloop/pipedata.h" 47 #if (defined(__APPLE__) && defined(__MACH__)) || defined(__OpenBSD__) 48 #define USE_KQUEUE_TIMER 51 #if !(defined(__APPLE__) && defined(__MACH__)) && !(defined(__OpenBSD__)) 52 #define USE_POSIX_TIMER 58 #define HAVE_BUILTIN(x) __has_builtin(x) 60 #define HAVE_BUILTIN(x) 0 63 #define badAllocOutput(object_name) \ 64 std::clog << FLog::LogLevel::Error \ 65 << __FILE__ << ":" << __LINE__ \ 66 << ": Not enough memory to alloc " \ 69 << __func__ << std::endl // ; 71 using uChar =
unsigned char;
72 using uShort =
unsigned short;
73 using uInt =
unsigned int;
74 using uLong =
unsigned long;
75 using uInt8 = std::uint8_t;
76 using uInt16 = std::uint16_t;
77 using uInt32 = std::uint32_t;
78 using uInt64 = std::uint64_t;
80 using sInt =
signed int;
81 using sLong =
signed long;
82 using sInt8 = std::int8_t;
83 using sInt16 = std::int16_t;
84 using sInt32 = std::int32_t;
85 using sInt64 = std::int64_t;
87 using lDouble =
long double;
88 using TimeValue = std::chrono::time_point<std::chrono::system_clock>;
89 using FCall = std::function<void()>;
101 constexpr
auto operator () (
const T& x)
const noexcept ->
bool 107 template <
typename T>
110 constexpr
auto operator () (
const T&)
const noexcept ->
bool 119 template<
typename CharT>
120 constexpr
auto is7bit (CharT ch) noexcept ->
bool 122 using char_type = std::make_unsigned_t<CharT>;
123 return static_cast<char_type
>(ch) < 128;
127 inline auto isPrintable (
char ch) noexcept ->
bool 129 return std::isprint(static_cast<unsigned char>(ch));
132 inline auto isPrintable (
wchar_t ch) noexcept ->
bool 134 return std::iswprint(std::wint_t(ch));
138 template <
typename StringT>
139 constexpr
auto C_STR (StringT&&
string) noexcept
141 return const_cast<char*
>(std::forward<StringT>(string));
144 template <
typename T>
145 constexpr
auto isNegative (
const T& x) noexcept ->
bool 150 template <
typename T>
153 constexpr
explicit operator int ()
const noexcept
155 return std::numeric_limits<T>::digits10;
159 template <
typename T>
162 constexpr
auto operator () (
const T& mt)
const noexcept -> std::size_t
164 using underlying_type = std::underlying_type_t<T>;
165 return std::hash<underlying_type>{}(underlying_type(mt));
169 template <
typename CharT>
170 constexpr
auto stringLength (
const CharT* s) noexcept -> std::size_t
172 return std::char_traits<CharT>::length(s);
175 template <
typename CharT>
176 using remove_ptr_t = std::remove_pointer_t<CharT>;
178 template <
typename CharT>
179 using remove_ptr_cv_t = std::remove_cv_t<remove_ptr_t<CharT>>;
181 template <
typename CharT>
182 using is_char_based_ptr = std::is_same<char, remove_ptr_cv_t<CharT>>;
184 template <
typename CharT>
185 using is_wchar_based_ptr = std::is_same<wchar_t, remove_ptr_cv_t<CharT>>;
187 template <
typename CharT>
188 using remove_ref_t = std::remove_reference_t<CharT>;
190 template <
typename CharT>
191 using remove_ref_extent_t = std::remove_extent_t<remove_ref_t<CharT>>;
193 template <
typename CharT>
194 using remove_ref_extent_cv_t = std::remove_cv_t<remove_ref_extent_t<CharT>>;
196 template <
typename CharT>
197 using is_char_based_array =
typename std::is_same<char, remove_ref_extent_cv_t<CharT>>;
199 template <
typename CharT>
200 using is_wchar_based_array =
typename std::is_same<wchar_t, remove_ref_extent_cv_t<CharT>>;
202 template <
typename CharT>
203 using enable_if_char_ptr_t =
205 std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value
208 template <
typename CharT>
209 using enable_if_char_array_t =
211 std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value
214 template <
typename CharT>
215 using enable_if_CString_t =
217 (std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value)
218 || (std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value)
221 template <
typename CharT>
223 : std::integral_constant<bool
224 , (std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value)
225 || (std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value)>
228 template <
typename CharT>
229 using enable_if_wchar_ptr_t =
231 std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value
234 template <
typename CharT>
235 using enable_if_wchar_array_t =
237 std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value
240 template <
typename CharT>
241 using enable_if_WCString_t =
243 (std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value)
244 || (std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value)
247 template <
typename CharT>
249 : std::integral_constant<bool
250 , (std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value)
251 || (std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value)>
254 template <
typename NumT>
255 using enable_if_arithmetic_without_char_t =
256 std::enable_if_t< std::is_arithmetic<NumT>::value
257 && ! std::is_same<char, NumT>::value
287 uInt8 crossed_out : 1;
288 uInt8 dbl_underline : 1;
289 uInt8 alt_charset : 1;
290 uInt8 pc_charset : 1;
291 uInt8 transparent : 1;
292 uInt8 color_overlay : 1;
293 uInt8 inherit_background : 1;
295 uInt8 no_changes : 1;
297 uInt8 fullwidth_padding : 1;
298 uInt8 char_width : 2;
304 #if HAVE_BUILTIN(__builtin_bit_cast) 305 constexpr
auto getFAttributeByte (
const FCharAttribute& fchar_attr
306 , std::size_t index ) noexcept -> uInt8
308 return (__builtin_bit_cast(uInt32, fchar_attr) >> (index << 3)) & 0xff;
312 , std::size_t index ) noexcept -> uInt8
315 std::memcpy (&byte, reinterpret_cast<const uInt8*>(&fchar_attr) + index,
sizeof(uInt8));
320 #if HAVE_BUILTIN(__builtin_bit_cast) 322 ,
const std::size_t index
323 ,
const uInt8 value ) noexcept
328 const auto clear_value = __builtin_bit_cast(uInt32, fchar_attr) & ~(uInt32(0xff) << (index << 3));
329 const auto ret = clear_value | (uInt32(value) << (index << 3));
335 , uInt8 value ) noexcept
338 std::memcpy(reinterpret_cast<uInt8*>(&fchar_attr) + index, &value,
sizeof(uInt8));
342 #if HAVE_BUILTIN(__builtin_bit_cast) 343 constexpr
auto getFAttributeWord (
const FCharAttribute& fchar_attr) noexcept -> uInt32
345 return __builtin_bit_cast(uInt32, fchar_attr);
348 inline auto getFAttributeWord (
const FCharAttribute& fchar_attr) noexcept -> uInt32
351 std::memcpy(&word, &fchar_attr,
sizeof(word));
356 #if HAVE_BUILTIN(__builtin_bit_cast) 357 constexpr
auto WordToFAttribute (uInt32 word) noexcept ->
FCharAttribute 362 inline auto WordToFAttribute (uInt32 word) noexcept ->
FCharAttribute 365 std::memcpy(&fchar_attr, &word,
sizeof(fchar_attr));
377 static constexpr std::size_t UNICODE_MAX = 5;
379 using FUnicode = std::array<wchar_t, UNICODE_MAX>;
381 enum class FColor : uInt16;
386 #if HAVE_BUILTIN(__builtin_bit_cast) 387 constexpr auto isFUnicodeEqual (const FUnicode& lhs, const FUnicode& rhs) noexcept -> bool
390 if ( lhs.size() != rhs.size() )
394 return std::memcmp(&lhs[0], &rhs[0], lhs.size() *
sizeof(wchar_t)) == 0;
397 inline auto isFUnicodeEqual (
const FUnicode& lhs,
const FUnicode& rhs) noexcept ->
bool 399 static_assert (
sizeof(lhs) ==
sizeof(rhs) ,
"Both sides are different sizes.");
401 return std::wmemcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
406 #if HAVE_BUILTIN(__builtin_bit_cast) 407 constexpr
auto getCompareBitMask() noexcept -> uInt32
409 constexpr
const FAttribute mask {{ 0xff, 0xff, 0x04, 0x00 }};
410 return __builtin_bit_cast(uInt32, mask.byte);
413 inline auto getCompareBitMask() noexcept -> uInt32
415 const FAttribute mask {{ 0xff, 0xff, 0x04, 0x00 }};
417 std::memcpy(&word, &mask,
sizeof(word));
426 FUnicode encoded_char{};
431 #if HAVE_BUILTIN(__builtin_bit_cast) 436 auto operator == (
const FChar& lhs,
const FChar& rhs) noexcept ->
bool 438 if ( ! isFUnicodeEqual(lhs.ch, rhs.ch)
439 || lhs.fg_color != rhs.fg_color
440 || lhs.bg_color != rhs.bg_color )
443 const auto mask = getCompareBitMask();
445 if ( (lhs.attr.word & mask) != (rhs.attr.word & mask) )
451 #if HAVE_BUILTIN(__builtin_bit_cast) 456 auto operator != (
const FChar& lhs,
const FChar& rhs) noexcept ->
bool 458 return ! ( lhs == rhs );
Definition: class_template.cpp:25