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> 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 {
69 public:
71  ~RedisSimpleString() override {}
72 
73  virtual String RESP() override;
74 };
75 
78 {
79 public:
80  RedisBulkString(Client &c) : RedisObject(Type::BulkString, c) { init(c); }
82  ~RedisBulkString() override {}
83 
84  virtual void init(Client &client) override;
85 
86  virtual String RESP() override;
87 };
88 
90 class RedisArray : public RedisObject
91 {
92 public:
94  RedisArray(Client &c) : RedisObject(Type::Array, c) { init(c); }
95  ~RedisArray() override { vec.empty(); }
96 
97  void add(std::shared_ptr<RedisObject> param) { vec.push_back(param); }
98 
99  operator std::vector<String>() const;
100 
101  virtual void init(Client &client) override;
102 
103  virtual String RESP() override;
104 
105 protected:
106  std::vector<std::shared_ptr<RedisObject>> vec;
107 };
108 
111 {
112 public:
113  RedisInteger(Client &c) : RedisSimpleString(c) { _type = Type::Integer; }
114  ~RedisInteger() override {}
115 
116  operator int() { return data.toInt(); }
117  operator bool() { return (bool)operator int(); }
118 };
119 
122 {
123 public:
124  RedisError(Client &c) : RedisSimpleString(c) { _type = Type::Error; }
125  ~RedisError() override {}
126 };
127 
130 {
131 public:
132  typedef enum
133  {
134  UnknownError = -254,
139 
142  ~RedisInternalError() override {}
143 
145  void setErrorString(String es) { data = es; }
146 
147  virtual String RESP() override { return "INTERNAL ERROR " + String(_code) + (data ? ": " + data : ""); }
148 
149 protected:
151 };
152 
154 class RedisCommand : public RedisArray
155 {
156 public:
157  RedisCommand(String command) : RedisArray()
158  {
159  add(std::shared_ptr<RedisObject>(new RedisBulkString(command)));
160  }
161 
162  RedisCommand(String command, ArgList args)
163  : RedisCommand(command)
164  {
165  for (auto arg : args)
166  {
167  add(std::shared_ptr<RedisObject>(new RedisBulkString(arg)));
168  }
169  }
170 
171  ~RedisCommand() override {}
172 
178  std::shared_ptr<RedisObject> issue(Client &cmdClient);
179 
180  template <typename T>
181  T issue_typed(Client &cmdClient);
182 
183 private:
184  String _err;
185 };
186 
187 #endif // REDIS_INTERNAL_H
RedisObject::init
virtual void init(Client &client)
Definition: RedisInternal.cpp:5
RedisCommand::RedisCommand
RedisCommand(String command, ArgList args)
Definition: RedisInternal.h:162
RedisCommand::issue
std::shared_ptr< RedisObject > issue(Client &cmdClient)
Definition: RedisInternal.cpp:83
RedisBulkString::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:22
RedisError
Definition: RedisInternal.h:121
RedisObject::BulkString
@ BulkString
Definition: RedisInternal.h:32
RedisArray::add
void add(std::shared_ptr< RedisObject > param)
Definition: RedisInternal.h:97
RedisSimpleString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:11
RedisObject::RedisObject
RedisObject(Type tc)
Definition: RedisInternal.h:38
RedisArray::~RedisArray
~RedisArray() override
Definition: RedisInternal.h:95
RedisArray::vec
std::vector< std::shared_ptr< RedisObject > > vec
Definition: RedisInternal.h:106
RedisArray::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:71
RedisCommand::~RedisCommand
~RedisCommand() override
Definition: RedisInternal.h:171
RedisCommand
Definition: RedisInternal.h:154
RedisBulkString::~RedisBulkString
~RedisBulkString() override
Definition: RedisInternal.h:82
RedisObject::_type
Type _type
Definition: RedisInternal.h:63
RedisSimpleString
Definition: RedisInternal.h:67
RedisInternalError::RedisInternalErrorCode
RedisInternalErrorCode
Definition: RedisInternal.h:132
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:150
RedisObject::Type
Type
Definition: RedisInternal.h:26
RedisInternalError::NoError
@ NoError
Definition: RedisInternal.h:137
RedisInternalError::RedisInternalError
RedisInternalError(RedisInternalErrorCode c, String es)
Definition: RedisInternal.h:141
RedisInteger::RedisInteger
RedisInteger(Client &c)
Definition: RedisInternal.h:113
RedisError::RedisError
RedisError(Client &c)
Definition: RedisInternal.h:124
RedisInternalError::Disconnected
@ Disconnected
Definition: RedisInternal.h:136
RedisInteger
Definition: RedisInternal.h:110
RedisArray::init
virtual void init(Client &client) override
Definition: RedisInternal.cpp:57
RedisObject::Array
@ Array
Definition: RedisInternal.h:33
RedisCommand::RedisCommand
RedisCommand(String command)
Definition: RedisInternal.h:157
RedisBulkString
Definition: RedisInternal.h:77
RedisObject::Error
@ Error
Definition: RedisInternal.h:30
RedisObject::type
Type type() const
Definition: RedisInternal.h:59
RedisInternalError::~RedisInternalError
~RedisInternalError() override
Definition: RedisInternal.h:142
RedisObject::Integer
@ Integer
Definition: RedisInternal.h:31
RedisObject::NoType
@ NoType
Definition: RedisInternal.h:28
RedisArray
Definition: RedisInternal.h:90
RedisInternalError
Definition: RedisInternal.h:129
RedisError::~RedisError
~RedisError() override
Definition: RedisInternal.h:125
RedisArray::RedisArray
RedisArray()
Definition: RedisInternal.h:93
RedisBulkString::RedisBulkString
RedisBulkString(Client &c)
Definition: RedisInternal.h:80
RedisObject::parseType
static std::shared_ptr< RedisObject > parseType(Client &)
Definition: RedisInternal.cpp:131
RedisArray::RedisArray
RedisArray(Client &c)
Definition: RedisInternal.h:94
RedisObject::InternalError
@ InternalError
Definition: RedisInternal.h:34
RedisCommand::issue_typed
T issue_typed(Client &cmdClient)
RedisInternalError::code
RedisInternalErrorCode code()
Definition: RedisInternal.h:144
ArgList
std::vector< String > ArgList
Definition: RedisInternal.h:12
RedisInternalError::setErrorString
void setErrorString(String es)
Definition: RedisInternal.h:145
RedisInternalError::RedisInternalError
RedisInternalError(RedisInternalErrorCode c)
Definition: RedisInternal.h:140
RedisInteger::~RedisInteger
~RedisInteger() override
Definition: RedisInternal.h:114
RedisInternalError::UnknownError
@ UnknownError
Definition: RedisInternal.h:134
RedisObject::~RedisObject
virtual ~RedisObject()
Definition: RedisInternal.h:41
RedisBulkString::RESP
virtual String RESP() override
Definition: RedisInternal.cpp:47
RedisSimpleString::~RedisSimpleString
~RedisSimpleString() override
Definition: RedisInternal.h:71
RedisObject::RESP
virtual String RESP()=0
RedisInternalError::RESP
virtual String RESP() override
Definition: RedisInternal.h:147
RedisSimpleString::RedisSimpleString
RedisSimpleString(Client &c)
Definition: RedisInternal.h:70
RedisObject::RedisObject
RedisObject(Type tc, Client &c)
Definition: RedisInternal.h:39
RedisObject::data
String data
Definition: RedisInternal.h:62
RedisBulkString::RedisBulkString
RedisBulkString(String &s)
Definition: RedisInternal.h:81
RedisInternalError::UnknownType
@ UnknownType
Definition: RedisInternal.h:135