Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / interprocess / test / mapped_file_test.cpp
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
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
24 using namespace boost::interprocess;
25
26 static const std::size_t FileSize = 1000;
27 inline std::string get_filename()
28 {
29    std::string ret (ipcdetail::get_temporary_path());
30    ret += "/";
31    ret += test::get_process_id_name();
32    return ret;
33 }
34
35 struct file_destroyer
36 {
37    ~file_destroyer()
38    {
39       //The last destructor will destroy the file
40       file_mapping::remove(get_filename().c_str());
41    }
42 };
43
44 //This wrapper is necessary to have a common constructor
45 //in generic named_creation_template functions
46 class mapped_file_creation_test_wrapper
47    : public file_destroyer
48    , public boost::interprocess::ipcdetail::managed_open_or_create_impl
49       <boost::interprocess::ipcdetail::file_wrapper, 0, true, false>
50 {
51    typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
52       <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
53    public:
54    mapped_file_creation_test_wrapper(boost::interprocess::create_only_t)
55       :  mapped_file(boost::interprocess::create_only, get_filename().c_str(), FileSize, read_write, 0, permissions())
56    {}
57
58    mapped_file_creation_test_wrapper(boost::interprocess::open_only_t)
59       :  mapped_file(boost::interprocess::open_only, get_filename().c_str(), read_write, 0)
60    {}
61
62    mapped_file_creation_test_wrapper(boost::interprocess::open_or_create_t)
63       :  mapped_file(boost::interprocess::open_or_create, get_filename().c_str(), FileSize, read_write, 0, permissions())
64    {}
65 };
66
67 int main ()
68 {
69    typedef boost::interprocess::ipcdetail::managed_open_or_create_impl
70       <boost::interprocess::ipcdetail::file_wrapper, 0, true, false> mapped_file;
71    file_mapping::remove(get_filename().c_str());
72    test::test_named_creation<mapped_file_creation_test_wrapper>();
73
74    //Create and get name, size and address
75    {
76       mapped_file file1(create_only, get_filename().c_str(), FileSize, read_write, 0, permissions());
77
78       //Overwrite all memory
79       std::memset(file1.get_user_address(), 0, file1.get_user_size());
80
81       //Now test move semantics
82       mapped_file move_ctor(boost::move(file1));
83       mapped_file move_assign;
84       move_assign = boost::move(move_ctor);
85    }
86 //   file_mapping::remove(get_filename().c_str());
87    return 0;
88 }
89
90 #include <boost/interprocess/detail/config_end.hpp>