c++ - pack any data type into vector -
c++ - pack any data type into vector <uint8_t> -
this question resembles
serialize info type vector<uint8_t> - utilize reinterpret_cast?
template <typename t> inline void pack (std::vector< uint8_t >& dst, t& data) { uint8_t * src = static_cast < uint8_t* >(static_cast < void * >(&data)); dst.insert (dst.end (), src, src + sizeof (t)); }
to unpack
template <typename t> inline void unpack (vector <uint8_t >& src, int index, t& data) { re-create (&src[index], &src[index + sizeof (t)], &data); }
i trying pack type of info byte array .
q1 :i have existing tedious implementation using uint8_t* , hope selection of vector best.
q2 : not able pack std::string above function . please allow me know how comfortable above function in packing types of info types
please allow me know how incorprate std::string above soluntion e pack , std::string vector
types of info want pack :
all pods std::string vector itself..
outer problem :
i want pack class these byte array
class startpeersessionrequest : public request { public: startpeersessionrequest(); virtual ~startpeersessionrequest(); void composerequestwithhardcodevalues(); vector<uint8_t> packrequestwithtemplate(); private: uint16_t mprotocolversion; uint16_t msessionflags; uint16_t mmaxresponselength; string mmake; string mmodel; string mserialnumber; uint8_t mtrackdelay; string mheadunitmodel; string mcarmodelyear; string mvin; uint16_t mvehiclemileage; uint8_t mshoutformat; uint8_t mnotificationinterval; }; class message { public: message(); virtual ~message(); void composemessage(vector<uint8_t> data, uint16_t opcode, uint16_t lengthofdata); uint16_t packetheader; uint16_t length; uint16_t request_response_id; uint16_t opcode; uint16_t checksum; vector<uint8_t> data; }
byte array info type have chosen vector(uint8_t)
i want write device file or send on bluetooth network. dont want deserialize same class is. receive response 1 time again byte array , need unpack response class
tl;dr: utilize serialization library boost.serialization or protocol buffers.
ad 1) vector ok. edit: stream better.
ad 2) well, can't serialize objects indirection way, because you'll pointer actual data, not info itself. , can serialize plain old data (standard layout in c++11) objects; or rather not guaranteed deserializing object not plain old data/standard layout result in working object. std::string
(nor other container) not pod , contains indirection (std::string
special kind of vector).
there no way serialize/pack arbitrary non-standard-layout objects byte array without special support. either have write serialization , deserialization functions each such type or have give byte array requirement , utilize boost::any maintain type info , phone call constructors , destructors behind scenes. note boost::any
non-standard-layout object indirection.
ad edit:
i don't want deserialize same class is.
yes, do. @ other end of connection. either
the other end uses same construction , have write both serialization , deserialization, 1 used @ 1 end , other @ other end, the other end uses different construction (possibly in different language) , work on both sides, have write matching serialize , deserialize, or the other end written else , have write serialization utilize specific agreed format.there thousands of ways serialize every type. string can serialized length , content or content terminated designated terminator (usually 0 byte), integer (and string size integer) can serialized different fixed number of bytes, in different order (endian), utilize variable length encoding etc.
and there changes. see did include protocol version. have write deserialization code different things based on version, means doing member-by-member anyway (you want output same construction independent on version maintain downstream code sane) etc.
if don't have protocol decided, recommend @ boost.serialization , protocol buffers libraries.
boost.serialization glues existing objects, specific c++ , provides backward compatibility, i.e. if sender sends version 2 , receiver not updated, message won't understood @ all. protocol buffers requires defining messages in it's special format , generating c++ definition that, there generators languages , supports forwards compatibility, i.e. if sender sends version 2, receiver understand fields in version 1 , ignore ones not.if reason can't utilize them (unless target extremely constrained should able to; work on mobile application uses both boost , protobuf , runs on current major mobile platforms), @ to the lowest degree read technical descriptions , perchance in code know how serialization well.
c++ vector stdstring
Comments
Post a Comment