qxLib
string_setup.h
Go to the documentation of this file.
1 /**
2 
3  @file string_setup.h
4  @author Khrapov
5  @date 10.06.2023
6  @copyright (c) Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/macros/config.h>
13 #include <qx/meta/tuple_utils.h>
14 
15 
16 #define QX_CHAR_T_CHAR 0
17 #define QX_CHAR_T_WCHAR_T 1
18 
19 #ifndef QX_CONF_CHAR
20  #define QX_CONF_CHAR QX_CHAR_T_WCHAR_T
21 #endif
22 
23 
24 #define QX_FMT_LIB_FMT 0
25 #define QX_FMT_LIB_STD 1
26 
27 #ifndef QX_CONF_FMT_LIB
28  #if __has_include("fmt/format.h")
29  #define QX_CONF_FMT_LIB QX_FMT_LIB_FMT
30  #else
31  #define QX_CONF_FMT_LIB QX_FMT_LIB_STD
32  #endif
33 #endif
34 
35 
36 #define QX_ALL_CHAR_TYPES char, wchar_t
37 
38 namespace qx::details
39 {
40 
41 using all_char_types = std::tuple<QX_ALL_CHAR_TYPES>;
42 
43 }
44 
45 
46 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
47  #define QX_CHAR_TYPE char
48  #define _QXT(quote) quote
49 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
50  #define QX_CHAR_TYPE wchar_t
51  #define _QXT(quote) L##quote
52 
53  #if !defined(QX_CONF_UNICODE_MACRO)
54  #define QX_CONF_UNICODE_MACRO 1
55  #endif
56 
57  #if QX_CONF_UNICODE_MACRO && QX_WIN
58  #define UNICODE
59  #endif
60 #else
61  #error Unsupported char type
62 #endif
63 
64 #define QXT(quote) _QXT(quote)
65 
66 namespace qx
67 {
68 
69 using char_type = QX_CHAR_TYPE;
70 using forbidden_char_types = tuple_utils::remove_t<details::all_char_types, std::tuple<char_type>>;
71 
72 } // namespace qx
73 
74 #if QX_CONF_FMT_LIB == QX_FMT_LIB_FMT
75 QX_PUSH_SUPPRESS_ALL_WARNINGS();
76  #define FMT_UNICODE 0
77  #define FMT_HEADER_ONLY 1
78  #include "fmt/format.h"
79  #include "fmt/xchar.h"
80 QX_POP_SUPPRESS_WARNINGS();
81  #define QX_FMT_NS fmt
82  #define _QX_FMT_DISABLE_RANGE_FORMATTING(T) \
83  template<class char_t> \
84  struct fmt::range_format_kind<T, char_t> \
85  : std::integral_constant<fmt::range_format, fmt::range_format::disabled> \
86  { \
87  }
88  #define _QX_FMT_DISABLE_TUPLE_FORMATTING(T) \
89  template<class char_t> \
90  struct fmt::is_tuple_formattable<T, char_t> : std::false_type \
91  { \
92  }
93 #elif QX_CONF_FMT_LIB == QX_FMT_LIB_STD
94  #include <format>
95  #define QX_FMT_NS std
96  #define _QX_FMT_DISABLE_RANGE_FORMATTING(T) \
97  template<> \
98  inline constexpr std::range_format std::format_kind<T> = std::range_format::disabled
99  #define _QX_FMT_DISABLE_TUPLE_FORMATTING(T)
100 #else
101  #error No fmt lib selected
102 #endif
103 
104 /**
105  @brief Disable built-in range formatting for a type
106  @details If T supports range formatting, this macro disables it.
107  This is useful for types that are ranges but should not be formatted as such.
108  @param T - type to disable range formatting for
109 **/
110 #define QX_FMT_DISABLE_RANGE_FORMATTING(T) _QX_FMT_DISABLE_RANGE_FORMATTING(T)
111 
112 /**
113  @brief Disable built-in tuple formatting for a type
114  @details If T supports tuple formatting, this macro disables it.
115  This is useful for types that are tuples but should not be formatted as such.
116  @param T - type to disable tuple formatting for
117 **/
118 #define QX_FMT_DISABLE_TUPLE_FORMATTING(T) _QX_FMT_DISABLE_TUPLE_FORMATTING(T)
Contains utils for working with std::tuple.