c++ - Combine Multiple Data Types To Formulate A packet -


i'm attempting create class combine variables of multiple types single variable can used send remote machine , reconstruct class data.

you construct new packet, packet have 'data variable', not sure type of variable should be, networking library allows data sent 'const void *'

datapacket *packet = new datapacket(); 

add data packet, passes value of type specify , 'appends' onto end of 'data variable'

packet.adddata<int>(45); packet.adddata<bool>(true); packet.adddata<const char*>("test"); 

send packet off, send 'data variable' remote machine through networking library

packet.send(); 

construct packet on receiving machine passing 'data variable' sent, construct copy of packet sent sending machine

datapacket *packet = new datapacket(data); 

read out data in same order wrote moving through 'data variable' start end little more every time getdata called

int var1 = packet.getdata<int>(); bool var2 = packet.getdata<bool>(); const char* var3 = packet.getdata<const char*>(); 

what i'm confused on type of variable should 'data variable' be, how construct identical packet 'data variable', , how put variables in , out of 'data variable' set functions.

edit: seem have misunderstood question. if want transmit data remote machine, i.e. on network or need serialization library boost.serialization: http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html. converting types, non trivial types, stream of bytes , converting them again not easy task. go boost.

my original answer works send data around inside process not outside process:

if don't want use boost.any or boost.variant can roll own using excelent non intrusive polymorphism pattern sean parent:

class packetdata{     struct context {         virtual ~context() = default;     };     template<typename t>     struct instance : context {         instance(t&& t):data_{std::move(t)}{          }         t data_;     };     std::unique_ptr<context> self_;     public:     template<typename t>     packetdata(t in):self_{new instance<t>{std::move(in)}}{}     //move easy, copy harder ;)      template<typename t>     bool isa(){         return dynamic_cast<instance<t>*>(self_.get()) != nullptr;     }     template<typename t>     t& asa(){         if(!isa<t>()){             throw std::runtime_error("bad cast or something");         }         return dynamic_cast<instance<t>*>(self_.get())->data_;     } }; 

after making variant class rest should easy:

using packet = std::vector<packetdata>;   packet packet; packet.push_back(4); packet.push_back(4.4f); packet.push_back(std::string{"test"}); int = packet[0].asa<int>(); float f = packet[1].asa<float>(); std::string s = packet[2].asa<std::string>(); 

here live example: http://ideone.com/u7rmol

if want speed around heap allocation puting stack allocator in packetdata class big enough hold usual data sizes. see http://home.roadrunner.com/~hinnant/stack_alloc.html


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -