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 
12 typedef std::vector<String> ArgList;
13 
18 /* The lack of RTTI on Ardunio is unfortunate but completely understandable.
19  * However, we're not going to let that stop us, so RedisObject implements a basic
20  * but functional type system for Redis objects.
21  */
22 class RedisObject {
23 public:
25  typedef enum {
26  NoType = '\0',
27  SimpleString = '+',
28  Error = '-',
29  Integer = ':',
30  BulkString = '$',
31  Array = '*',
32  InternalError = '!'
33  } Type;
34 
35  RedisObject() {}
36  RedisObject(Type tc) : _type(tc) {}
37  RedisObject(Type tc, Client& c) : _type(tc) { init(c); }
38 
39  virtual ~RedisObject() {}
40 
41  static std::shared_ptr<RedisObject> parseType(Client&);
42 
48  virtual void init(Client& client);
49 
51  virtual String RESP() = 0;
52 
55  virtual operator String() { return data; }
56 
57  Type type() const { return _type; }
58 
59 protected:
60  String data;
61  Type _type = Type::NoType;
62 };
63 
66 public:
67  RedisSimpleString(Client& c) : RedisObject(Type::SimpleString, c) {}
68  ~RedisSimpleString() override {}
69 
70  virtual String RESP() override;
71 };
72 
74 class RedisBulkString : public RedisObject {
75 public:
76  RedisBulkString(Client& c) : RedisObject(Type::BulkString, c) { init(c); }
77  RedisBulkString(String& s) : RedisObject(Type::BulkString) { data = s; }
78  ~RedisBulkString() override {}
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) { init(c); }
90  ~RedisArray() override { vec.empty(); }
91 
92  void add(std::shared_ptr<RedisObject> param) { vec.push_back(param); }
93 
94  operator std::vector<String>() const;
95 
96  virtual void init(Client& client) override;
97 
98  virtual String RESP() override;
99 
100 protected:
101  std::vector<std::shared_ptr<RedisObject>> vec;
102 };
103 
106 public:
107  RedisInteger(Client& c) : RedisSimpleString(c) { _type = Type::Integer; }
108  ~RedisInteger() override {}
109 
110  operator int() { return data.toInt(); }
111  operator bool() { return (bool)operator int(); }
112 };
113 
116 public:
117  RedisError(Client& c) : RedisSimpleString(c) { _type = Type::Error; }
118  ~RedisError() override {}
119 };
120 
123 {
124 public:
125  RedisInternalError(String err) : RedisObject(Type::InternalError) { data = err; }
126  RedisInternalError(const char* err) : RedisInternalError(String(err)) {}
127  ~RedisInternalError() override {}
128 
129  virtual String RESP() override { return "INTERNAL ERROR: " + data; }
130 };
131 
133 class RedisCommand : public RedisArray {
134 public:
135  RedisCommand(String command) : RedisArray() {
136  add(std::shared_ptr<RedisObject>(new RedisBulkString(command)));
137  }
138 
139  RedisCommand(String command, ArgList args)
140  : RedisCommand(command)
141  {
142  for (auto arg : args) {
143  add(std::shared_ptr<RedisObject>(new RedisBulkString(arg)));
144  }
145  }
146 
147  ~RedisCommand() override {}
148 
154  std::shared_ptr<RedisObject> issue(Client& cmdClient);
155 
156  template <typename T>
157  T issue_typed(Client& cmdClient);
158 
159 private:
160  String _err;
161 };
162 #endif // REDIS_INTERNAL_H
RedisObject::init
virtual void init(Client &client)
Definition: RedisInternal.cpp:5
RedisCommand::issue
std::shared_ptr< RedisObject > issue(Client &cmdClient)
Definition: RedisInternal.cpp:80
RedisBulkString::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:22
RedisError
Definition: RedisInternal.h:115
RedisSimpleString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:11
RedisArray::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:69
RedisCommand
Definition: RedisInternal.h:133
RedisSimpleString
Definition: RedisInternal.h:65
RedisObject
Definition: RedisInternal.h:22
RedisObject::Type
Type
Definition: RedisInternal.h:25
RedisInteger
Definition: RedisInternal.h:105
RedisArray::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:55
RedisBulkString
Definition: RedisInternal.h:74
RedisArray
Definition: RedisInternal.h:86
RedisInternalError
Definition: RedisInternal.h:123
RedisBulkString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:45
RedisObject::RESP
virtual String RESP()=0
RedisInternalError::RESP
virtual String RESP() override
Definition: RedisInternal.h:129