MythTV master
service.h
Go to the documentation of this file.
1
2// 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
25//
26// Notes for derived classes -
27//
28// * This implementation can't handle declared default parameters
29//
30// * When called, any missing params are sent default values for its datatype
31//
32// * Q_CLASSINFO( "<methodName>_Method", "BOTH" )
33// is used to determine HTTP method type.
34// Defaults to "BOTH", available values:
35// "GET", "POST" or "BOTH"
36//
39
40class Service : public QObject
41{
42 Q_OBJECT
43
44 public:
45
46 explicit inline Service( QObject *parent = nullptr );
47
48 public:
49
51 // This method should be overridden to handle non-QObject based custom types
53
54 virtual QVariant ConvertToVariant ( int nType, void *pValue );
55
56 virtual void* ConvertToParameterPtr( int nTypeId,
57 const QString &sParamType,
58 void* pParam,
59 const QString &sValue );
60
61 static bool ToBool( const QString &sVal );
62
63 public:
64
65 bool HAS_PARAM(const QString& p) const { return m_parsedParams.contains(p); }
66 QList<QString> m_parsedParams; // lowercased
67};
68
70//
72
73inline Service::Service(QObject *parent) : QObject(parent)
74{
75 qRegisterMetaType< QFileInfo >();
76}
77
79//
81
82// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
83#define SCRIPT_CATCH_EXCEPTION( default, code ) \
84 try \
85 { \
86 code \
87 } \
88 catch( QString &msg ) \
89 { \
90 m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
91 return default; \
92 } \
93 catch( const char *msg ) \
94 { \
95 m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
96 return default; \
97 } \
98 catch( ... ) \
99 { \
100 m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, "Unknown Exception" ); \
101 return default; \
102 }
103
105// The following replaces the standard Q_SCRIPT_DECLARE_QMETAOBJECT macro
106// This implementation passes the QScriptEngine to the constructor as the
107// first parameter.
109
110// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
111#define Q_SCRIPT_DECLARE_QMETAOBJECT_MYTHTV(T, _Arg1) \
112template<> inline QScriptValue qscriptQMetaObjectConstructor<T>(QScriptContext *ctx, QScriptEngine *eng, T *) /* NOLINT(bugprone-macro-parentheses) */ \
113{ \
114 _Arg1 arg1 = qscriptvalue_cast<_Arg1> (ctx->argument(0)); \
115 T* t = new T(eng, arg1); /* NOLINT(bugprone-macro-parentheses) */ \
116 if (ctx->isCalledAsConstructor()) \
117 return eng->newQObject(ctx->thisObject(), t, QScriptEngine::AutoOwnership); \
118 QScriptValue o = eng->newQObject(t, QScriptEngine::AutoOwnership); \
119 o.setPrototype(ctx->callee().property(QString::fromLatin1("prototype"))); \
120 return o; \
121}
122
123
124#endif
QList< QString > m_parsedParams
Definition: service.h:66
bool HAS_PARAM(const QString &p) const
Definition: service.h:65
virtual void * ConvertToParameterPtr(int nTypeId, const QString &sParamType, void *pParam, const QString &sValue)
Definition: service.cpp:49
static bool ToBool(const QString &sVal)
Definition: service.cpp:169
Service(QObject *parent=nullptr)
Definition: service.h:73
virtual QVariant ConvertToVariant(int nType, void *pValue)
Definition: service.cpp:22