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/config.hpp>
12
13#ifndef BOOST_WINDOWS
14int main()
15{
16 return 0;
17}
18
19#else //BOOST_WINDOWS
20
21#define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
22//Force user-defined get_shared_dir
23#define BOOST_INTERPROCESS_SHARED_DIR_FUNC
24#include <boost/interprocess/detail/shared_dir_helpers.hpp>
25#include <string>
26
27#include "get_process_id_name.hpp"
28
29namespace boost {
30namespace interprocess {
31namespace ipcdetail {
32
33static bool dir_created = false;
34
35inline void get_shared_dir(std::string &shared_dir)
36{
37 shared_dir = boost::interprocess::ipcdetail::get_temporary_path();
38 shared_dir += "/boostipctesteventlog_";
39 shared_dir += boost::interprocess::test::get_process_id_name();
40 if(!dir_created)
41 ipcdetail::open_or_create_shared_directory(shared_dir.c_str());
42 dir_created = true;
43}
44
45inline void get_shared_dir(std::wstring &shared_dir)
46{
47 shared_dir = boost::interprocess::ipcdetail::get_temporary_wpath();
48 shared_dir += L"/boostipctesteventlog_";
49 shared_dir += boost::interprocess::test::get_process_id_wname();
50 if(!dir_created)
51 ipcdetail::open_or_create_shared_directory(shared_dir.c_str());
52 dir_created = true;
53}
54
55}}} //namespace boost::interprocess::ipcdetail
56
57#include <boost/interprocess/shared_memory_object.hpp>
58#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
59#include <boost/interprocess/exceptions.hpp>
60#include "named_creation_template.hpp"
61#include <cstring> //for strcmp, memset
62#include <iostream> //for cout
63#include <string>
64#include "get_process_id_name.hpp"
65
66using namespace boost::interprocess;
67
68static const std::size_t ShmSize = 1000;
69static const char * ShmName = test::get_process_id_name();
70static const wchar_t * ShmNameW = test::get_process_id_wname();
71
72struct eraser
73{
74 ~eraser()
75 {
76 shared_memory_object::remove(ShmName);
77 }
78};
79
80typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> shared_memory;
81
82//This wrapper is necessary to have a common constructor
83//in generic named_creation_template functions
84class shared_memory_creation_test_wrapper
85 : public eraser
86 , public shared_memory
87{
88
89 public:
90 shared_memory_creation_test_wrapper(create_only_t)
91 : shared_memory(create_only, ShmName, ShmSize, read_write, 0, permissions())
92 {}
93
94 shared_memory_creation_test_wrapper(open_only_t)
95 : shared_memory(open_only, ShmName, read_write, 0)
96 {}
97
98 shared_memory_creation_test_wrapper(open_or_create_t)
99 : shared_memory(open_or_create, ShmName, ShmSize, read_write, 0, permissions())
100 {}
101};
102
103//This wrapper is necessary to have a common constructor
104//in generic named_creation_template functions
105class shared_memory_creation_test_wrapper_w
106 : public eraser
107 , public shared_memory
108{
109
110 public:
111 shared_memory_creation_test_wrapper_w(create_only_t)
112 : shared_memory(create_only, ShmNameW, ShmSize, read_write, 0, permissions())
113 {}
114
115 shared_memory_creation_test_wrapper_w(open_only_t)
116 : shared_memory(open_only, ShmNameW, read_write, 0)
117 {}
118
119 shared_memory_creation_test_wrapper_w(open_or_create_t)
120 : shared_memory(open_or_create, ShmNameW, ShmSize, read_write, 0, permissions())
121 {}
122};
123
124
125int main ()
126{
127 int ret = 0;
128 BOOST_TRY{
129 shared_memory_object::remove(ShmName);
130 test::test_named_creation<shared_memory_creation_test_wrapper>();
131 #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
132 test::test_named_creation<shared_memory_creation_test_wrapper_w>();
133 #endif
134
135 //Create and get name, size and address
136 {
137 shared_memory_object::remove(ShmName);
138 shared_memory shm1(create_only, ShmName, ShmSize, read_write, 0, permissions());
139
140 //Overwrite all memory
141 std::memset(shm1.get_user_address(), 0, shm1.get_user_size());
142
143 //Now test move semantics
144 shared_memory move_ctor(boost::move(shm1));
145 shared_memory move_assign;
146 move_assign = boost::move(move_ctor);
147 }
148 }
149 BOOST_CATCH(std::exception &ex){
150 std::cout << ex.what() << std::endl;
151 ret = 1;
152 } BOOST_CATCH_END
153 shared_memory_object::remove(ShmName);
154 return ret;
155}
156
157#endif //BOOST_WINDOWS
158

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