Skip to content

Commit 36059f1

Browse files
committed
Merge pull request #64 from macq-vraman/master
Improved message API
2 parents 2fe3373 + 846356e commit 36059f1

File tree

1 file changed

+190
-25
lines changed

1 file changed

+190
-25
lines changed

src/sio_message.h

Lines changed: 190 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ namespace sio
2727
flag_binary,
2828
flag_array,
2929
flag_object,
30-
flag_boolean,
31-
flag_null
30+
flag_boolean,
31+
flag_null
3232
};
3333

34-
virtual ~message(){};
34+
virtual ~message(){};
3535

3636
class list;
3737

@@ -42,11 +42,11 @@ namespace sio
4242

4343
typedef shared_ptr<message> ptr;
4444

45-
virtual bool get_bool() const
46-
{
47-
assert(false);
48-
return false;
49-
}
45+
virtual bool get_bool() const
46+
{
47+
assert(false);
48+
return false;
49+
}
5050

5151
virtual int64_t get_int() const
5252
{
@@ -114,26 +114,26 @@ namespace sio
114114
message(flag f):_flag(f){}
115115
};
116116

117-
class null_message : public message
118-
{
119-
protected:
117+
class null_message : public message
118+
{
119+
protected:
120120
null_message()
121-
:message(flag_null)
121+
:message(flag_null)
122122
{
123123
}
124124

125-
public:
125+
public:
126126
static message::ptr create()
127127
{
128128
return ptr(new null_message());
129129
}
130-
};
130+
};
131131

132-
class bool_message : public message
133-
{
134-
bool _v;
132+
class bool_message : public message
133+
{
134+
bool _v;
135135

136-
protected:
136+
protected:
137137
bool_message(bool v)
138138
:message(flag_boolean),_v(v)
139139
{
@@ -149,7 +149,7 @@ namespace sio
149149
{
150150
return _v;
151151
}
152-
};
152+
};
153153

154154
class int_message : public message
155155
{
@@ -257,7 +257,77 @@ namespace sio
257257
{
258258
return ptr(new array_message());
259259
}
260-
260+
261+
void push(message::ptr const& message)
262+
{
263+
if(message)
264+
_v.push_back(message);
265+
}
266+
267+
void push(const string& text)
268+
{
269+
_v.push_back(string_message::create(text));
270+
}
271+
272+
void push(string&& text)
273+
{
274+
_v.push_back(string_message::create(move(text)));
275+
}
276+
277+
void push(shared_ptr<string> const& binary)
278+
{
279+
if(binary)
280+
_v.push_back(binary_message::create(binary));
281+
}
282+
283+
void push(shared_ptr<const string> const& binary)
284+
{
285+
if(binary)
286+
_v.push_back(binary_message::create(binary));
287+
}
288+
289+
void insert(size_t pos,message::ptr const& message)
290+
{
291+
_v.insert(_v.begin()+pos, message);
292+
}
293+
294+
void insert(size_t pos,const string& text)
295+
{
296+
_v.insert(_v.begin()+pos, string_message::create(text));
297+
}
298+
299+
void insert(size_t pos,string&& text)
300+
{
301+
_v.insert(_v.begin()+pos, string_message::create(move(text)));
302+
}
303+
304+
void insert(size_t pos,shared_ptr<string> const& binary)
305+
{
306+
if(binary)
307+
_v.insert(_v.begin()+pos, binary_message::create(binary));
308+
}
309+
310+
void insert(size_t pos,shared_ptr<const string> const& binary)
311+
{
312+
if(binary)
313+
_v.insert(_v.begin()+pos, binary_message::create(binary));
314+
}
315+
316+
size_t size() const
317+
{
318+
return _v.size();
319+
}
320+
321+
const message::ptr& at(size_t i) const
322+
{
323+
return _v[i];
324+
}
325+
326+
const message::ptr& operator[] (size_t i) const
327+
{
328+
return _v[i];
329+
}
330+
261331
vector<ptr>& get_vector()
262332
{
263333
return _v;
@@ -280,7 +350,58 @@ namespace sio
280350
{
281351
return ptr(new object_message());
282352
}
283-
353+
354+
void insert(const string & key,message::ptr const& message)
355+
{
356+
_v[key] = message;
357+
}
358+
359+
void insert(const string & key,const string& text)
360+
{
361+
_v[key] = string_message::create(text);
362+
}
363+
364+
void insert(const string & key,string&& text)
365+
{
366+
_v[key] = string_message::create(move(text));
367+
}
368+
369+
void insert(const string & key,shared_ptr<string> const& binary)
370+
{
371+
if(binary)
372+
_v[key] = binary_message::create(binary);
373+
}
374+
375+
void insert(const string & key,shared_ptr<const string> const& binary)
376+
{
377+
if(binary)
378+
_v[key] = binary_message::create(binary);
379+
}
380+
381+
bool has(const string & key)
382+
{
383+
return _v.find(key) != _v.end();
384+
}
385+
386+
const message::ptr& at(const string & key) const
387+
{
388+
static shared_ptr<message> not_found;
389+
390+
map<string,message::ptr>::const_iterator it = _v.find(key);
391+
if (it != _v.end()) return it->second;
392+
return not_found;
393+
}
394+
395+
const message::ptr& operator[] (const string & key) const
396+
{
397+
return at(key);
398+
}
399+
400+
bool has(const string & key) const
401+
{
402+
return _v.find(key) != _v.end();
403+
}
404+
284405
map<string,message::ptr>& get_map()
285406
{
286407
return _v;
@@ -315,10 +436,10 @@ namespace sio
315436
return *this;
316437
}
317438

318-
template <typename T>
319-
list(T&& content,
320-
typename enable_if<is_same<vector<message::ptr>,typename remove_reference<T>::type>::value>::type* = 0):
321-
m_vector(std::forward<T>(content))
439+
template <typename T>
440+
list(T&& content,
441+
typename enable_if<is_same<vector<message::ptr>,typename remove_reference<T>::type>::value>::type* = 0):
442+
m_vector(std::forward<T>(content))
322443
{
323444
}
324445

@@ -332,6 +453,7 @@ namespace sio
332453
{
333454
if(message)
334455
m_vector.push_back(message);
456+
335457
}
336458

337459
list(const string& text)
@@ -360,14 +482,57 @@ namespace sio
360482
{
361483
if(message)
362484
m_vector.push_back(message);
485+
}
486+
487+
void push(const string& text)
488+
{
489+
m_vector.push_back(string_message::create(text));
490+
}
491+
492+
void push(string&& text)
493+
{
494+
m_vector.push_back(string_message::create(move(text)));
495+
}
363496

497+
void push(shared_ptr<string> const& binary)
498+
{
499+
if(binary)
500+
m_vector.push_back(binary_message::create(binary));
501+
}
502+
503+
void push(shared_ptr<const string> const& binary)
504+
{
505+
if(binary)
506+
m_vector.push_back(binary_message::create(binary));
364507
}
365508

366509
void insert(size_t pos,message::ptr const& message)
367510
{
368511
m_vector.insert(m_vector.begin()+pos, message);
369512
}
370513

514+
void insert(size_t pos,const string& text)
515+
{
516+
m_vector.insert(m_vector.begin()+pos, string_message::create(text));
517+
}
518+
519+
void insert(size_t pos,string&& text)
520+
{
521+
m_vector.insert(m_vector.begin()+pos, string_message::create(move(text)));
522+
}
523+
524+
void insert(size_t pos,shared_ptr<string> const& binary)
525+
{
526+
if(binary)
527+
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
528+
}
529+
530+
void insert(size_t pos,shared_ptr<const string> const& binary)
531+
{
532+
if(binary)
533+
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
534+
}
535+
371536
size_t size() const
372537
{
373538
return m_vector.size();

0 commit comments

Comments
 (0)