mio
mio copied to clipboard
Question About Usage
Hello,
Let me apologize in advance for dumb question.
I am trying to figure out a cross-platform way to store and access structs via memory mapped files.
I guess the question is how do I perform operations with mio on types wider than byte or char?
Thanks in Advance
struct Tick
{
double Price;
int Volume;
};
const auto path = "ticks.bin";
std::error_code error;
mio::mmap_sink rw_mmap = mio::make_mmap_sink(path, 0, mio::map_entire_file, error);
for(Tick *t = rw_mmap->begin(); t != rw_mmap->end(); ++t)
{
t->Price = 5; //
}
Hello @unclepaul84,
I'm thrilled that you're giving mio a go! Unfortunately serialization is not supported, mio serves as a thin wrapper around the platform memory mapping facilities, which also don't provide this feature, afaik.
This could be a feature on top of mio, a serialization framework, but I think it may be better suited as a separate library to keep separation of concerns clearer.
Happy to accept feedback, of course.
I would need this too (reading only). Any idea how to easily implement this on top of 'mio' for this int-double struct?
What I have currently:
struct indval {
int i;
double x;
};
std::error_code error;
mio::mmap_source ro_mmap;
ro_mmap.map(path, error);
const indval * data = reinterpret_cast<const indval*>(ro_mmap.data());
cout << data->i << " // " << data->x << std::endl;
@privefl what you're doing is bound to break because of padding and alignment. you can use Boost.serialization or, if you prefer something smaller, cereal
I'm doing exactly what @privefl suggested and it works. When the original file is created sizeof(indval) bytes is written to disk. This will include any padding bytes added by the compiler. It's just a binary blob of bytes. As long as you use the same padding the bytes on disk and in memory are the same. This is highly non portable of course
I've finally used two doubles, for safety.