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#include <boost/interprocess/detail/workaround.hpp>
11
12#if defined(BOOST_INTERPROCESS_MAPPED_FILES)
13
14#include <boost/interprocess/allocators/allocator.hpp>
15#include <boost/interprocess/containers/vector.hpp>
16#include <boost/interprocess/detail/file_wrapper.hpp>
17#include <boost/interprocess/file_mapping.hpp>
18#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
19#include "named_creation_template.hpp"
20#include <cstdio>
21#include <cstring>
22#include <string>
23#include <boost/interprocess/detail/os_file_functions.hpp>
24#include "get_process_id_name.hpp"
25
26using namespace boost::interprocess;
27
28static const std::size_t FileSize = 1000;
29
30struct file_destroyer
31{
32 ~file_destroyer()
33 {
34 //The last destructor will destroy the file
35 file_mapping::remove(filename: get_filename().c_str());
36 }
37};
38
39typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
40 <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
41
42//This wrapper is necessary to have a common constructor
43//in generic named_creation_template functions
44class mapped_file_creation_test_wrapper
45 : public file_destroyer
46 , public mapped_file
47{
48 public:
49 mapped_file_creation_test_wrapper(boost::interprocess::create_only_t)
50 : mapped_file(boost::interprocess::create_only, get_filename().c_str(), FileSize, read_write, 0, permissions())
51 {}
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
57 mapped_file_creation_test_wrapper(boost::interprocess::open_or_create_t)
58 : mapped_file(boost::interprocess::open_or_create, get_filename().c_str(), FileSize, read_write, 0, permissions())
59 {}
60};
61
62#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
63
64class mapped_file_creation_test_wrapper_w
65 : public file_destroyer
66 , public mapped_file
67{
68 public:
69 mapped_file_creation_test_wrapper_w(boost::interprocess::create_only_t)
70 : mapped_file(boost::interprocess::create_only, get_wfilename().c_str(), FileSize, read_write, 0, permissions())
71 {}
72
73 mapped_file_creation_test_wrapper_w(boost::interprocess::open_only_t)
74 : mapped_file(boost::interprocess::open_only, get_wfilename().c_str(), read_write, 0)
75 {}
76
77 mapped_file_creation_test_wrapper_w(boost::interprocess::open_or_create_t)
78 : mapped_file(boost::interprocess::open_or_create, get_wfilename().c_str(), FileSize, read_write, 0, permissions())
79 {}
80};
81
82#endif //BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
83
84int main ()
85{
86 file_mapping::remove(filename: get_filename().c_str());
87 test::test_named_creation<mapped_file_creation_test_wrapper>();
88 #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
89 test::test_named_creation<mapped_file_creation_test_wrapper_w>();
90 #endif
91
92 //Create and get name, size and address
93 {
94 mapped_file file1(create_only, get_filename().c_str(), FileSize, read_write, 0, permissions());
95
96 //Overwrite all memory
97 std::memset(s: file1.get_user_address(), c: 0, n: file1.get_user_size());
98
99 //Now test move semantics
100 mapped_file move_ctor(boost::move(t&: file1));
101 mapped_file move_assign;
102 move_assign = boost::move(t&: move_ctor);
103 }
104// file_mapping::remove(get_filename().c_str());
105 return 0;
106}
107
108#else //#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
109
110int main()
111{
112 return 0;
113}
114
115#endif//#if !defined(BOOST_INTERPROCESS_MAPPED_FILES)
116

source code of boost/libs/interprocess/test/mapped_file_test.cpp