forked from RedisCppTeam/redis-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCResult.cpp
225 lines (185 loc) · 3.96 KB
/
CResult.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "CResult.h"
#include <sstream>
#include "RdException.hpp"
CResult::CResult():
_type( REDIS_REPLY_NIL )
{
_arry.clear();
}
CResult::CResult( const CResult& other ):
std::string( other ),
_type( other._type ),
_arry( other._arry )
{
}
CResult::CResult(const std::string &value):
std::string( value ),
_type( REDIS_REPLY_NIL )
{
_arry.clear();
}
CResult::~CResult()
{
}
void CResult::setType(const ReplyType e)
{
_type = e;
}
ReplyType CResult::getType() const
{
return _type;
}
bool CResult::addElement(const CResult &ele)
{
if ( _type != REDIS_REPLY_ARRAY )
{
return false;
}
_arry.push_back( ele );
return true;
}
const CResult::ListCResult &CResult::getArry( void ) const
{
if ( _type != REDIS_REPLY_ARRAY )
{
throw TypeErr( "Data is not arry type" );
}
return _arry;
}
int64_t CResult::getInt(void) const
{
if ( _type != REDIS_REPLY_INTEGERER )
{
throw TypeErr( "Data is not int type" );
}
int64_t value;
std::istringstream istr( std::string(*this) );
istr >> value;
if ( istr.fail() )
{
throw TypeErr( "Data is not int type" );
}
return value;
}
string CResult::getString( void ) const
{
if ( _type != REDIS_REPLY_STRING )
{
throw TypeErr( "Data is not string type" );
}
return *this;
}
string CResult::getErrorString( void ) const
{
if ( _type != REDIS_REPLY_ERROR )
{
throw TypeErr( "Data is not error type" );
}
return *this;
}
string CResult::getStatus( void ) const
{
if ( _type != REDIS_REPLY_STATUS )
{
throw TypeErr( "Data is not error type" );
}
return *this;
}
std::string CResult::display( const CResult &ele, int indent )
{
ReplyType e = ele.getType( );
string type =CResult::getTypeString( e );
std::stringstream out;
if ( REDIS_REPLY_ARRAY == e )
{
CResult::ListCResult::const_iterator it = ele.getArry().begin() ;
indent += 3;
out <<"{\n";
for ( int i = 0; i < indent; i++ )
out << " ";
indent += 3;
out << "type:" << type <<", value:[" << std::endl;
for ( ; it!=ele.getArry().end(); it++ )
{
for ( int i = 0; i < indent; i++ )
out << " ";
out << display( *it, indent ) << std::endl;
}
for ( int i = 0; i < indent-3; i++ )
out << " ";
out << "]\n";
for ( int i = 0; i < indent-6; i++ )
out << " ";
out << "};";
}else
{
out << "{type: " << type << " ,value: " << std::string( ele ) << "};";
}
return out.str() ;
}
void CResult::assign( const CResult &other)
{
if ( this == &other )
{
return;
}
_type = other._type;
string::assign( other );
_arry = other._arry;
return;
}
CResult &CResult::operator=( const std::string &value )
{
if ( this == &value )
{
return *this;
}
string::assign( value );
return *this;
}
CResult &CResult::operator=( const CResult &other)
{
assign( other );
return *this;
}
void CResult::clear()
{
_type = REDIS_REPLY_NIL;
std::string::clear();
_arry.clear();
}
string CResult::getTypeString( ReplyType e )
{
string type;
switch ( e )
{
case REDIS_REPLY_ARRAY:
type = "REPLY_ARRY";
break;
case REDIS_REPLY_ERROR :
type = "REPLY_ERROR";
break;
case REDIS_REPLY_INTEGERER:
type = "REPLY_INTEGERER";
break;
case REDIS_REPLY_NIL :
type = "REPLY_NIL";
break;
case REDIS_REPLY_STATUS :
type = "REPLY_STATUS";
break;
case REDIS_REPLY_STRING :
type = "REPLY_STRING";
break;
default:
type = "REPLY_UNKNOW";
break;
}
return type;
}
std::ostream &operator<<(std::ostream& out, const CResult &value)
{
string str = CResult::display( value, 0 );
out << str;
return out ;
}