123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- //
- // sio_message.h
- //
- // Created by Melo Yao on 3/25/15.
- //
- #ifndef __SIO_MESSAGE_H__
- #define __SIO_MESSAGE_H__
- #include <string>
- #include <memory>
- #include <vector>
- #include <map>
- #include <cassert>
- #include <type_traits>
- #include <sstream>
- namespace sio
- {
- class message
- {
- public:
- enum flag
- {
- flag_integer,
- flag_double,
- flag_string,
- flag_binary,
- flag_array,
- flag_object,
- flag_boolean,
- flag_null
- };
- virtual ~message(){};
- class list;
- flag get_flag() const
- {
- return _flag;
- }
- typedef std::shared_ptr<message> ptr;
- virtual bool get_bool() const
- {
- assert(false);
- return false;
- }
- virtual int64_t get_int() const
- {
- assert(false);
- return 0;
- }
- virtual double get_double() const
- {
- assert(false);
- return 0;
- }
- virtual std::string to_string() const
- {
- assert(false);
- static std::string s_empty_string;
- s_empty_string.clear();
- return s_empty_string;
- }
- virtual std::string const& get_string() const
- {
- assert(false);
- static std::string s_empty_string;
- s_empty_string.clear();
- return s_empty_string;
- }
- virtual std::shared_ptr<const std::string> const& get_binary() const
- {
- assert(false);
- static std::shared_ptr<const std::string> s_empty_binary;
- s_empty_binary = nullptr;
- return s_empty_binary;
- }
- virtual const std::vector<ptr>& get_vector() const
- {
- assert(false);
- static std::vector<ptr> s_empty_vector;
- s_empty_vector.clear();
- return s_empty_vector;
- }
- virtual std::vector<ptr>& get_vector()
- {
- assert(false);
- static std::vector<ptr> s_empty_vector;
- s_empty_vector.clear();
- return s_empty_vector;
- }
- virtual const std::map<std::string,message::ptr>& get_map() const
- {
- assert(false);
- static std::map<std::string,message::ptr> s_empty_map;
- s_empty_map.clear();
- return s_empty_map;
- }
- virtual std::map<std::string,message::ptr>& get_map()
- {
- assert(false);
- static std::map<std::string,message::ptr> s_empty_map;
- s_empty_map.clear();
- return s_empty_map;
- }
- private:
- flag _flag;
- protected:
- message(flag f):_flag(f){}
- };
- class null_message : public message
- {
- protected:
- null_message()
- :message(flag_null)
- {
- }
- public:
- static message::ptr create()
- {
- return ptr(new null_message());
- }
- };
- class bool_message : public message
- {
- bool _v;
- protected:
- bool_message(bool v)
- :message(flag_boolean),_v(v)
- {
- }
- public:
- static message::ptr create(bool v)
- {
- return ptr(new bool_message(v));
- }
- bool get_bool() const
- {
- return _v;
- }
- std::string to_string()const
- {
- return _v?"true":"false";
- }
- };
- class int_message : public message
- {
- int64_t _v;
- protected:
- int_message(int64_t v)
- :message(flag_integer),_v(v)
- {
- }
- public:
- static message::ptr create(int64_t v)
- {
- return ptr(new int_message(v));
- }
- int64_t get_int() const
- {
- return _v;
- }
- double get_double() const//add double accessor for integer.
- {
- return static_cast<double>(_v);
- }
- std::string to_string()const
- {
- return std::to_string(_v);
- }
- };
- class double_message : public message
- {
- double _v;
- double_message(double v)
- :message(flag_double),_v(v)
- {
- }
- public:
- static message::ptr create(double v)
- {
- return ptr(new double_message(v));
- }
- double get_double() const
- {
- return _v;
- }
- std::string to_string()const
- {
- return std::to_string(_v);
- }
- };
- class string_message : public message
- {
- std::string _v;
- string_message(std::string const& v)
- :message(flag_string),_v(v)
- {
- }
- string_message(std::string&& v)
- :message(flag_string),_v(move(v))
- {
- }
- public:
- static message::ptr create(std::string const& v)
- {
- return ptr(new string_message(v));
- }
- static message::ptr create(std::string&& v)
- {
- return ptr(new string_message(move(v)));
- }
- std::string const& get_string() const
- {
- return _v;
- }
- std::string to_string()const
- {
- return "'"+_v+"'";
- }
- };
- class binary_message : public message
- {
- std::shared_ptr<const std::string> _v;
- binary_message(std::shared_ptr<const std::string> const& v)
- :message(flag_binary),_v(v)
- {
- }
- public:
- static message::ptr create(std::shared_ptr<const std::string> const& v)
- {
- return ptr(new binary_message(v));
- }
- std::shared_ptr<const std::string> const& get_binary() const
- {
- return _v;
- }
- };
- class array_message : public message
- {
- std::vector<message::ptr> _v;
- array_message():message(flag_array)
- {
- }
- public:
- static message::ptr create()
- {
- return ptr(new array_message());
- }
- void push(message::ptr const& message)
- {
- if(message)
- _v.push_back(message);
- }
- void push(const std::string& text)
- {
- _v.push_back(string_message::create(text));
- }
- void push(std::string&& text)
- {
- _v.push_back(string_message::create(move(text)));
- }
- void push(std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- _v.push_back(binary_message::create(binary));
- }
- void push(std::shared_ptr<const std::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 std::string& text)
- {
- _v.insert(_v.begin()+pos, string_message::create(text));
- }
- void insert(size_t pos,std::string&& text)
- {
- _v.insert(_v.begin()+pos, string_message::create(move(text)));
- }
- void insert(size_t pos,std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- _v.insert(_v.begin()+pos, binary_message::create(binary));
- }
- void insert(size_t pos,std::shared_ptr<const std::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];
- }
- std::vector<ptr>& get_vector()
- {
- return _v;
- }
- const std::vector<ptr>& get_vector() const
- {
- return _v;
- }
- std::string to_string()const
- {
- std::ostringstream ss;
- ss<<"[";
- for(size_t i=0;i<size();i++)
- {
- ss<<_v[i]->to_string()<<",";
- }
- ss<<"]";
- return ss.str();
- }
- };
- class object_message : public message
- {
- std::map<std::string,message::ptr> _v;
- object_message() : message(flag_object)
- {
- }
- public:
- static message::ptr create()
- {
- return ptr(new object_message());
- }
- void insert(const std::string & key,message::ptr const& message)
- {
- _v[key] = message;
- }
- void insert(const std::string & key,const std::string& text)
- {
- _v[key] = string_message::create(text);
- }
- void insert(const std::string & key,std::string&& text)
- {
- _v[key] = string_message::create(move(text));
- }
- void insert(const std::string & key,std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- _v[key] = binary_message::create(binary);
- }
- void insert(const std::string & key,std::shared_ptr<const std::string> const& binary)
- {
- if(binary)
- _v[key] = binary_message::create(binary);
- }
- bool has(const std::string & key)
- {
- return _v.find(key) != _v.end();
- }
- const message::ptr& at(const std::string & key) const
- {
- static std::shared_ptr<message> not_found;
- std::map<std::string,message::ptr>::const_iterator it = _v.find(key);
- if (it != _v.cend()) return it->second;
- return not_found;
- }
- const message::ptr& operator[] (const std::string & key) const
- {
- return at(key);
- }
- bool has(const std::string & key) const
- {
- return _v.find(key) != _v.end();
- }
- std::map<std::string,message::ptr>& get_map()
- {
- return _v;
- }
- const std::map<std::string,message::ptr>& get_map() const
- {
- return _v;
- }
- std::string to_string()const
- {
- std::ostringstream ss;
- ss<<"{";
- for(auto&it:_v)
- {
- ss<<it.first<<":"<<it.second->to_string()<<",";
- }
- ss<<"}";
- return ss.str();
- }
- };
- class message::list
- {
- public:
- list()
- {
- }
- list(std::nullptr_t)
- {
- }
- list(message::list&& rhs):
- m_vector(std::move(rhs.m_vector))
- {
- }
- list & operator= (const message::list && rhs)
- {
- m_vector = std::move(rhs.m_vector);
- return *this;
- }
- template <typename T>
- list(T&& content,
- typename std::enable_if<std::is_same<std::vector<message::ptr>,typename std::remove_reference<T>::type>::value>::type* = 0):
- m_vector(std::forward<T>(content))
- {
- }
- list(message::list const& rhs):
- m_vector(rhs.m_vector)
- {
- }
- list(message::ptr const& message)
- {
- if(message)
- m_vector.push_back(message);
- }
- list(const std::string& text)
- {
- m_vector.push_back(string_message::create(text));
- }
- list(std::string&& text)
- {
- m_vector.push_back(string_message::create(move(text)));
- }
- list(std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- m_vector.push_back(binary_message::create(binary));
- }
- list(std::shared_ptr<const std::string> const& binary)
- {
- if(binary)
- m_vector.push_back(binary_message::create(binary));
- }
- void push(message::ptr const& message)
- {
- if(message)
- m_vector.push_back(message);
- }
- void push(const std::string& text)
- {
- m_vector.push_back(string_message::create(text));
- }
- void push(std::string&& text)
- {
- m_vector.push_back(string_message::create(move(text)));
- }
- void push(std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- m_vector.push_back(binary_message::create(binary));
- }
- void push(std::shared_ptr<const std::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 std::string& text)
- {
- m_vector.insert(m_vector.begin()+pos, string_message::create(text));
- }
- void insert(size_t pos,std::string&& text)
- {
- m_vector.insert(m_vector.begin()+pos, string_message::create(move(text)));
- }
- void insert(size_t pos,std::shared_ptr<std::string> const& binary)
- {
- if(binary)
- m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
- }
- void insert(size_t pos,std::shared_ptr<const std::string> const& binary)
- {
- if(binary)
- m_vector.insert(m_vector.begin()+pos, binary_message::create(binary));
- }
- size_t size() const
- {
- return m_vector.size();
- }
- const message::ptr& at(size_t i) const
- {
- return m_vector[i];
- }
- const message::ptr& operator[] (size_t i) const
- {
- return m_vector[i];
- }
- message::ptr to_array_message(std::string const& event_name) const
- {
- message::ptr arr = array_message::create();
- arr->get_vector().push_back(string_message::create(event_name));
- arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
- return arr;
- }
- message::ptr to_array_message() const
- {
- message::ptr arr = array_message::create();
- arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
- return arr;
- }
- std::string to_string()const
- {
- std::ostringstream ss;
- ss<<"[";
- for(size_t i=0;i<size();i++)
- {
- ss<<m_vector[i]->to_string()<<",";
- }
- ss<<"]";
- return ss.str();
- }
- private:
- std::vector<message::ptr> m_vector;
- };
- }
- #endif
|