1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/***********************************************************************
* ftypes.h - Implements global data types *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2017-2023 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#ifndef FTYPES_H
#define FTYPES_H
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
#error "Only <final/final.h> can be included directly."
#endif
#include <sys/types.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <array>
#include <chrono>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#define null nullptr
#define badAllocOutput(object_name) \
std::clog << FLog::LogLevel::Error \
<< __FILE__ << ":" << __LINE__ \
<< ": Not enough memory to alloc " \
<< (object_name) \
<< " in " \
<< __func__ << std::endl // ;
using uChar = unsigned char;
using uShort = unsigned short;
using uInt = unsigned int;
using uLong = unsigned long;
using uInt8 = std::uint8_t;
using uInt16 = std::uint16_t;
using uInt32 = std::uint32_t;
using uInt64 = std::uint64_t;
using sInt = signed int;
using sLong = signed long;
using sInt8 = std::int8_t;
using sInt16 = std::int16_t;
using sInt32 = std::int32_t;
using sInt64 = std::int64_t;
using lDouble = long double;
using TimeValue = std::chrono::time_point<std::chrono::system_clock>;
using FCall = std::function<void()>;
namespace finalcut
{
namespace internal
{
template <typename T
, bool is_signed>
struct is_negative
{
constexpr auto operator () (const T& x) const noexcept -> bool
{
return x < 0;
}
};
template <typename T>
struct is_negative<T, false>
{
constexpr auto operator () (const T&) const noexcept -> bool
{
return false;
}
};
} // namespace internal
// Check for 7-bit ASCII
template<typename CharT>
constexpr auto is7bit (CharT ch) noexcept -> bool
{
using char_type = std::make_unsigned_t<CharT>;
return static_cast<char_type>(ch) < 128;
}
// Typecast to c-string
template <typename StringT>
constexpr auto C_STR (StringT&& string) noexcept
{
return const_cast<char*>(std::forward<StringT>(string));
}
template <typename T>
constexpr auto isNegative (const T& x) noexcept -> bool
{
return internal::is_negative<T, std::numeric_limits<T>::is_signed>{}(x);
}
template <typename T>
struct getPrecision
{
constexpr explicit operator int () const noexcept
{
return std::numeric_limits<T>::digits10;
}
};
template <typename T>
struct EnumHash
{
constexpr auto operator () (const T& mt) const noexcept -> std::size_t
{
using underlying_type = std::underlying_type_t<T>;
return std::hash<underlying_type>{}(underlying_type(mt));
}
};
template <typename CharT>
constexpr auto stringLength (const CharT* s) noexcept -> std::size_t
{
return std::char_traits<CharT>::length(s);
}
template <typename CharT>
using remove_ptr_t = std::remove_pointer_t<CharT>;
template <typename CharT>
using remove_ptr_cv_t = std::remove_cv_t<remove_ptr_t<CharT>>;
template <typename CharT>
using is_char_based_ptr = std::is_same<char, remove_ptr_cv_t<CharT>>;
template <typename CharT>
using is_wchar_based_ptr = std::is_same<wchar_t, remove_ptr_cv_t<CharT>>;
template <typename CharT>
using remove_ref_t = std::remove_reference_t<CharT>;
template <typename CharT>
using remove_ref_extent_t = std::remove_extent_t<remove_ref_t<CharT>>;
template <typename CharT>
using remove_ref_extent_cv_t = std::remove_cv_t<remove_ref_extent_t<CharT>>;
template <typename CharT>
using is_char_based_array = typename std::is_same<char, remove_ref_extent_cv_t<CharT>>;
template <typename CharT>
using is_wchar_based_array = typename std::is_same<wchar_t, remove_ref_extent_cv_t<CharT>>;
template <typename CharT>
using enable_if_char_ptr_t =
std::enable_if_t<
std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value
, std::nullptr_t>;
template <typename CharT>
using enable_if_char_array_t =
std::enable_if_t<
std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value
, std::nullptr_t>;
template <typename CharT>
using enable_if_CString_t =
std::enable_if_t<
(std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value)
|| (std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value)
, std::nullptr_t>;
template <typename CharT>
struct isCString
: std::integral_constant<bool
, (std::is_pointer<CharT>::value && is_char_based_ptr<CharT>::value)
|| (std::is_array<remove_ref_t<CharT>>::value && is_char_based_array<CharT>::value)>
{ };
template <typename CharT>
using enable_if_wchar_ptr_t =
std::enable_if_t<
std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value
, std::nullptr_t>;
template <typename CharT>
using enable_if_wchar_array_t =
std::enable_if_t<
std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value
, std::nullptr_t>;
template <typename CharT>
using enable_if_WCString_t =
std::enable_if_t<
(std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value)
|| (std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value)
, std::nullptr_t>;
template <typename CharT>
struct isWCString
: std::integral_constant<bool
, (std::is_pointer<CharT>::value && is_wchar_based_ptr<CharT>::value)
|| (std::is_array<remove_ref_t<CharT>>::value && is_wchar_based_array<CharT>::value)>
{ };
template <typename NumT>
using enable_if_arithmetic_without_char_t =
std::enable_if_t< std::is_arithmetic<NumT>::value
&& ! std::is_same<char, NumT>::value
, std::nullptr_t>;
struct TCapAttributes
{
uInt8 p1 : 1; // Standout
uInt8 p2 : 1; // Underline
uInt8 p3 : 1; // Reverse
uInt8 p4 : 1; // Blink
uInt8 p5 : 1; // Dim
uInt8 p6 : 1; // Bold
uInt8 p7 : 1; // Invisible
uInt8 p8 : 1; // Protected
uInt8 p9 : 1; // Alternate charset
uInt8 : 7; // padding bits
};
struct FCharAttribute
{
// Attribute byte #0
uInt8 bold : 1; // bold
uInt8 dim : 1; // dim
uInt8 italic : 1; // italic
uInt8 underline : 1; // underline
uInt8 blink : 1; // blink
uInt8 reverse : 1; // reverse
uInt8 standout : 1; // standout
uInt8 invisible : 1; // invisible
// Attribute byte #1
uInt8 protect : 1; // protect mode
uInt8 crossed_out : 1; // crossed out
uInt8 dbl_underline : 1; // double underline
uInt8 alt_charset : 1; // alternate character set (vt100)
uInt8 pc_charset : 1; // pc character set (CP437)
uInt8 transparent : 1; // transparent
uInt8 color_overlay : 1; // color overlay
uInt8 inherit_background : 1; // inherit background
// Attribute byte #2
uInt8 no_changes : 1; // no changes required
uInt8 printed : 1; // is printed to VTerm
uInt8 fullwidth_padding : 1; // padding char (after a full-width char)
uInt8 char_width : 2; // number of character cells on screen
uInt8 : 3; // padding bits
// Attribute byte #3
uInt8 : 8; // padding byte
};
union FAttribute
{
uInt8 byte[4];
uInt32 word;
FCharAttribute bit;
};
static constexpr std::size_t UNICODE_MAX = 5;
using FUnicode = std::array<wchar_t, UNICODE_MAX>;
enum class FColor : uInt16; // forward declaration
struct FChar
{
FUnicode ch{}; // Character code
FUnicode encoded_char{}; // Encoded output character
FColor fg_color{}; // Foreground color
FColor bg_color{}; // Background color
FAttribute attr{}; // Attributes
};
// FChar operator functions
//----------------------------------------------------------------------
constexpr auto isFUnicodeEqual (const FUnicode& lhs, const FUnicode& rhs) noexcept -> bool
{
const auto* l_iter = std::cbegin(lhs);
const auto* r_iter = std::cbegin(rhs);
const auto* const l_last = std::cend(lhs);
while ( *l_iter == *r_iter && *l_iter != L'\0' && l_iter != l_last )
{
++l_iter;
++r_iter;
}
return *l_iter == *r_iter;
}
//----------------------------------------------------------------------
constexpr auto operator == (const FChar& lhs, const FChar& rhs) noexcept -> bool
{
return isFUnicodeEqual(lhs.ch, rhs.ch)
&& lhs.fg_color == rhs.fg_color
&& lhs.bg_color == rhs.bg_color
&& lhs.attr.byte[0] == rhs.attr.byte[0]
&& lhs.attr.byte[1] == rhs.attr.byte[1]
&& lhs.attr.bit.fullwidth_padding \
== rhs.attr.bit.fullwidth_padding;
}
//----------------------------------------------------------------------
constexpr auto operator != (const FChar& lhs, const FChar& rhs) noexcept -> bool
{
return ! ( lhs == rhs );
}
} // namespace finalcut
#endif // FTYPES_H
|