Skip to content

Improved message API #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 190 additions & 25 deletions src/sio_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ namespace sio
flag_binary,
flag_array,
flag_object,
flag_boolean,
flag_null
flag_boolean,
flag_null
};

virtual ~message(){};
virtual ~message(){};

class list;

Expand All @@ -42,11 +42,11 @@ namespace sio

typedef shared_ptr<message> ptr;

virtual bool get_bool() const
{
assert(false);
return false;
}
virtual bool get_bool() const
{
assert(false);
return false;
}

virtual int64_t get_int() const
{
Expand Down Expand Up @@ -114,26 +114,26 @@ namespace sio
message(flag f):_flag(f){}
};

class null_message : public message
{
protected:
class null_message : public message
{
protected:
null_message()
:message(flag_null)
:message(flag_null)
{
}

public:
public:
static message::ptr create()
{
return ptr(new null_message());
}
};
};

class bool_message : public message
{
bool _v;
class bool_message : public message
{
bool _v;

protected:
protected:
bool_message(bool v)
:message(flag_boolean),_v(v)
{
Expand All @@ -149,7 +149,7 @@ namespace sio
{
return _v;
}
};
};

class int_message : public message
{
Expand Down Expand Up @@ -257,7 +257,77 @@ namespace sio
{
return ptr(new array_message());
}


void push(message::ptr const& message)
{
if(message)
_v.push_back(message);
}

void push(const string& text)
{
_v.push_back(string_message::create(text));
}

void push(string&& text)
{
_v.push_back(string_message::create(move(text)));
}

void push(shared_ptr<string> const& binary)
{
if(binary)
_v.push_back(binary_message::create(binary));
}

void push(shared_ptr<const string> const& binary)
{
if(binary)
_v.push_back(binary_message::create(binary));
}

void insert(size_t pos,message::ptr const& message)
{
_v.insert(_v.begin()+pos, message);
}

void insert(size_t pos,const string& text)
{
_v.insert(_v.begin()+pos, string_message::create(text));
}

void insert(size_t pos,string&& text)
{
_v.insert(_v.begin()+pos, string_message::create(move(text)));
}

void insert(size_t pos,shared_ptr<string> const& binary)
{
if(binary)
_v.insert(_v.begin()+pos, binary_message::create(binary));
}

void insert(size_t pos,shared_ptr<const string> const& binary)
{
if(binary)
_v.insert(_v.begin()+pos, binary_message::create(binary));
}

size_t size() const
{
return _v.size();
}

const message::ptr& at(size_t i) const
{
return _v[i];
}

const message::ptr& operator[] (size_t i) const
{
return _v[i];
}

vector<ptr>& get_vector()
{
return _v;
Expand All @@ -280,7 +350,58 @@ namespace sio
{
return ptr(new object_message());
}


void insert(const string & key,message::ptr const& message)
{
_v[key] = message;
}

void insert(const string & key,const string& text)
{
_v[key] = string_message::create(text);
}

void insert(const string & key,string&& text)
{
_v[key] = string_message::create(move(text));
}

void insert(const string & key,shared_ptr<string> const& binary)
{
if(binary)
_v[key] = binary_message::create(binary);
}

void insert(const string & key,shared_ptr<const string> const& binary)
{
if(binary)
_v[key] = binary_message::create(binary);
}

bool has(const string & key)
{
return _v.find(key) != _v.end();
}

const message::ptr& at(const string & key) const
{
static shared_ptr<message> not_found;

map<string,message::ptr>::const_iterator it = _v.find(key);
if (it != _v.end()) return it->second;
return not_found;
}

const message::ptr& operator[] (const string & key) const
{
return at(key);
}

bool has(const string & key) const
{
return _v.find(key) != _v.end();
}

map<string,message::ptr>& get_map()
{
return _v;
Expand Down Expand Up @@ -315,10 +436,10 @@ namespace sio
return *this;
}

template <typename T>
list(T&& content,
typename enable_if<is_same<vector<message::ptr>,typename remove_reference<T>::type>::value>::type* = 0):
m_vector(std::forward<T>(content))
template <typename T>
list(T&& content,
typename enable_if<is_same<vector<message::ptr>,typename remove_reference<T>::type>::value>::type* = 0):
m_vector(std::forward<T>(content))
{
}

Expand All @@ -332,6 +453,7 @@ namespace sio
{
if(message)
m_vector.push_back(message);

}

list(const string& text)
Expand Down Expand Up @@ -360,14 +482,57 @@ namespace sio
{
if(message)
m_vector.push_back(message);
}

void push(const string& text)
{
m_vector.push_back(string_message::create(text));
}

void push(string&& text)
{
m_vector.push_back(string_message::create(move(text)));
}

void push(shared_ptr<string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}

void push(shared_ptr<const string> const& binary)
{
if(binary)
m_vector.push_back(binary_message::create(binary));
}

void insert(size_t pos,message::ptr const& message)
{
m_vector.insert(m_vector.begin()+pos, message);
}

void insert(size_t pos,const string& text)
{
m_vector.insert(m_vector.begin()+pos, string_message::create(text));
}

void insert(size_t pos,string&& text)
{
m_vector.insert(m_vector.begin()+pos, string_message::create(move(text)));
}

void insert(size_t pos,shared_ptr<string> const& binary)
{
if(binary)
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
}

void insert(size_t pos,shared_ptr<const string> const& binary)
{
if(binary)
m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
}

size_t size() const
{
return m_vector.size();
Expand Down