test.cxx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "include/mmf.hxx"
  2. #include <iostream>
  3. #pragma pack(push, 1)
  4. struct Data {
  5. char cstr[50];
  6. };
  7. #pragma pack(pop)
  8. auto main(int argc, char* argv[]) -> int {
  9. using namespace pmmf;
  10. if (argc < 2) {
  11. std::cerr << "Required 1 file paths!" << std::endl;
  12. return -1;
  13. }
  14. MappedFile mapped_file(argv[1], ProtectionMode::rw, MapFlag::shared);
  15. if(!mapped_file.isFileOpen()) {
  16. std::cerr << "Can't open \"" << argv[1] << "\" file" << std::endl;
  17. return -1;
  18. }
  19. std::cout << "File \"" << argv[1] << "\" is open" << std::endl
  20. << "| File size: " << mapped_file.getFileSize() << " bytes" << std::endl;
  21. MappedData<Data> second_mapped_array = mapped_file.getMappedData<Data>(sizeof(Data), 2);
  22. if(!second_mapped_array.isMapped()) {
  23. std::cerr << "Can't map second area of \"" << argv[1] << "\" file" << std::endl;
  24. return -1;
  25. }
  26. std::cout << "Second area of \"" << argv[1] << "\" file is mapped" << std::endl;
  27. MappedData<Data> first_mapped_array = mapped_file.getMappedData<Data>(0);
  28. if(!second_mapped_array.isMapped()) {
  29. std::cerr << "Can't map first area of \"" << argv[1] << "\" file" << std::endl;
  30. return -1;
  31. }
  32. std::cout << "First area of \"" << argv[1] << "\" file is mapped" << std::endl;
  33. Data& page_1_data_1 = *first_mapped_array;
  34. Data& page_2_data_1 = *second_mapped_array;
  35. Data& page_2_data_2 = second_mapped_array[1];
  36. new(&page_1_data_1) Data{"Lorem ipsum dolor sit amet, consectetur efficitur"};
  37. if(!first_mapped_array.flush()) {
  38. std::cerr << "Can't flush first area of \"" << argv[1] << "\" file" << std::endl;
  39. return -1;
  40. }
  41. std::cout << "First area of \"" << argv[1] << "\" file is flushed" << std::endl
  42. << "| Recorded data: " << first_mapped_array->cstr << std::endl;
  43. new(&page_2_data_1) Data{"Lorem ipsum dolor sit amet, consectetur tincidunt"};
  44. new(&page_2_data_2) Data{"Lorem ipsum dolor sit amet, consectetur porttitor"};
  45. if(!second_mapped_array.flush()) {
  46. std::cerr << "Can't flush second area of \"" << argv[1] << "\" file" << std::endl;
  47. return -1;
  48. }
  49. std::cout << "Second area of \"" << argv[1] << "\" file is flushed" << std::endl;
  50. for(auto& data : second_mapped_array)
  51. std::cout << "| Recorded data: " << data.cstr << std::endl;
  52. std::cout << "File size: " << mapped_file.getFileSize() << " bytes" << std::endl;
  53. return 0;
  54. }