mapped_file_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #if defined(BOOST_INTERPROCESS_MAPPED_FILES)
  11. #include <boost/interprocess/detail/config_begin.hpp>
  12. #include <boost/interprocess/allocators/allocator.hpp>
  13. #include <boost/interprocess/containers/vector.hpp>
  14. #include <boost/interprocess/detail/file_wrapper.hpp>
  15. #include <boost/interprocess/file_mapping.hpp>
  16. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  17. #include "named_creation_template.hpp"
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <string>
  21. #include <boost/interprocess/detail/os_file_functions.hpp>
  22. #include "get_process_id_name.hpp"
  23. using namespace boost::interprocess;
  24. static const std::size_t FileSize = 1000;
  25. inline std::string get_filename()
  26. {
  27. std::string ret (ipcdetail::get_temporary_path());
  28. ret += "/";
  29. ret += test::get_process_id_name();
  30. return ret;
  31. }
  32. struct file_destroyer
  33. {
  34. ~file_destroyer()
  35. {
  36. //The last destructor will destroy the file
  37. file_mapping::remove(get_filename().c_str());
  38. }
  39. };
  40. //This wrapper is necessary to have a common constructor
  41. //in generic named_creation_template functions
  42. class mapped_file_creation_test_wrapper
  43. : public file_destroyer
  44. , public boost::interprocess::ipcdetail::managed_open_or_create_impl
  45. <boost::interprocess::ipcdetail::file_wrapper, 0, true, false>
  46. {
  47. typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
  48. <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
  49. public:
  50. mapped_file_creation_test_wrapper(boost::interprocess::create_only_t)
  51. : mapped_file(boost::interprocess::create_only, get_filename().c_str(), FileSize, read_write, 0, permissions())
  52. {}
  53. mapped_file_creation_test_wrapper(boost::interprocess::open_only_t)
  54. : mapped_file(boost::interprocess::open_only, get_filename().c_str(), read_write, 0)
  55. {}
  56. mapped_file_creation_test_wrapper(boost::interprocess::open_or_create_t)
  57. : mapped_file(boost::interprocess::open_or_create, get_filename().c_str(), FileSize, read_write, 0, permissions())
  58. {}
  59. };
  60. int main ()
  61. {
  62. typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
  63. <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
  64. file_mapping::remove(get_filename().c_str());
  65. test::test_named_creation<mapped_file_creation_test_wrapper>();
  66. //Create and get name, size and address
  67. {
  68. mapped_file file1(create_only, get_filename().c_str(), FileSize, read_write, 0, permissions());
  69. //Overwrite all memory
  70. std::memset(file1.get_user_address(), 0, file1.get_user_size());
  71. //Now test move semantics
  72. mapped_file move_ctor(boost::move(file1));
  73. mapped_file move_assign;
  74. move_assign = boost::move(move_ctor);
  75. }
  76. // file_mapping::remove(get_filename().c_str());
  77. return 0;
  78. }
  79. #include <boost/interprocess/detail/config_end.hpp>
  80. #else //#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
  81. int main()
  82. {
  83. return 0;
  84. }
  85. #endif//#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)