TreeFrogFramework  2.8
tglobal.h
Go to the documentation of this file.
1 #pragma once
2 constexpr auto TF_VERSION_STR = "2.8.0";
3 constexpr auto TF_VERSION_NUMBER = 0x020800;
4 constexpr auto TF_SRC_REVISION = 2834;
5 
6 #include <QMetaType>
7 #include <QIODevice>
8 #include <QtGlobal>
9 
10 
11 #define T_DEFINE_CONTROLLER(TYPE) T_DEFINE_TYPE(TYPE)
12 #define T_DEFINE_VIEW(TYPE) T_DEFINE_TYPE(TYPE)
13 #define T_DEFINE_TYPE(TYPE) \
14  class Static##TYPE##Definition { \
15  public: \
16  Static##TYPE##Definition() noexcept \
17  { \
18  Tf::objectFactories()->insert(QByteArray(#TYPE).toLower(), []() { return new TYPE(); }); \
19  } \
20  }; \
21  static Static##TYPE##Definition _static##TYPE##Definition;
22 
23 #if QT_VERSION < 0x060000
24 #define T_REGISTER_STREAM_OPERATORS(TYPE) \
25  class Static##TYPE##Instance { \
26  public: \
27  Static##TYPE##Instance() noexcept \
28  { \
29  qRegisterMetaTypeStreamOperators<TYPE>(#TYPE); \
30  } \
31  }; \
32  static Static##TYPE##Instance _static##TYPE##Instance;
33 #else
34 // do no longer exist in qt6, qRegisterMetaTypeStreamOperators().
35 #define T_REGISTER_STREAM_OPERATORS(TYPE)
36 #endif
37 
38 #define T_DEFINE_PROPERTY(TYPE, PROPERTY) \
39  inline void set##PROPERTY(const TYPE &v__) noexcept { PROPERTY = v__; } \
40  inline TYPE get##PROPERTY() const noexcept { return PROPERTY; }
41 
42 
43 #if defined(Q_CC_GNU) && !defined(__INSURE__)
44 #if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG)
45 #define T_ATTRIBUTE_FORMAT(A, B) __attribute__((format(gnu_printf, (A), (B))))
46 #else
47 #define T_ATTRIBUTE_FORMAT(A, B) __attribute__((format(printf, (A), (B))))
48 #endif
49 #else
50 #define T_ATTRIBUTE_FORMAT(A, B)
51 #endif
52 
53 
54 #define T_EXPORT(VAR) \
55  do { \
56  QVariant ___##VAR##_; \
57  ___##VAR##_.setValue(VAR); \
58  Tf::currentController()->exportVariant(QLatin1String(#VAR), (___##VAR##_), true); \
59  } while (0)
60 #define texport(VAR) T_EXPORT(VAR)
61 
62 #define T_EXPORT_UNLESS(VAR) \
63  do { \
64  QVariant ___##VAR##_; \
65  ___##VAR##_.setValue(VAR); \
66  Tf::currentController()->exportVariant(QLatin1String(#VAR), (___##VAR##_), false); \
67  } while (0)
68 #define texportUnless(VAR) T_EXPORT_UNLESS(VAR)
69 
70 #define T_FETCH(TYPE, VAR) TYPE VAR = variant(QLatin1String(#VAR)).value<TYPE>()
71 #define tfetch(TYPE, VAR) T_FETCH(TYPE, VAR)
72 
73 #define T_FETCH_V(TYPE, VAR, DEFAULT) TYPE VAR = (hasVariant(QLatin1String(#VAR))) ? (variant(QLatin1String(#VAR)).value<TYPE>()) : (DEFAULT)
74 #define tfetchv(TYPE, VAR, DEFAULT) T_FETCH_V(TYPE, VAR, DEFAULT)
75 
76 #if QT_VERSION < 0x060000
77 #define T_EHEX(VAR) \
78  do { \
79  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
80  int ___##VAR##_type = (___##VAR##_).type(); \
81  switch (___##VAR##_type) { \
82  case QMetaType::QJsonValue: \
83  eh((___##VAR##_).toJsonValue()); \
84  break; \
85  case QMetaType::QJsonObject: \
86  eh((___##VAR##_).toJsonObject()); \
87  break; \
88  case QMetaType::QJsonArray: \
89  eh((___##VAR##_).toJsonArray()); \
90  break; \
91  case QMetaType::QJsonDocument: \
92  eh((___##VAR##_).toJsonDocument()); \
93  break; \
94  case QMetaType::QVariantMap: \
95  eh((___##VAR##_).toMap()); \
96  break; \
97  default: \
98  eh(___##VAR##_); \
99  } \
100  } while (0)
101 
102 #else
103 #define T_EHEX(VAR) \
104  do { \
105  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
106  int ___##VAR##_type = (___##VAR##_).typeId(); \
107  switch (___##VAR##_type) { \
108  case QMetaType::QJsonValue: \
109  eh((___##VAR##_).toJsonValue()); \
110  break; \
111  case QMetaType::QJsonObject: \
112  eh((___##VAR##_).toJsonObject()); \
113  break; \
114  case QMetaType::QJsonArray: \
115  eh((___##VAR##_).toJsonArray()); \
116  break; \
117  case QMetaType::QJsonDocument: \
118  eh((___##VAR##_).toJsonDocument()); \
119  break; \
120  case QMetaType::QVariantMap: \
121  eh((___##VAR##_).toMap()); \
122  break; \
123  default: \
124  eh(___##VAR##_); \
125  } \
126  } while (0)
127 
128 #endif
129 
130 #define tehex(VAR) T_EHEX(VAR)
131 
132 #define T_EHEX_V(VAR, DEFAULT) \
133  do { \
134  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
135  if ((___##VAR##_).isNull()) { \
136  eh(DEFAULT); \
137  } else { \
138  T_EHEX(VAR); \
139  } \
140  } while (0)
141 
142 #define tehexv(VAR, DEFAULT) T_EHEX_V(VAR, DEFAULT)
143 
144 // alias of tehexv
145 #define T_EHEX2(VAR, DEFAULT) T_EHEX_V(VAR, DEFAULT)
146 #define tehex2(VAR, DEFAULT) T_EHEX2(VAR, DEFAULT)
147 
148 #if QT_VERSION < 0x060000
149 #define T_ECHOEX(VAR) \
150  do { \
151  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
152  int ___##VAR##_type = (___##VAR##_).type(); \
153  switch (___##VAR##_type) { \
154  case QMetaType::QJsonValue: \
155  echo((___##VAR##_).toJsonValue()); \
156  break; \
157  case QMetaType::QJsonObject: \
158  echo((___##VAR##_).toJsonObject()); \
159  break; \
160  case QMetaType::QJsonArray: \
161  echo((___##VAR##_).toJsonArray()); \
162  break; \
163  case QMetaType::QJsonDocument: \
164  echo((___##VAR##_).toJsonDocument()); \
165  break; \
166  case QMetaType::QVariantMap: \
167  echo((___##VAR##_).toMap()); \
168  break; \
169  default: \
170  echo(___##VAR##_); \
171  } \
172  } while (0)
173 
174 #else
175 #define T_ECHOEX(VAR) \
176  do { \
177  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
178  int ___##VAR##_type = (___##VAR##_).typeId(); \
179  switch (___##VAR##_type) { \
180  case QMetaType::QJsonValue: \
181  echo((___##VAR##_).toJsonValue()); \
182  break; \
183  case QMetaType::QJsonObject: \
184  echo((___##VAR##_).toJsonObject()); \
185  break; \
186  case QMetaType::QJsonArray: \
187  echo((___##VAR##_).toJsonArray()); \
188  break; \
189  case QMetaType::QJsonDocument: \
190  echo((___##VAR##_).toJsonDocument()); \
191  break; \
192  case QMetaType::QVariantMap: \
193  echo((___##VAR##_).toMap()); \
194  break; \
195  default: \
196  echo(___##VAR##_); \
197  } \
198  } while (0)
199 
200 #endif
201 
202 
203 #define techoex(VAR) T_ECHOEX(VAR)
204 
205 #define T_ECHOEX_V(VAR, DEFAULT) \
206  do { \
207  auto ___##VAR##_ = variant(QLatin1String(#VAR)); \
208  if ((___##VAR##_).isNull()) { \
209  echo(DEFAULT); \
210  } else { \
211  T_ECHOEX(VAR); \
212  } \
213  } while (0)
214 
215 #define techoexv(VAR, DEFAULT) T_ECHOEX_V(VAR, DEFAULT)
216 
217 // alias of techoexv
218 #define T_ECHOEX2(VAR, DEFAULT) T_ECHOEX_V(VAR, DEFAULT)
219 #define techoex2(VAR, DEFAULT) T_ECHOEX2(VAR, DEFAULT)
220 
221 #define T_FLASH(VAR) \
222  do { \
223  QVariant ___##VAR##_; \
224  ___##VAR##_.setValue(VAR); \
225  Tf::currentController()->setFlash(QLatin1String(#VAR), (___##VAR##_)); \
226  } while (0)
227 
228 #define tflash(VAR) T_FLASH(VAR)
229 
230 #define T_VARIANT(VAR) (variant(QLatin1String(#VAR)).toString())
231 
232 
233 #define tFatal TDebug(Tf::FatalLevel).fatal
234 #define tError TDebug(Tf::ErrorLevel).error
235 #define tWarn TDebug(Tf::WarnLevel).warn
236 #define tInfo TDebug(Tf::InfoLevel).info
237 #define tDebug TDebug(Tf::DebugLevel).debug
238 #define tTrace TDebug(Tf::TraceLevel).trace
239 
240 namespace Tf {
241 #if QT_VERSION < 0x060000 // 6.0.0
242 constexpr auto ReadOnly = QIODevice::ReadOnly;
243 constexpr auto WriteOnly = QIODevice::WriteOnly;
244 #else
247 #endif
248 }
249 
250 #include "tfexception.h"
251 #include "tfnamespace.h"
252 #include "tdeclexport.h"
253 #include <TDebug>
254 #include <cstdint>
255 #include <cstring>
256 #include <functional>
257 #include <algorithm>
258 
259 class TWebApplication;
260 class TActionContext;
261 class TAbstractController;
262 class TAbstractActionContext;
263 class TAppSettings;
264 class TDatabaseContext;
265 class TCache;
266 class QSqlDatabase;
267 
268 namespace Tf {
269 T_CORE_EXPORT TWebApplication *app() noexcept;
270 T_CORE_EXPORT TAppSettings *appSettings() noexcept;
271 T_CORE_EXPORT const QVariantMap &conf(const QString &configName) noexcept;
272 T_CORE_EXPORT void msleep(int64_t msecs) noexcept;
274 
275 // Thread-safe std::random number generator
276 T_CORE_EXPORT uint32_t rand32_r() noexcept;
277 T_CORE_EXPORT uint64_t rand64_r() noexcept;
278 T_CORE_EXPORT uint64_t random(uint64_t min, uint64_t max) noexcept;
279 T_CORE_EXPORT uint64_t random(uint64_t max) noexcept;
280 
281 T_CORE_EXPORT TCache *cache() noexcept;
285 T_CORE_EXPORT QSqlDatabase &currentSqlDatabase(int id) noexcept;
286 T_CORE_EXPORT QMap<QByteArray, std::function<QObject *()>> *objectFactories() noexcept;
287 
288 // LZ4 lossless compression algorithm
289 T_CORE_EXPORT QByteArray lz4Compress(const char *data, int nbytes, int compressionLevel = 1) noexcept;
290 T_CORE_EXPORT QByteArray lz4Compress(const QByteArray &data, int compressionLevel = 1) noexcept;
291 T_CORE_EXPORT QByteArray lz4Uncompress(const char *data, int nbytes) noexcept;
292 T_CORE_EXPORT QByteArray lz4Uncompress(const QByteArray &data) noexcept;
293 
294 inline bool strcmp(const QByteArray &str1, const QByteArray &str2)
295 {
296  return str1.length() == str2.length() && !std::strncmp(str1.data(), str2.data(), str1.length());
297 }
298 
299 constexpr auto CR = "\x0d";
300 constexpr auto LF = "\x0a";
301 constexpr auto CRLF = "\x0d\x0a";
302 constexpr auto CRLFCRLF = "\x0d\x0a\x0d\x0a";
303 }
The TAbstractController class is the abstract base class of controllers, providing functionality comm...
Definition: tabstractcontroller.h:13
The TCache class stores items so that can be served faster.
Definition: tcache.h:7
The TDatabaseContext class is the base class of contexts for database access.
Definition: tdatabasecontext.h:11
The TWebApplication class provides an event loop for TreeFrog applications.
Definition: twebapplication.h:23
The Tf namespace contains miscellaneous identifiers used throughout the library of TreeFrog Framework...
Definition: tdebug.h:10
constexpr auto CRLF
Definition: tglobal.h:301
T_CORE_EXPORT QByteArray lz4Compress(const char *data, int nbytes, int compressionLevel=1) noexcept
Definition: tglobal.cpp:190
constexpr auto CRLFCRLF
Definition: tglobal.h:302
T_CORE_EXPORT const QVariantMap & conf(const QString &configName) noexcept
Returns the map associated with config file configName in 'conf' directory.
Definition: tglobal.cpp:57
constexpr auto WriteOnly
Definition: tglobal.h:246
T_CORE_EXPORT uint64_t random(uint64_t min, uint64_t max) noexcept
Random number generator in the range from min to max.
Definition: tglobal.cpp:98
T_CORE_EXPORT TWebApplication * app() noexcept
Returns a global pointer referring to the unique application object.
Definition: tglobal.cpp:40
bool strcmp(const QByteArray &str1, const QByteArray &str2)
Definition: tglobal.h:294
T_CORE_EXPORT TDatabaseContext * currentDatabaseContext()
Definition: tglobal.cpp:143
constexpr auto CR
Definition: tglobal.h:299
T_CORE_EXPORT QByteArray lz4Uncompress(const char *data, int nbytes) noexcept
Definition: tglobal.cpp:246
T_CORE_EXPORT QMap< QByteArray, std::function< QObject *()> > * objectFactories() noexcept
Definition: tglobal.cpp:183
T_CORE_EXPORT uint32_t rand32_r() noexcept
Definition: tglobal.cpp:78
T_CORE_EXPORT int64_t getMSecsSinceEpoch()
Definition: tglobal.cpp:295
T_CORE_EXPORT void msleep(int64_t msecs) noexcept
Causes the current thread to sleep for msecs milliseconds.
Definition: tglobal.cpp:65
constexpr auto LF
Definition: tglobal.h:300
T_CORE_EXPORT QSqlDatabase & currentSqlDatabase(int id) noexcept
Definition: tglobal.cpp:177
T_CORE_EXPORT TAbstractController * currentController()
Definition: tglobal.cpp:122
T_CORE_EXPORT TAppSettings * appSettings() noexcept
Returns a global pointer referring to the unique application settings object.
Definition: tglobal.cpp:48
constexpr auto ReadOnly
Definition: tglobal.h:245
T_CORE_EXPORT uint64_t rand64_r() noexcept
Definition: tglobal.cpp:87
T_CORE_EXPORT TCache * cache() noexcept
Definition: tglobal.cpp:116
const TAbstractController * constCurrentController()
Definition: tglobal.h:283
#define T_CORE_EXPORT
Definition: tdeclexport.h:28
constexpr auto TF_SRC_REVISION
Definition: tglobal.h:4
constexpr auto TF_VERSION_NUMBER
Definition: tglobal.h:3
constexpr auto TF_VERSION_STR
Definition: tglobal.h:2