Arduino-Redis
A Redis library for Arduino.
RedisInternal.h
1 #ifndef REDIS_INTERNAL_H
2 #define REDIS_INTERNAL_H
3 
4 #include "Arduino.h"
5 #include "Client.h"
6 #include <map>
7 #include <vector>
8 #include <memory>
9 #include <functional>
10 
11 #define CRLF F("\r\n")
12 #define ARDUINO_REDIS_SERIAL_TRACE 0
13 
14 typedef std::vector<String> ArgList;
15 
20 /* The lack of RTTI on Ardunio is unfortunate but completely understandable.
21  * However, we're not going to let that stop us, so RedisObject implements a basic
22  * but functional type system for Redis objects.
23  */
24 class RedisObject {
25 public:
27  typedef enum {
28  NoType = ' ',
29  SimpleString = '+',
30  Error = '-',
31  Integer = ':',
32  BulkString = '$',
33  Array = '*',
34  InternalError = '!'
35  } Type;
36 
37  RedisObject() {}
38  RedisObject(Type tc) : _type(tc) {}
39  RedisObject(Type tc, Client& c) : _type(tc) { init(c); }
40 
41  ~RedisObject() {}
42 
43  static std::shared_ptr<RedisObject> parseType(Client&);
44 
50  virtual void init(Client& client);
51 
53  virtual String RESP() = 0;
54 
57  virtual operator String() { return data; }
58 
59  Type type() const { return _type; }
60 
61 protected:
62  String data;
63  Type _type = Type::NoType;
64 };
65 
68 public:
69  RedisSimpleString(Client& c) : RedisObject(Type::SimpleString, c) {}
70 
71  virtual String RESP() override;
72 };
73 
75 class RedisBulkString : public RedisObject {
76 public:
77  RedisBulkString(Client& c) : RedisObject(Type::BulkString) { init(c); }
78  RedisBulkString(String& s) : RedisObject(Type::BulkString) { data = s; }
79 
80  virtual void init(Client& client) override;
81 
82  virtual String RESP() override;
83 };
84 
86 class RedisArray : public RedisObject {
87 public:
88  RedisArray() : RedisObject(Type::Array) {}
89  RedisArray(Client& c) : RedisObject(Type::Array, c) {}
90 
91  void add(std::shared_ptr<RedisObject> param) { vec.push_back(param); }
92 
93  virtual String RESP() override;
94 
95 protected:
96  std::vector<std::shared_ptr<RedisObject>> vec;
97 };
98 
101 public:
102  RedisInteger(Client& c) : RedisSimpleString(c) { _type = Type::Integer; }
103  operator int() { return data.toInt(); }
104  operator bool() { return (bool)operator int(); }
105 };
106 
109 public:
110  RedisError(Client& c) : RedisSimpleString(c) { _type = Type::Error; }
111 };
112 
115 {
116 public:
117  RedisInternalError(String err) : RedisObject(Type::InternalError) { data = err; }
118  RedisInternalError(const char* err) : RedisInternalError(String(err)) {}
119 
120  virtual String RESP() override { return "INTERNAL ERROR: " + data; }
121 };
122 
124 class RedisCommand : public RedisArray {
125 public:
126  RedisCommand(String command) : RedisArray() {
127  add(std::shared_ptr<RedisObject>(new RedisBulkString(command)));
128  }
129 
130  RedisCommand(String command, ArgList args)
131  : RedisCommand(command)
132  {
133  for (auto arg : args) {
134  add(std::shared_ptr<RedisObject>(new RedisBulkString(arg)));
135  }
136  }
137 
143  std::shared_ptr<RedisObject> issue(Client& cmdClient);
144 
145  template <typename T>
146  T issue_typed(Client& cmdClient);
147 
148 private:
149  String _err;
150 };
151 #endif // REDIS_INTERNAL_H
virtual String RESP() override
virtual String RESP()=0
virtual void init(Client &client)