mio icon indicating copy to clipboard operation
mio copied to clipboard

Question About Usage

Open unclepaul84 opened this issue 5 years ago • 6 comments

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; //
      } 

unclepaul84 avatar Jan 15 '20 15:01 unclepaul84

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.

vimpunk avatar Jan 19 '20 17:01 vimpunk

I would need this too (reading only). Any idea how to easily implement this on top of 'mio' for this int-double struct?

privefl avatar Apr 07 '20 11:04 privefl

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 avatar Apr 07 '20 11:04 privefl

@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

sykhro avatar Apr 23 '20 10:04 sykhro

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

spudwa avatar Nov 17 '22 03:11 spudwa

I've finally used two doubles, for safety.

privefl avatar Nov 17 '22 07:11 privefl