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 <vector>
7 #include <memory>
8 #include <functional>
9 
10 #define CRLF F("\r\n")
11 #define ARDUINO_REDIS_SERIAL_TRACE 0
12 
13 #define sprint(fmt, ...) do { if (ARDUINO_REDIS_SERIAL_TRACE) Serial.printf("[TRACE] " fmt, ##__VA_ARGS__); } while (0)
14 
15 typedef std::vector<String> ArgList;
16 
21 /* The lack of RTTI on Ardunio is unfortunate but completely understandable.
22  * However, we're not going to let that stop us, so RedisObject implements a basic
23  * but functional type system for Redis objects.
24  */
25 class RedisObject {
26 public:
28  typedef enum {
29  NoType = '\0',
30  SimpleString = '+',
31  Error = '-',
32  Integer = ':',
33  BulkString = '$',
34  Array = '*',
35  InternalError = '!'
36  } Type;
37 
38  RedisObject() {}
39  RedisObject(Type tc) : _type(tc) {}
40  RedisObject(Type tc, Client& c) : _type(tc) { init(c); }
41 
42  virtual ~RedisObject() {}
43 
44  static std::shared_ptr<RedisObject> parseType(Client&);
45 
51  virtual void init(Client& client);
52 
54  virtual String RESP() = 0;
55 
58  virtual operator String() { return data; }
59 
60  Type type() const { return _type; }
61 
62 protected:
63  String data;
64  Type _type = Type::NoType;
65 };
66 
69 public:
70  RedisSimpleString(Client& c) : RedisObject(Type::SimpleString, c) {}
71  ~RedisSimpleString() override {}
72 
73  virtual String RESP() override;
74 };
75 
77 class RedisBulkString : public RedisObject {
78 public:
79  RedisBulkString(Client& c) : RedisObject(Type::BulkString, c) { init(c); }
80  RedisBulkString(String& s) : RedisObject(Type::BulkString) { data = s; }
81  ~RedisBulkString() override {}
82 
83  virtual void init(Client& client) override;
84 
85  virtual String RESP() override;
86 };
87 
89 class RedisArray : public RedisObject {
90 public:
91  RedisArray() : RedisObject(Type::Array) {}
92  RedisArray(Client& c) : RedisObject(Type::Array, c) { init(c); }
93  ~RedisArray() override { vec.empty(); }
94 
95  void add(std::shared_ptr<RedisObject> param) { vec.push_back(param); }
96 
97  operator std::vector<String>() const;
98 
99  virtual void init(Client& client) override;
100 
101  virtual String RESP() override;
102 
103 protected:
104  std::vector<std::shared_ptr<RedisObject>> vec;
105 };
106 
109 public:
110  RedisInteger(Client& c) : RedisSimpleString(c) { _type = Type::Integer; }
111  ~RedisInteger() override {}
112 
113  operator int() { return data.toInt(); }
114  operator bool() { return (bool)operator int(); }
115 };
116 
119 public:
120  RedisError(Client& c) : RedisSimpleString(c) { _type = Type::Error; }
121  ~RedisError() override {}
122 };
123 
126 {
127 public:
128  RedisInternalError(String err) : RedisObject(Type::InternalError) { data = err; }
129  RedisInternalError(const char* err) : RedisInternalError(String(err)) {}
130  ~RedisInternalError() override {}
131 
132  virtual String RESP() override { return "INTERNAL ERROR: " + data; }
133 };
134 
136 class RedisCommand : public RedisArray {
137 public:
138  RedisCommand(String command) : RedisArray() {
139  add(std::shared_ptr<RedisObject>(new RedisBulkString(command)));
140  }
141 
142  RedisCommand(String command, ArgList args)
143  : RedisCommand(command)
144  {
145  for (auto arg : args) {
146  add(std::shared_ptr<RedisObject>(new RedisBulkString(arg)));
147  }
148  }
149 
150  ~RedisCommand() override {}
151 
157  std::shared_ptr<RedisObject> issue(Client& cmdClient);
158 
159  template <typename T>
160  T issue_typed(Client& cmdClient);
161 
162 private:
163  String _err;
164 };
165 #endif // REDIS_INTERNAL_H
virtual String RESP() override
virtual String RESP()=0
virtual void init(Client &client)