Arduino-Redis
A Redis library for Arduino.
RedisInternal.h
Go to the documentation of this file.
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  */
23 {
24 public:
26  typedef enum
27  {
28  NoType = '\0',
29  SimpleString = '+',
30  Error = '-',
31  Integer = ':',
32  BulkString = '$',
33  Array = '*',
35  } Type;
36 
38  RedisObject(Type tc) : _type(tc) {}
39  RedisObject(Type tc, Client &c) : _type(tc) { init(c); }
40 
41  virtual ~RedisObject() {}
42 
43  static std::shared_ptr<RedisObject> parseTypeNonBlocking(Client &);
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() { if(data) return data; return String("(nil)");}
59 
60  Type type() const { return _type; }
61 
62 protected:
63  String data;
64  Type _type = Type::NoType;
65 };
66 
69 {
70 public:
72  ~RedisSimpleString() override {}
73 
74  virtual String RESP() override;
75 };
76 
79 {
80 public:
81  RedisBulkString(Client &c) : RedisObject(Type::BulkString, c) { init(c); }
83  ~RedisBulkString() override {}
84 
85  virtual void init(Client &client) override;
86 
87  virtual String RESP() override;
88 };
89 
91 class RedisArray : public RedisObject
92 {
93 public:
95  RedisArray(Client &c) : RedisObject(Type::Array, c) { init(c); }
96  ~RedisArray() override { vec.empty(); }
97 
98  void add(std::shared_ptr<RedisObject> param) { vec.push_back(param); }
99 
101  operator std::vector<String>() const;
102 
103  operator std::vector<std::shared_ptr<RedisObject>>() const;
104 
105  virtual void init(Client &client) override;
106 
107  virtual String RESP() override;
108 
109 protected:
110  std::vector<std::shared_ptr<RedisObject>> vec;
111 };
112 
115 {
116 public:
117  RedisInteger(Client &c) : RedisSimpleString(c) { _type = Type::Integer; }
118  ~RedisInteger() override {}
119 
120  operator int() { return data.toInt(); }
121  operator bool() { return (bool)operator int(); }
122 };
123 
126 {
127 public:
128  RedisError(Client &c) : RedisSimpleString(c) { _type = Type::Error; }
129  ~RedisError() override {}
130 };
131 
134 {
135 public:
136  typedef enum
137  {
138  UnknownError = -254,
143 
146  ~RedisInternalError() override {}
147 
149  void setErrorString(String es) { data = es; }
150 
151  virtual String RESP() override { return "-INTERNAL ERROR " + String(_code) + (data ? ": " + data : ""); }
152 
153 protected:
155 };
156 
158 class RedisCommand : public RedisArray
159 {
160 public:
161  RedisCommand(String command) : RedisArray()
162  {
163  add(std::shared_ptr<RedisObject>(new RedisBulkString(command)));
164  }
165 
166  RedisCommand(String command, ArgList args)
167  : RedisCommand(command)
168  {
169  for (auto arg : args)
170  {
171  add(std::shared_ptr<RedisObject>(new RedisBulkString(arg)));
172  }
173  }
174 
175  ~RedisCommand() override {}
176 
182  std::shared_ptr<RedisObject> issue(Client &cmdClient);
183 
184  template <typename T>
185  T issue_typed(Client &cmdClient);
186 
187 private:
188  String _err;
189 };
190 
191 #endif // REDIS_INTERNAL_H
RedisObject::init
virtual void init(Client &client)
Definition: RedisInternal.cpp:6
RedisCommand::RedisCommand
RedisCommand(String command, ArgList args)
Definition: RedisInternal.h:166
RedisCommand::issue
std::shared_ptr< RedisObject > issue(Client &cmdClient)
Definition: RedisInternal.cpp:101
RedisBulkString::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:23
RedisError
Definition: RedisInternal.h:125
RedisObject::BulkString
@ BulkString
Definition: RedisInternal.h:32
RedisArray::add
void add(std::shared_ptr< RedisObject > param)
Definition: RedisInternal.h:98
RedisSimpleString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:12
RedisObject::RedisObject
RedisObject(Type tc)
Definition: RedisInternal.h:38
RedisArray::~RedisArray
~RedisArray() override
Definition: RedisInternal.h:96
RedisArray::vec
std::vector< std::shared_ptr< RedisObject > > vec
Definition: RedisInternal.h:110
RedisArray::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:89
RedisCommand::~RedisCommand
~RedisCommand() override
Definition: RedisInternal.h:175
RedisCommand
Definition: RedisInternal.h:158
RedisBulkString::~RedisBulkString
~RedisBulkString() override
Definition: RedisInternal.h:83
RedisObject::_type
Type _type
Definition: RedisInternal.h:64
RedisSimpleString
Definition: RedisInternal.h:68
RedisInternalError::RedisInternalErrorCode
RedisInternalErrorCode
Definition: RedisInternal.h:136
RedisObject::RedisObject
RedisObject()
Definition: RedisInternal.h:37
RedisObject
Definition: RedisInternal.h:22
RedisObject::SimpleString
@ SimpleString
Definition: RedisInternal.h:29
RedisInternalError::_code
RedisInternalErrorCode _code
Definition: RedisInternal.h:154
RedisObject::Type
Type
Definition: RedisInternal.h:26
RedisObject::parseTypeNonBlocking
static std::shared_ptr< RedisObject > parseTypeNonBlocking(Client &)
Definition: RedisInternal.cpp:154
RedisInternalError::NoError
@ NoError
Definition: RedisInternal.h:141
RedisInternalError::RedisInternalError
RedisInternalError(RedisInternalErrorCode c, String es)
Definition: RedisInternal.h:145
RedisInteger::RedisInteger
RedisInteger(Client &c)
Definition: RedisInternal.h:117
RedisError::RedisError
RedisError(Client &c)
Definition: RedisInternal.h:128
RedisInternalError::Disconnected
@ Disconnected
Definition: RedisInternal.h:140
RedisInteger
Definition: RedisInternal.h:114
RedisArray::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:58
RedisObject::Array
@ Array
Definition: RedisInternal.h:33
RedisCommand::RedisCommand
RedisCommand(String command)
Definition: RedisInternal.h:161
RedisBulkString
Definition: RedisInternal.h:78
RedisObject::Error
@ Error
Definition: RedisInternal.h:30
RedisObject::type
Type type() const
Definition: RedisInternal.h:60
RedisInternalError::~RedisInternalError
~RedisInternalError() override
Definition: RedisInternal.h:146
RedisObject::Integer
@ Integer
Definition: RedisInternal.h:31
RedisObject::NoType
@ NoType
Definition: RedisInternal.h:28
RedisArray
Definition: RedisInternal.h:91
RedisInternalError
Definition: RedisInternal.h:133
RedisError::~RedisError
~RedisError() override
Definition: RedisInternal.h:129
RedisArray::RedisArray
RedisArray()
Definition: RedisInternal.h:94
RedisBulkString::RedisBulkString
RedisBulkString(Client &c)
Definition: RedisInternal.h:81
RedisObject::parseType
static std::shared_ptr< RedisObject > parseType(Client &)
Definition: RedisInternal.cpp:186
RedisArray::RedisArray
RedisArray(Client &c)
Definition: RedisInternal.h:95
RedisObject::InternalError
@ InternalError
Definition: RedisInternal.h:34
RedisCommand::issue_typed
T issue_typed(Client &cmdClient)
RedisInternalError::code
RedisInternalErrorCode code()
Definition: RedisInternal.h:148
ArgList
std::vector< String > ArgList
Definition: RedisInternal.h:12
RedisInternalError::setErrorString
void setErrorString(String es)
Definition: RedisInternal.h:149
RedisInternalError::RedisInternalError
RedisInternalError(RedisInternalErrorCode c)
Definition: RedisInternal.h:144
RedisInteger::~RedisInteger
~RedisInteger() override
Definition: RedisInternal.h:118
RedisInternalError::UnknownError
@ UnknownError
Definition: RedisInternal.h:138
RedisObject::~RedisObject
virtual ~RedisObject()
Definition: RedisInternal.h:41
RedisBulkString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:48
RedisSimpleString::~RedisSimpleString
~RedisSimpleString() override
Definition: RedisInternal.h:72
RedisObject::RESP
virtual String RESP()=0
RedisInternalError::RESP
virtual String RESP() override
Definition: RedisInternal.h:151
RedisSimpleString::RedisSimpleString
RedisSimpleString(Client &c)
Definition: RedisInternal.h:71
RedisObject::RedisObject
RedisObject(Type tc, Client &c)
Definition: RedisInternal.h:39
RedisObject::data
String data
Definition: RedisInternal.h:63
RedisBulkString::RedisBulkString
RedisBulkString(String &s)
Definition: RedisInternal.h:82
RedisInternalError::UnknownType
@ UnknownType
Definition: RedisInternal.h:139