Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / interprocess / example / doc_managed_copy_on_write.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-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/config_begin.hpp>
11 #include <boost/interprocess/detail/os_file_functions.hpp>
12 //[doc_managed_copy_on_write
13 #include <boost/interprocess/managed_mapped_file.hpp>
14 #include <fstream> //std::fstream
15 #include <iterator>//std::distance
16
17 //<-
18 #include "../test/get_process_id_name.hpp"
19 //->
20
21 int main()
22 {
23    using namespace boost::interprocess;
24
25    //Define file names
26    //<-
27    #if 1
28    const char *ManagedFile  = 0;
29    const char *ManagedFile2 = 0;
30    std::string managed_file_name(boost::interprocess::ipcdetail::get_temporary_path());
31    managed_file_name += "/"; managed_file_name += test::get_process_id_name();
32    ManagedFile = managed_file_name.c_str();
33    std::string managed_file2_name(boost::interprocess::ipcdetail::get_temporary_path());
34    managed_file2_name += "/"; managed_file2_name += test::get_process_id_name();  managed_file2_name += "_2";
35    ManagedFile2 = managed_file2_name.c_str();
36    #else
37    //->
38    const char *ManagedFile  = "MyManagedFile";
39    const char *ManagedFile2 = "MyManagedFile2";
40    //<-
41    #endif
42    //->
43
44    //Try to erase any previous managed segment with the same name
45    file_mapping::remove(ManagedFile);
46    file_mapping::remove(ManagedFile2);
47    remove_file_on_destroy destroyer1(ManagedFile);
48    remove_file_on_destroy destroyer2(ManagedFile2);
49
50    {
51       //Create an named integer in a managed mapped file
52       managed_mapped_file managed_file(create_only, ManagedFile, 65536);
53       managed_file.construct<int>("MyInt")(0u);
54
55       //Now create a copy on write version
56       managed_mapped_file managed_file_cow(open_copy_on_write, ManagedFile);
57
58       //Erase the int and create a new one
59       if(!managed_file_cow.destroy<int>("MyInt"))
60          throw int(0);
61       managed_file_cow.construct<int>("MyInt2");
62
63       //Check changes
64       if(managed_file_cow.find<int>("MyInt").first && !managed_file_cow.find<int>("MyInt2").first)
65          throw int(0);
66
67       //Check the original is intact
68       if(!managed_file.find<int>("MyInt").first && managed_file.find<int>("MyInt2").first)
69          throw int(0);
70
71       {  //Dump the modified copy on write segment to a file
72          std::fstream file(ManagedFile2, std::ios_base::out | std::ios_base::binary);
73          if(!file)
74             throw int(0);
75        file.write(static_cast<const char *>(managed_file_cow.get_address()), (std::streamsize)managed_file_cow.get_size());
76       }
77
78       //Now open the modified file and test changes
79       managed_mapped_file managed_file_cow2(open_only, ManagedFile2);
80       if(managed_file_cow2.find<int>("MyInt").first && !managed_file_cow2.find<int>("MyInt2").first)
81          throw int(0);
82    }
83    {
84       //Now create a read-only version
85       managed_mapped_file managed_file_ro(open_read_only, ManagedFile);
86
87       //Check the original is intact
88       if(!managed_file_ro.find<int>("MyInt").first && managed_file_ro.find<int>("MyInt2").first)
89          throw int(0);
90
91       //Check the number of named objects using the iterators
92       if(std::distance(managed_file_ro.named_begin(),  managed_file_ro.named_end())  != 1 &&
93          std::distance(managed_file_ro.unique_begin(), managed_file_ro.unique_end()) != 0 )
94          throw int(0);
95    }
96    return 0;
97 }
98 //]
99 #include <boost/interprocess/detail/config_end.hpp>