I'm building a system, with C++, that uses Tokyo Cabinet (original API in C). The problem is I want to store a class such as:
class Entity {
public:
string entityName;
short type;
vector<another_struct> x;
vector<another_struct> y
vector<string> z;
};
The problem is that vectors and strings have variable length. When I pass a void* (my object) to Tokyo Cabinet so it can store it, I also have to pass the size of the object in bytes. But that can't be trivially done.
What is the best way to determine the number of bytes of an object? Or what is the best way to store variable length objects in Tokyo Cabinet.
I'm already considering looking for serialization libs.
Thanks
You cannot portably treat a non-POD C++ struct/class as a raw sequence of bytes - this is regardless of use of pointers or std::string and std::vector, though the latter virtually guarantee that it will break in practice. You need to serialize the object into a sequence of chars first - I'd suggest Boost.Serialization for a good, flexible cross-platform serialization framework.
I think it is worse than that. The actual storage for the vectors is not contiguous with the rest of the object. You see std::vector<>s keep their data in separate allocations on the heap (so they can expand them if needed). You'll need a API that understands c++ and the STL.
In short. This isn't going to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With