fix doc example typo
[boost.git] / boost / interprocess / managed_mapped_file.hpp
blob065c424dad0bba382a27877e065c5254ca03e366
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008. 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 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
12 #define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
21 #include <boost/interprocess/detail/managed_memory_impl.hpp>
22 #include <boost/interprocess/creation_tags.hpp>
23 #include <boost/interprocess/detail/file_wrapper.hpp>
24 #include <boost/interprocess/detail/move.hpp>
25 #include <boost/interprocess/file_mapping.hpp>
27 namespace boost {
28 namespace interprocess {
30 //!A basic mapped file named object creation class. Initializes the
31 //!mapped file. Inherits all basic functionality from
32 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
33 template
35 class CharType,
36 class AllocationAlgorithm,
37 template<class IndexConfig> class IndexType
39 class basic_managed_mapped_file
40 : public detail::basic_managed_memory_impl
41 <CharType, AllocationAlgorithm, IndexType
42 ,detail::managed_open_or_create_impl<detail::file_wrapper>::ManagedOpenOrCreateUserOffset>
44 /// @cond
45 public:
46 typedef detail::basic_managed_memory_impl
47 <CharType, AllocationAlgorithm, IndexType,
48 detail::managed_open_or_create_impl<detail::file_wrapper>::ManagedOpenOrCreateUserOffset> base_t;
49 typedef detail::file_wrapper device_type;
50 basic_managed_mapped_file(basic_managed_mapped_file&);
51 basic_managed_mapped_file & operator=(basic_managed_mapped_file&);
53 private:
55 typedef detail::create_open_func<base_t> create_open_func_t;
56 typedef detail::managed_open_or_create_impl<detail::file_wrapper> managed_open_or_create_type;
58 basic_managed_mapped_file *get_this_pointer()
59 { return this; }
61 private:
62 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t;
63 /// @endcond
65 public: //functions
66 BOOST_INTERPROCESS_ENABLE_MOVE_EMULATION(basic_managed_mapped_file)
68 //!Creates mapped file and creates and places the segment manager.
69 //!This can throw.
70 basic_managed_mapped_file()
73 //!Creates mapped file and creates and places the segment manager.
74 //!This can throw.
75 basic_managed_mapped_file(create_only_t create_only, const char *name,
76 std::size_t size, const void *addr = 0)
77 : m_mfile(create_only, name, size, read_write, addr,
78 create_open_func_t(get_this_pointer(), detail::DoCreate))
81 //!Creates mapped file and creates and places the segment manager if
82 //!segment was not created. If segment was created it connects to the
83 //!segment.
84 //!This can throw.
85 basic_managed_mapped_file (open_or_create_t open_or_create,
86 const char *name, std::size_t size,
87 const void *addr = 0)
88 : m_mfile(open_or_create, name, size, read_write, addr,
89 create_open_func_t(get_this_pointer(),
90 detail::DoOpenOrCreate))
93 //!Connects to a created mapped file and its segment manager.
94 //!This can throw.
95 basic_managed_mapped_file (open_only_t open_only, const char* name,
96 const void *addr = 0)
97 : m_mfile(open_only, name, read_write, addr,
98 create_open_func_t(get_this_pointer(),
99 detail::DoOpen))
102 //!Connects to a created mapped file and its segment manager
103 //!in copy_on_write mode.
104 //!This can throw.
105 basic_managed_mapped_file (open_copy_on_write_t, const char* name,
106 const void *addr = 0)
107 : m_mfile(open_only, name, copy_on_write, addr,
108 create_open_func_t(get_this_pointer(),
109 detail::DoOpen))
112 //!Connects to a created mapped file and its segment manager
113 //!in read-only mode.
114 //!This can throw.
115 basic_managed_mapped_file (open_read_only_t, const char* name,
116 const void *addr = 0)
117 : m_mfile(open_only, name, read_only, addr,
118 create_open_func_t(get_this_pointer(),
119 detail::DoOpen))
122 //!Moves the ownership of "moved"'s managed memory to *this.
123 //!Does not throw
124 basic_managed_mapped_file(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file) moved)
126 this->swap(moved);
129 //!Moves the ownership of "moved"'s managed memory to *this.
130 //!Does not throw
131 basic_managed_mapped_file &operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file) moved)
133 basic_managed_mapped_file tmp(boost::interprocess::move(moved));
134 this->swap(tmp);
135 return *this;
138 //!Destroys *this and indicates that the calling process is finished using
139 //!the resource. The destructor function will deallocate
140 //!any system resources allocated by the system for use by this process for
141 //!this resource. The resource can still be opened again calling
142 //!the open constructor overload. To erase the resource from the system
143 //!use remove().
144 ~basic_managed_mapped_file()
147 //!Swaps the ownership of the managed mapped memories managed by *this and other.
148 //!Never throws.
149 void swap(basic_managed_mapped_file &other)
151 base_t::swap(other);
152 m_mfile.swap(other.m_mfile);
155 //!Flushes cached data to file.
156 //!Never throws
157 bool flush()
158 { return m_mfile.flush(); }
160 //!Tries to resize mapped file so that we have room for
161 //!more objects.
163 //!This function is not synchronized so no other thread or process should
164 //!be reading or writing the file
165 static bool grow(const char *filename, std::size_t extra_bytes)
167 return base_t::template grow
168 <basic_managed_mapped_file>(filename, extra_bytes);
171 //!Tries to resize mapped file to minimized the size of the file.
173 //!This function is not synchronized so no other thread or process should
174 //!be reading or writing the file
175 static bool shrink_to_fit(const char *filename)
177 return base_t::template shrink_to_fit
178 <basic_managed_mapped_file>(filename);
181 /// @cond
183 //!Tries to find a previous named allocation address. Returns a memory
184 //!buffer and the object count. If not found returned pointer is 0.
185 //!Never throws.
186 template <class T>
187 std::pair<T*, std::size_t> find (char_ptr_holder_t name)
189 if(m_mfile.get_mapped_region().get_mode() == read_only){
190 return base_t::template find_no_lock<T>(name);
192 else{
193 return base_t::template find<T>(name);
197 private:
198 managed_open_or_create_type m_mfile;
199 /// @endcond
202 } //namespace interprocess {
203 } //namespace boost {
205 #include <boost/interprocess/detail/config_end.hpp>
207 #endif //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP