TreeFrogFramework  1.18
tcommandlineinterface.h
Go to the documentation of this file.
1 #ifndef TCOMMANDLINEINTERFACE_H
2 #define TCOMMANDLINEINTERFACE_H
3 
4 #include <QtCore>
5 #include <TGlobal>
6 #include <TWebApplication>
7 #include <TActionThread>
8 #include <TSystemGlobal>
9 #include <TSqlDatabasePool>
10 #include <TKvsDatabasePool>
11 
12 
13 #define TF_CLI_MAIN(STATICFUNCTION) \
14  int main(int argc, char *argv[]) \
15  { \
16  class Thread : public TActionThread { \
17  public: \
18  Thread() : TActionThread(0), returnCode(0) { } \
19  volatile int returnCode; \
20  protected: \
21  virtual void run() \
22  { \
23  returnCode = STATICFUNCTION(); \
24  commitTransactions(); \
25  for (QMap<int, QSqlDatabase>::iterator it = sqlDatabases.begin(); it != sqlDatabases.end(); ++it) { \
26  it.value().close(); /* close SQL database */ \
27  } \
28  for (QMap<int, TKvsDatabase>::iterator it = kvsDatabases.begin(); it != kvsDatabases.end(); ++it) { \
29  it.value().close(); /* close KVS database */ \
30  } \
31  QEventLoop eventLoop; \
32  while (eventLoop.processEvents()) {} \
33  } \
34  }; \
35  TWebApplication app(argc, argv); \
36  QByteArray codecName = app.appSettings().value("InternalEncoding", "UTF-8").toByteArray(); \
37  QTextCodec *codec = QTextCodec::codecForName(codecName); \
38  QTextCodec::setCodecForLocale(codec); \
39  TF_SET_CODEC_FOR_TR(codec); \
40  tSetupSystemLogger(); \
41  tSetupQueryLogger(); \
42  tSetupAppLoggers(); \
43  int idx = QCoreApplication::arguments().indexOf("-e"); \
44  QString env = (idx > 0) ? QCoreApplication::arguments().value(idx + 1) : QString("product"); \
45  app.setDatabaseEnvironment(env); \
46  TSqlDatabasePool::instantiate(1); \
47  TKvsDatabasePool::instantiate(1); \
48  Thread thread; \
49  QObject::connect(&thread, SIGNAL(finished()), &app, SLOT(quit())); \
50  thread.start(); \
51  app.exec(); \
52  tReleaseAppLoggers(); \
53  tReleaseQueryLogger(); \
54  tReleaseSystemLogger(); \
55  return thread.returnCode; \
56  }
57 
58 #endif // TCOMMANDLINEINTERFACE_H