Should I use a vector or a string or...
I need to use a variable length array to store binary data AND strings of text. I''ve looked at string and vectors. Vectors seem the obvious choice like:
typedef vector ByteArray
,however as easy as it would be to implement I also need the ability to
-copy in text easily (Rather than looping to fill the ByteArray with []
-Copy a struct on top of it. ie, I have a bitfield struct that will form a message.
In C I would have done this easily by just mallocing out a hunk of ram and copying the data right in with memcpy. How can I do this in a C++ kind of way?
gimp
Chris Brodie
hmmm, so you want a single variable to contain any kind of data? Sounds like your talking about using void pointers (void*), but that''s not a very C++ way of going about things (since you asked).
If this is a message to be sent from one task to another, you might want to make a base class message, then derive each message type from that message.
But if you just want to go for raw storage, you can create a byte vector (vector<char> or vector<unsigned char>), then just use STL copy and have nasty casts all over the place:
struct MyStruct
{
int a;
char b;
double c;
} s;
copy ((char*) &s, (char*) &s + sizeof s, vec.begin ());
This would work, but you can see how very ugly and unintuitive (i.e. somebody reading this would say, "what the hell is he doing?!") this solution is. Try seeing if a class heirarchy will solve your problem instead.
If this is a message to be sent from one task to another, you might want to make a base class message, then derive each message type from that message.
But if you just want to go for raw storage, you can create a byte vector (vector<char> or vector<unsigned char>), then just use STL copy and have nasty casts all over the place:
struct MyStruct
{
int a;
char b;
double c;
} s;
copy ((char*) &s, (char*) &s + sizeof s, vec.begin ());
This would work, but you can see how very ugly and unintuitive (i.e. somebody reading this would say, "what the hell is he doing?!") this solution is. Try seeing if a class heirarchy will solve your problem instead.
store a vector of pairs.
eg:
std::vector< std::pair >
just replace the int with whatever you''re binary data is.
access example:
eg:
std::vector< std::pair >
just replace the int with whatever you''re binary data is.
access example:
std::vector< std::pair<int, string> >::iterator IT;for(IT = myVector.begin(); IT != myVector.end(); ++IT){ cout << IT->first << " : " << IT->second << endl;}
i have some time on my hands, so i''ll show how to add to the vector too...
void foo(std::vector< std::pair<int, std::string> >& myVector, int n, std::string s){ //make a pair containing int value ''n'' and string ''s'' std::pair<int, std::string> thePair(n, s); myVector.push_back(thePair); //OR, for even less to type: myVector.push_back(std::make_pair(n, s)); //note: that make_pair does not work for me - which is why i showed the other way first. //if i''m doing the make_pair bit wrong - someone please correct me!}
Thanks guys...
Here is a better explaination of what I''m doing. The core part of the game is driven by message queue''s. For example, when a new packet is recieved from the server it is unpacked in to a number of individual messages. Each message is represented by binary data, maybe a few bytes. Next from the command line, console, and config files I recieve text based messages like "map blackforest".
I have pluggable factories reading the queue and passing the data to each processing function. Now initially the obvious choice for storing the data was a unsigned char*, that allowed me to copy in the say 5 byte message or a 12 byte text message. My binary data didn''t get messed with as the char was unsigned, but character data was still validly represented. I could easily copy in the binary data from structs too or ditto with the text messages...
I was trying to be a good gimp and do it the c++ way but increasingly the C way looks easier...
Here is a better explaination of what I''m doing. The core part of the game is driven by message queue''s. For example, when a new packet is recieved from the server it is unpacked in to a number of individual messages. Each message is represented by binary data, maybe a few bytes. Next from the command line, console, and config files I recieve text based messages like "map blackforest".
I have pluggable factories reading the queue and passing the data to each processing function. Now initially the obvious choice for storing the data was a unsigned char*, that allowed me to copy in the say 5 byte message or a 12 byte text message. My binary data didn''t get messed with as the char was unsigned, but character data was still validly represented. I could easily copy in the binary data from structs too or ditto with the text messages...
I was trying to be a good gimp and do it the c++ way but increasingly the C way looks easier...
Chris Brodie
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement