MythTV  master
service.h
Go to the documentation of this file.
1 // Program Name: service.h
3 // Created : Jan. 19, 2010
4 //
5 // Purpose : Base class for all Web Services
6 //
7 // Copyright (c) 2010 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef SERVICE_H_
14 #define SERVICE_H_
15 
16 #include <QObject>
17 #include <QMetaType>
18 #include <QVariant>
19 #include <QFileInfo>
20 #include <QDateTime>
21 #include <QString>
22 
23 #include "serviceexp.h"
24 
27 //
28 // Notes for derived classes -
29 //
30 // * This implementation can't handle declared default parameters
31 //
32 // * When called, any missing params are sent default values for its datatype
33 //
34 // * Q_CLASSINFO( "<methodName>_Method", "BOTH" )
35 // is used to determine HTTP method type.
36 // Defaults to "BOTH", available values:
37 // "GET", "POST" or "BOTH"
38 //
41 
42 class SERVICE_PUBLIC Service : public QObject
43 {
44  Q_OBJECT
45 
46  public:
47 
48  explicit inline Service( QObject *parent = nullptr );
49 
50  public:
51 
53  // This method should be overridden to handle non-QObject based custom types
55 
56  virtual QVariant ConvertToVariant ( int nType, void *pValue );
57 
58  virtual void* ConvertToParameterPtr( int nTypeId,
59  const QString &sParamType,
60  void* pParam,
61  const QString &sValue );
62 
63  static bool ToBool( const QString &sVal );
64 
65  public:
66 
67  bool HAS_PARAM(const QString& p) const { return m_parsedParams.contains(p); }
68  QList<QString> m_parsedParams; // lowercased
69 };
70 
72 //
74 
75 inline Service::Service(QObject *parent) : QObject(parent)
76 {
77  qRegisterMetaType< QFileInfo >();
78 }
79 
81 //
83 
84 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
85 #define SCRIPT_CATCH_EXCEPTION( default, code ) \
86  try \
87  { \
88  code \
89  } \
90  catch( QString &msg ) \
91  { \
92  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
93  return default; \
94  } \
95  catch( const char *msg ) \
96  { \
97  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
98  return default; \
99  } \
100  catch( ... ) \
101  { \
102  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, "Unknown Exception" ); \
103  return default; \
104  }
105 
107 // The following replaces the standard Q_SCRIPT_DECLARE_QMETAOBJECT macro
108 // This implementation passes the QScriptEngine to the constructor as the
109 // first parameter.
111 
112 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
113 #define Q_SCRIPT_DECLARE_QMETAOBJECT_MYTHTV(T, _Arg1) \
114 template<> inline QScriptValue qscriptQMetaObjectConstructor<T>(QScriptContext *ctx, QScriptEngine *eng, T *) /* NOLINT(bugprone-macro-parentheses) */ \
115 { \
116  _Arg1 arg1 = qscriptvalue_cast<_Arg1> (ctx->argument(0)); \
117  T* t = new T(eng, arg1); /* NOLINT(bugprone-macro-parentheses) */ \
118  if (ctx->isCalledAsConstructor()) \
119  return eng->newQObject(ctx->thisObject(), t, QScriptEngine::AutoOwnership); \
120  QScriptValue o = eng->newQObject(t, QScriptEngine::AutoOwnership); \
121  o.setPrototype(ctx->callee().property(QString::fromLatin1("prototype"))); \
122  return o; \
123 }
124 
125 
126 #endif
Service::Service
Service(QObject *parent=nullptr)
Definition: service.h:75
Service::HAS_PARAM
bool HAS_PARAM(const QString &p) const
Definition: service.h:67
SERVICE_PUBLIC
#define SERVICE_PUBLIC
Definition: serviceexp.h:9
Service
Definition: service.h:42
hardwareprofile.config.p
p
Definition: config.py:33
serviceexp.h
Service::m_parsedParams
QList< QString > m_parsedParams
Definition: service.h:68