| 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 | #ifndef BOOST_INTERPROCESS_TEST_NAMED_RESOURCE_TEMPLATE_HEADER |
| 12 | #define |
| 13 | |
| 14 | #include <boost/interprocess/detail/config_begin.hpp> |
| 15 | #include <boost/interprocess/exceptions.hpp> |
| 16 | #include "boost_interprocess_check.hpp" |
| 17 | #include "get_process_id_name.hpp" |
| 18 | #include <iostream> |
| 19 | #include <typeinfo> |
| 20 | #include <boost/interprocess/creation_tags.hpp> |
| 21 | |
| 22 | namespace boost { namespace interprocess { namespace test { |
| 23 | |
| 24 | template <class NamedResource> |
| 25 | inline void create_then_open_then_open_or_create() |
| 26 | { |
| 27 | BOOST_TRY{ |
| 28 | //Create it and open it twice |
| 29 | NamedResource nresource1(create_only); |
| 30 | NamedResource nresource2(open_only); |
| 31 | NamedResource nresource3(open_or_create); |
| 32 | } |
| 33 | BOOST_CATCH(...){ |
| 34 | //This shouldn't throw so show the error |
| 35 | BOOST_INTERPROCESS_CHECK( false ); |
| 36 | } BOOST_CATCH_END |
| 37 | } |
| 38 | |
| 39 | template <class NamedResource> |
| 40 | inline void open_or_create_then_create() |
| 41 | { |
| 42 | //Create it with open_or_create and try to create it twice |
| 43 | NamedResource nresource1(open_or_create); |
| 44 | BOOST_TRY{ |
| 45 | NamedResource nresource2(create_only); |
| 46 | } |
| 47 | BOOST_CATCH(interprocess_exception &err){ |
| 48 | BOOST_INTERPROCESS_CHECK(err.get_error_code() == already_exists_error); |
| 49 | } BOOST_CATCH_END |
| 50 | } |
| 51 | |
| 52 | template <class NamedResource> |
| 53 | inline void dont_create_and_open() |
| 54 | { |
| 55 | //Try to open it without creating |
| 56 | BOOST_TRY{ |
| 57 | NamedResource nresource1(open_only); |
| 58 | } |
| 59 | BOOST_CATCH(interprocess_exception &err){ |
| 60 | BOOST_INTERPROCESS_CHECK(err.get_error_code() == not_found_error); |
| 61 | return; |
| 62 | } BOOST_CATCH_END |
| 63 | //The mutex should not exist |
| 64 | BOOST_INTERPROCESS_CHECK(false); |
| 65 | } |
| 66 | |
| 67 | template <class NamedResource> |
| 68 | inline void test_named_creation() |
| 69 | { |
| 70 | std::cout << "create_then_open_then_open_or_create<" |
| 71 | << typeid(NamedResource).name() << ">" << std::endl; |
| 72 | create_then_open_then_open_or_create<NamedResource>(); |
| 73 | std::cout << "open_or_create_then_create<" |
| 74 | << typeid(NamedResource).name() << ">" << std::endl; |
| 75 | open_or_create_then_create<NamedResource>(); |
| 76 | std::cout << "dont_create_and_open<" |
| 77 | << typeid(NamedResource).name() << ">" << std::endl; |
| 78 | dont_create_and_open<NamedResource>(); |
| 79 | } |
| 80 | |
| 81 | template<class NamedSync> |
| 82 | class named_sync_wrapper |
| 83 | : public NamedSync |
| 84 | { |
| 85 | public: |
| 86 | named_sync_wrapper() |
| 87 | : NamedSync(open_or_create, test::get_process_id_ptr_name(this)) |
| 88 | {} |
| 89 | |
| 90 | ~named_sync_wrapper() |
| 91 | { |
| 92 | NamedSync::remove(test::get_process_id_ptr_name(this)); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template<class NamedSync> |
| 97 | struct named_sync_deleter |
| 98 | { |
| 99 | ~named_sync_deleter() |
| 100 | { NamedSync::remove(test::get_process_id_name()); } |
| 101 | }; |
| 102 | |
| 103 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) |
| 104 | |
| 105 | template<class NamedSync> |
| 106 | struct named_sync_deleter_w |
| 107 | { |
| 108 | ~named_sync_deleter_w() |
| 109 | { NamedSync::remove(test::get_process_id_wname()); } |
| 110 | }; |
| 111 | |
| 112 | #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) |
| 113 | |
| 114 | |
| 115 | //This wrapper is necessary to have a common constructor |
| 116 | //in generic named_creation_template functions |
| 117 | template<class NamedSync> |
| 118 | class named_sync_creation_test_wrapper |
| 119 | : public test::named_sync_deleter<NamedSync>, public NamedSync |
| 120 | { |
| 121 | public: |
| 122 | named_sync_creation_test_wrapper(create_only_t) |
| 123 | : NamedSync(create_only, test::get_process_id_name()) |
| 124 | {} |
| 125 | |
| 126 | named_sync_creation_test_wrapper(open_only_t) |
| 127 | : NamedSync(open_only, test::get_process_id_name()) |
| 128 | {} |
| 129 | |
| 130 | named_sync_creation_test_wrapper(open_or_create_t) |
| 131 | : NamedSync(open_or_create, test::get_process_id_name()) |
| 132 | {} |
| 133 | |
| 134 | ~named_sync_creation_test_wrapper() |
| 135 | {} |
| 136 | }; |
| 137 | |
| 138 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) |
| 139 | |
| 140 | //This wrapper is necessary to have a common constructor |
| 141 | //in generic named_creation_template functions |
| 142 | template<class NamedSync> |
| 143 | class named_sync_creation_test_wrapper_w |
| 144 | : public test::named_sync_deleter_w<NamedSync>, public NamedSync |
| 145 | { |
| 146 | public: |
| 147 | named_sync_creation_test_wrapper_w(create_only_t) |
| 148 | : NamedSync(create_only, test::get_process_id_wname()) |
| 149 | {} |
| 150 | |
| 151 | named_sync_creation_test_wrapper_w(open_only_t) |
| 152 | : NamedSync(open_only, test::get_process_id_wname()) |
| 153 | {} |
| 154 | |
| 155 | named_sync_creation_test_wrapper_w(open_or_create_t) |
| 156 | : NamedSync(open_or_create, test::get_process_id_wname()) |
| 157 | {} |
| 158 | |
| 159 | ~named_sync_creation_test_wrapper_w() |
| 160 | {} |
| 161 | }; |
| 162 | |
| 163 | #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) |
| 164 | |
| 165 | }}} //namespace boost { namespace interprocess { namespace test { |
| 166 | |
| 167 | #include <boost/interprocess/detail/config_end.hpp> |
| 168 | |
| 169 | #endif //BOOST_INTERPROCESS_TEST_NAMED_RESOURCE_TEMPLATE_HEADER |
| 170 | |