qredisclient
response.h
1 #pragma once
2 #include <QByteArray>
3 #include <QVariant>
4 #include <QVector>
5 #include <QString>
6 #include <QSharedPointer>
7 #include <hiredis/read.h>
8 
9 #include "exception.h"
10 
11 namespace RedisClient {
12 class Response
13 {
14  ADD_EXCEPTION
15 
16 public:
17  enum Type { Status, Error, Integer, Bulk, MultiBulk, Unknown };
18 
19 public:
20  Response();
21  Response(const QByteArray &);
22  virtual ~Response(void);
23 
24  QVariant getValue();
25  Type getType() const;
26  QByteArray source() const;
27  QString toRawString() const;
28 
29  bool isEmpty() const;
30  bool isErrorMessage() const;
31  bool isErrorStateMessage() const;
32  bool isDisabledCommandErrorMessage() const;
33  bool isOkMessage() const;
34  bool isValid();
35  bool isMessage() const;
36  bool isArray() const;
37  bool hasUnusedBuffer() const;
38 
39  // Pub/Sub support
40  QByteArray getChannel() const;
41 
42  // Cluster support
43  bool isAskRedirect() const;
44  bool isMovedRedirect() const;
45  QByteArray getRedirectionHost() const;
46  uint getRedirectionPort() const;
47 
48 public:
49  void setSource(const QByteArray&);
50  void appendToSource(const QByteArray&);
51  QByteArray getUnusedBuffer();
52  void reset();
53 
54  static QString valueToHumanReadString(const QVariant&, int indentLevel=0);
55 
56 protected:
57  Type getResponseType(const QByteArray&) const;
58  Type getResponseType(const char) const;
59 
60  bool parse();
61  void feed(const QByteArray &buffer);
62 
63 protected:
64  QByteArray m_responseSource;
65  QSharedPointer<redisReader> m_redisReader;
66  QSharedPointer<QVariant> m_result;
67 
68 private:
69  /*
70  * hiredis custom functions
71  */
72  static void *createStringObject(const redisReadTask *task, char *str, size_t len);
73  static void *createArrayObject(const redisReadTask *t, int elements);
74  static void *createIntegerObject(const redisReadTask *task, long long value);
75  static void *createNilObject(const redisReadTask *task);
76  static void freeObject(void *obj);
77 
78  static const redisReplyObjectFunctions defaultFunctions;
79 
80  static redisReader *redisReaderCreate(void)
81  {
82  return redisReaderCreateWithFunctions(const_cast<redisReplyObjectFunctions*>(&defaultFunctions));
83  }
84 };
85 }
86 
87 Q_DECLARE_METATYPE(QVector<QVariant*>)
Definition: command.h:8
Definition: response.h:12