| 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Peter Dimov 2002-2005, 2007. |
| 4 | // (C) Copyright Ion Gaztanaga 2006-2012. |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // See http://www.boost.org/libs/interprocess for documentation. |
| 10 | // |
| 11 | ////////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #include <boost/interprocess/offset_ptr.hpp> |
| 14 | #include <boost/interprocess/smart_ptr/shared_ptr.hpp> |
| 15 | #include <boost/interprocess/smart_ptr/weak_ptr.hpp> |
| 16 | #include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp> |
| 17 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 18 | #include <boost/interprocess/allocators/allocator.hpp> |
| 19 | #include <boost/interprocess/containers/string.hpp> |
| 20 | #include <boost/interprocess/containers/vector.hpp> |
| 21 | #include <boost/interprocess/smart_ptr/deleter.hpp> |
| 22 | #include <boost/interprocess/smart_ptr/scoped_ptr.hpp> |
| 23 | #include <boost/core/lightweight_test.hpp> |
| 24 | #include <string> |
| 25 | #include "get_process_id_name.hpp" |
| 26 | |
| 27 | |
| 28 | #include <boost/interprocess/sync/upgradable_lock.hpp> |
| 29 | #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp> |
| 30 | |
| 31 | using namespace boost::interprocess; |
| 32 | |
| 33 | class base_class |
| 34 | { |
| 35 | public: |
| 36 | virtual ~base_class() |
| 37 | {} |
| 38 | }; |
| 39 | |
| 40 | class derived_class |
| 41 | : public base_class |
| 42 | { |
| 43 | public: |
| 44 | virtual ~derived_class() BOOST_OVERRIDE |
| 45 | {} |
| 46 | }; |
| 47 | |
| 48 | int simple_test() |
| 49 | { |
| 50 | typedef managed_shared_memory::segment_manager segment_mngr_t; |
| 51 | typedef allocator<base_class, segment_mngr_t> base_class_allocator; |
| 52 | typedef deleter<base_class, segment_mngr_t> base_deleter_t; |
| 53 | typedef shared_ptr<base_class, base_class_allocator, base_deleter_t> base_shared_ptr; |
| 54 | |
| 55 | std::string process_name; |
| 56 | test::get_process_id_name(str&: process_name); |
| 57 | |
| 58 | shared_memory_object::remove(filename: process_name.c_str()); |
| 59 | { |
| 60 | managed_shared_memory shmem(create_only, process_name.c_str(), 10000); |
| 61 | |
| 62 | { |
| 63 | base_shared_ptr s_ptr(base_shared_ptr::pointer(0), |
| 64 | base_class_allocator(shmem.get_segment_manager()), |
| 65 | base_deleter_t(shmem.get_segment_manager())); |
| 66 | |
| 67 | base_shared_ptr s_ptr2(shmem.construct<base_class>(name: "base_class" )(), |
| 68 | base_class_allocator(shmem.get_segment_manager()), |
| 69 | base_deleter_t(shmem.get_segment_manager())); |
| 70 | |
| 71 | base_shared_ptr s_ptr3(offset_ptr<derived_class>(shmem.construct<derived_class>(name: "derived_class" )()), |
| 72 | base_class_allocator(shmem.get_segment_manager()), |
| 73 | base_deleter_t(shmem.get_segment_manager())); |
| 74 | |
| 75 | if(s_ptr3.get_deleter() == 0){ |
| 76 | return 1; |
| 77 | } |
| 78 | //if(s_ptr3.get_allocator() == 0){ |
| 79 | //return 1; |
| 80 | //} |
| 81 | |
| 82 | base_shared_ptr s_ptr_empty; |
| 83 | |
| 84 | if(s_ptr_empty.get_deleter() != 0){ |
| 85 | return 1; |
| 86 | } |
| 87 | //if(s_ptr_empty.get_allocator() != 0){ |
| 88 | //return 1; |
| 89 | //} |
| 90 | } |
| 91 | } |
| 92 | shared_memory_object::remove(filename: process_name.c_str()); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int string_shared_ptr_vector_insertion_test() |
| 97 | { |
| 98 | typedef managed_shared_memory::segment_manager segment_mngr_t; |
| 99 | |
| 100 | //Allocator of chars |
| 101 | typedef allocator<char, segment_mngr_t> char_allocator_t; |
| 102 | |
| 103 | //A shared memory string class |
| 104 | typedef basic_string<char, std::char_traits<char>, char_allocator_t> string_t; |
| 105 | |
| 106 | //A shared memory string allocator |
| 107 | typedef allocator<string_t, segment_mngr_t> string_allocator_t; |
| 108 | |
| 109 | //A deleter for shared_ptr<> that erases a shared memory string |
| 110 | typedef deleter<string_t, segment_mngr_t> string_deleter_t; |
| 111 | |
| 112 | //A shared pointer that points to a shared memory string and its instantiation |
| 113 | typedef shared_ptr<string_t, string_allocator_t, string_deleter_t> string_shared_ptr_t; |
| 114 | |
| 115 | //An allocator for shared pointers to a string in shared memory |
| 116 | typedef allocator<string_shared_ptr_t, segment_mngr_t> string_shared_ptr_allocator_t; |
| 117 | |
| 118 | //A weak pointer that points to a shared memory string and its instantiation |
| 119 | typedef weak_ptr<string_t, string_allocator_t, string_deleter_t> string_weak_ptr_t; |
| 120 | |
| 121 | //An allocator for weak pointers to a string in shared memory |
| 122 | typedef allocator<string_weak_ptr_t, segment_mngr_t > string_weak_ptr_allocator_t; |
| 123 | |
| 124 | //A vector of shared pointers to strings (all in shared memory) and its instantiation |
| 125 | typedef vector<string_shared_ptr_t, string_shared_ptr_allocator_t> |
| 126 | string_shared_ptr_vector_t; |
| 127 | |
| 128 | //A vector of weak pointers to strings (all in shared memory) and its instantiation |
| 129 | typedef vector<string_weak_ptr_t, string_weak_ptr_allocator_t> |
| 130 | string_weak_ptr_vector_t; |
| 131 | |
| 132 | std::string process_name; |
| 133 | test::get_process_id_name(str&: process_name); |
| 134 | |
| 135 | //A shared memory managed memory classes |
| 136 | shared_memory_object::remove(filename: process_name.c_str()); |
| 137 | { |
| 138 | managed_shared_memory shmem(create_only, process_name.c_str(), 20000); |
| 139 | |
| 140 | { |
| 141 | const int NumElements = 100; |
| 142 | //Construct the allocator of strings |
| 143 | string_allocator_t string_allocator(shmem.get_segment_manager()); |
| 144 | //Construct the allocator of a shared_ptr to string |
| 145 | string_shared_ptr_allocator_t string_shared_ptr_allocator(shmem.get_segment_manager()); |
| 146 | //Construct the allocator of a shared_ptr to string |
| 147 | string_weak_ptr_allocator_t string_weak_ptr_allocator(shmem.get_segment_manager()); |
| 148 | //This is a string deleter using destroy_ptr() function of the managed_shared_memory |
| 149 | string_deleter_t deleter(shmem.get_segment_manager()); |
| 150 | //Create a string in shared memory, to avoid leaks with exceptions use |
| 151 | //scoped ptr until we store this pointer in the shared ptr |
| 152 | scoped_ptr<string_t, string_deleter_t> scoped_string |
| 153 | (shmem.construct<string_t>(name: anonymous_instance)(string_allocator), deleter); |
| 154 | //Now construct a shared pointer to a string |
| 155 | string_shared_ptr_t string_shared_ptr (scoped_string.get(), |
| 156 | string_shared_ptr_allocator, |
| 157 | deleter); |
| 158 | //Check use count is just one |
| 159 | if(!string_shared_ptr.unique()){ |
| 160 | return 1; |
| 161 | } |
| 162 | //We don't need the scoped_ptr anonymous since the raw pointer is in the shared ptr |
| 163 | scoped_string.release(); |
| 164 | //Now fill a shared memory vector of shared_ptrs to a string |
| 165 | string_shared_ptr_vector_t my_sharedptr_vector(string_shared_ptr_allocator); |
| 166 | my_sharedptr_vector.insert(p: my_sharedptr_vector.begin(), n: NumElements, x: string_shared_ptr); |
| 167 | //Insert in the middle to test movability |
| 168 | my_sharedptr_vector.insert(p: my_sharedptr_vector.nth(n: my_sharedptr_vector.size()/2u), n: NumElements, x: string_shared_ptr); |
| 169 | //Now check the shared count is the objects contained in the |
| 170 | //vector plus string_shared_ptr |
| 171 | if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ |
| 172 | return 1; |
| 173 | } |
| 174 | //Now create a weak ptr from the shared_ptr |
| 175 | string_weak_ptr_t string_weak_ptr (string_shared_ptr); |
| 176 | //Use count should remain the same |
| 177 | if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ |
| 178 | return 1; |
| 179 | } |
| 180 | //Now reset the local shared_ptr and check use count |
| 181 | string_shared_ptr.reset(); |
| 182 | if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size())){ |
| 183 | return 1; |
| 184 | } |
| 185 | //Now reset the local shared_ptr's use count should be zero |
| 186 | if(string_shared_ptr.use_count() != 0){ |
| 187 | return 1; |
| 188 | } |
| 189 | //Now recreate the shared ptr from the weak ptr |
| 190 | //and recheck use count |
| 191 | string_shared_ptr = string_shared_ptr_t(string_weak_ptr); |
| 192 | if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ |
| 193 | return 1; |
| 194 | } |
| 195 | //Now fill a vector of weak_ptr-s |
| 196 | string_weak_ptr_vector_t my_weakptr_vector(string_weak_ptr_allocator); |
| 197 | my_weakptr_vector.insert(p: my_weakptr_vector.begin(), n: NumElements, x: string_weak_ptr); |
| 198 | //The shared count should remain the same |
| 199 | if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ |
| 200 | return 1; |
| 201 | } |
| 202 | //So weak pointers should be fine |
| 203 | string_weak_ptr_vector_t::iterator beg = my_weakptr_vector.begin(), |
| 204 | end = my_weakptr_vector.end(); |
| 205 | for(;beg != end; ++beg){ |
| 206 | if(beg->expired()){ |
| 207 | return 1; |
| 208 | } |
| 209 | //The shared pointer constructed from weak ptr should |
| 210 | //be the same as the original, since all weak pointer |
| 211 | //point the the same object |
| 212 | if(string_shared_ptr_t(*beg) != string_shared_ptr){ |
| 213 | return 1; |
| 214 | } |
| 215 | } |
| 216 | //Now destroy all the shared ptr-s of the shared ptr vector |
| 217 | my_sharedptr_vector.clear(); |
| 218 | //The only alive shared ptr should be the local one |
| 219 | if(string_shared_ptr.use_count() != 1){ |
| 220 | return 1; |
| 221 | } |
| 222 | //Now we invalidate the last alive shared_ptr |
| 223 | string_shared_ptr.reset(); |
| 224 | //Now all weak pointers should have expired |
| 225 | beg = my_weakptr_vector.begin(); |
| 226 | end = my_weakptr_vector.end(); |
| 227 | for(;beg != end; ++beg){ |
| 228 | if(!beg->expired()){ |
| 229 | return 1; |
| 230 | } |
| 231 | bool success = false; |
| 232 | //Now this should throw |
| 233 | BOOST_TRY{ |
| 234 | string_shared_ptr_t dummy(*beg); |
| 235 | //We should never reach here |
| 236 | return 1; |
| 237 | } |
| 238 | BOOST_CATCH(const boost::interprocess::bad_weak_ptr &){ |
| 239 | success = true; |
| 240 | } BOOST_CATCH_END |
| 241 | if(!success){ |
| 242 | return 1; |
| 243 | } |
| 244 | } |
| 245 | //Clear weak ptr vector |
| 246 | my_weakptr_vector.clear(); |
| 247 | //Now lock returned shared ptr should return null |
| 248 | if(string_weak_ptr.lock().get()){ |
| 249 | return 1; |
| 250 | } |
| 251 | //Reset weak_ptr |
| 252 | string_weak_ptr.reset(); |
| 253 | } |
| 254 | } |
| 255 | shared_memory_object::remove(filename: process_name.c_str()); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | // |
| 260 | // This part is taken from shared_ptr_basic_test.cpp |
| 261 | // |
| 262 | // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. |
| 263 | // Copyright (c) 2006 Ion Gaztanaga |
| 264 | // |
| 265 | // Distributed under the Boost Software License, Version 1.0. (See |
| 266 | // accompanying file LICENSE_1_0.txt or copy at |
| 267 | // http://www.boost.org/LICENSE_1_0.txt) |
| 268 | // |
| 269 | |
| 270 | static int cnt = 0; |
| 271 | |
| 272 | struct X |
| 273 | { |
| 274 | X(){ ++cnt; } |
| 275 | // virtual destructor deliberately omitted |
| 276 | virtual ~X(){ --cnt; } |
| 277 | |
| 278 | virtual int id() const |
| 279 | { return 1; } |
| 280 | |
| 281 | private: |
| 282 | X(X const &); |
| 283 | X & operator= (X const &); |
| 284 | }; |
| 285 | |
| 286 | struct Y: public X |
| 287 | { |
| 288 | Y(){ ++cnt; } |
| 289 | virtual ~Y() BOOST_OVERRIDE{ --cnt; } |
| 290 | |
| 291 | virtual int id() const BOOST_OVERRIDE |
| 292 | { return 2; } |
| 293 | |
| 294 | private: |
| 295 | Y(Y const &); |
| 296 | Y & operator= (Y const &); |
| 297 | }; |
| 298 | |
| 299 | int * get_object() |
| 300 | { ++cnt; return &cnt; } |
| 301 | |
| 302 | void release_object(int * p) |
| 303 | { BOOST_TEST(p == &cnt); --cnt; } |
| 304 | |
| 305 | template<class T, class A, class D> |
| 306 | void test_is_X(shared_ptr<T, A, D> const & p) |
| 307 | { |
| 308 | BOOST_TEST(p->id() == 1); |
| 309 | BOOST_TEST((*p).id() == 1); |
| 310 | } |
| 311 | |
| 312 | template<class T, class A, class D> |
| 313 | void test_is_X(weak_ptr<T, A, D> const & p) |
| 314 | { |
| 315 | BOOST_TEST(p.get() != 0); |
| 316 | BOOST_TEST(p.get()->id() == 1); |
| 317 | } |
| 318 | |
| 319 | template<class T, class A, class D> |
| 320 | void test_is_Y(shared_ptr<T, A, D> const & p) |
| 321 | { |
| 322 | BOOST_TEST(p->id() == 2); |
| 323 | BOOST_TEST((*p).id() == 2); |
| 324 | } |
| 325 | |
| 326 | template<class T, class A, class D> |
| 327 | void test_is_Y(weak_ptr<T, A, D> const & p) |
| 328 | { |
| 329 | shared_ptr<T, A, D> q = p.lock(); |
| 330 | BOOST_TEST(q.get() != 0); |
| 331 | BOOST_TEST(q->id() == 2); |
| 332 | } |
| 333 | |
| 334 | template<class T, class T2> |
| 335 | void test_eq(T const & a, T2 const & b) |
| 336 | { |
| 337 | BOOST_TEST(a == b); |
| 338 | BOOST_TEST(!(a != b)); |
| 339 | BOOST_TEST(!(a < b)); |
| 340 | BOOST_TEST(!(b < a)); |
| 341 | } |
| 342 | |
| 343 | template<class T, class T2> |
| 344 | void test_ne(T const & a, T2 const & b) |
| 345 | { |
| 346 | BOOST_TEST(!(a == b)); |
| 347 | BOOST_TEST(a != b); |
| 348 | BOOST_TEST(a < b || b < a); |
| 349 | BOOST_TEST(!(a < b && b < a)); |
| 350 | } |
| 351 | |
| 352 | template<class T, class U, class A, class D, class D2> |
| 353 | void test_shared(weak_ptr<T, A, D> const & a, weak_ptr<U, A, D2> const & b) |
| 354 | { |
| 355 | BOOST_TEST(!(a < b)); |
| 356 | BOOST_TEST(!(b < a)); |
| 357 | } |
| 358 | |
| 359 | template<class T, class U, class A, class D, class D2> |
| 360 | void test_nonshared(weak_ptr<T, A, D> const & a, weak_ptr<U, A, D2> const & b) |
| 361 | { |
| 362 | BOOST_TEST(a < b || b < a); |
| 363 | BOOST_TEST(!(a < b && b < a)); |
| 364 | } |
| 365 | |
| 366 | template<class T, class U> |
| 367 | void test_eq2(T const & a, U const & b) |
| 368 | { |
| 369 | BOOST_TEST(a == b); |
| 370 | BOOST_TEST(!(a != b)); |
| 371 | } |
| 372 | |
| 373 | template<class T, class U> |
| 374 | void test_ne2(T const & a, U const & b) |
| 375 | { |
| 376 | BOOST_TEST(!(a == b)); |
| 377 | BOOST_TEST(a != b); |
| 378 | } |
| 379 | |
| 380 | template<class T, class A, class D> |
| 381 | void test_is_zero(shared_ptr<T, A, D> const & p) |
| 382 | { |
| 383 | BOOST_TEST(!p); |
| 384 | BOOST_TEST(p.get() == 0); |
| 385 | } |
| 386 | |
| 387 | template<class T, class A, class D> |
| 388 | void test_is_nonzero(shared_ptr<T, A, D> const & p) |
| 389 | { |
| 390 | // p? true: false is used to test p in a boolean context. |
| 391 | // BOOST_TEST(p) is not guaranteed to test the conversion, |
| 392 | // as the macro might test !!p instead. |
| 393 | BOOST_TEST(p? true: false); |
| 394 | BOOST_TEST(p.get() != 0); |
| 395 | } |
| 396 | |
| 397 | int basic_shared_ptr_test() |
| 398 | { |
| 399 | typedef managed_shared_memory::segment_manager segment_mngr_t; |
| 400 | typedef allocator<void, segment_mngr_t> v_allocator_t; |
| 401 | typedef deleter<X, segment_mngr_t> x_deleter_t; |
| 402 | typedef deleter<Y, segment_mngr_t> y_deleter_t; |
| 403 | typedef shared_ptr<X, v_allocator_t, x_deleter_t> x_shared_ptr; |
| 404 | typedef shared_ptr<Y, v_allocator_t, y_deleter_t> y_shared_ptr; |
| 405 | typedef weak_ptr<X, v_allocator_t, x_deleter_t> x_weak_ptr; |
| 406 | typedef weak_ptr<Y, v_allocator_t, y_deleter_t> y_weak_ptr; |
| 407 | |
| 408 | std::string process_name; |
| 409 | test::get_process_id_name(str&: process_name); |
| 410 | |
| 411 | shared_memory_object::remove(filename: process_name.c_str()); |
| 412 | { |
| 413 | managed_shared_memory shmem(create_only, process_name.c_str(), 10000); |
| 414 | { |
| 415 | v_allocator_t v_allocator (shmem.get_segment_manager()); |
| 416 | x_deleter_t x_deleter (shmem.get_segment_manager()); |
| 417 | y_deleter_t y_deleter (shmem.get_segment_manager()); |
| 418 | |
| 419 | y_shared_ptr p (shmem.construct<Y>(name: anonymous_instance)(), v_allocator, y_deleter); |
| 420 | x_shared_ptr p2(shmem.construct<X>(name: anonymous_instance)(), v_allocator, x_deleter); |
| 421 | |
| 422 | test_is_nonzero(p); |
| 423 | test_is_nonzero(p: p2); |
| 424 | test_is_Y(p); |
| 425 | test_is_X(p: p2); |
| 426 | test_ne(a: p, b: p2); |
| 427 | |
| 428 | { |
| 429 | shared_ptr<X, v_allocator_t, y_deleter_t> q(p); |
| 430 | test_eq(a: p, b: q); |
| 431 | } |
| 432 | |
| 433 | y_shared_ptr p3 (dynamic_pointer_cast<Y>(r: p)); |
| 434 | shared_ptr<Y, v_allocator_t, x_deleter_t> p4 (dynamic_pointer_cast<Y>(r: p2)); |
| 435 | test_is_nonzero(p: p3); |
| 436 | test_is_zero(p: p4); |
| 437 | BOOST_TEST(p.use_count() == 2); |
| 438 | BOOST_TEST(p2.use_count() == 1); |
| 439 | BOOST_TEST(p3.use_count() == 2); |
| 440 | test_is_Y(p: p3); |
| 441 | test_eq2(a: p, b: p3); |
| 442 | test_ne2(a: p2, b: p4); |
| 443 | |
| 444 | shared_ptr<void, v_allocator_t, y_deleter_t> p5(p); |
| 445 | |
| 446 | test_is_nonzero(p: p5); |
| 447 | test_eq2(a: p, b: p5); |
| 448 | BOOST_TEST(p5.use_count() == 3); |
| 449 | |
| 450 | x_weak_ptr wp1(p2); |
| 451 | |
| 452 | BOOST_TEST(!wp1.expired()); |
| 453 | BOOST_TEST(wp1.use_count() != 0); |
| 454 | |
| 455 | p.reset(); |
| 456 | p2.reset(); |
| 457 | p3.reset(); |
| 458 | p4.reset(); |
| 459 | |
| 460 | test_is_zero(p); |
| 461 | test_is_zero(p: p2); |
| 462 | test_is_zero(p: p3); |
| 463 | test_is_zero(p: p4); |
| 464 | |
| 465 | BOOST_TEST(p5.use_count() == 1); |
| 466 | BOOST_TEST(wp1.expired()); |
| 467 | BOOST_TEST(wp1.use_count() == 0); |
| 468 | |
| 469 | BOOST_TRY{ |
| 470 | x_shared_ptr sp1(wp1); |
| 471 | BOOST_ERROR("shared_ptr<X, A, D> sp1(wp1) failed to throw" ); |
| 472 | } |
| 473 | BOOST_CATCH(boost::interprocess::bad_weak_ptr const &) |
| 474 | {} BOOST_CATCH_END |
| 475 | |
| 476 | test_is_zero(p: wp1.lock()); |
| 477 | |
| 478 | weak_ptr<X, v_allocator_t, y_deleter_t> wp2 = static_pointer_cast<X>(r: p5); |
| 479 | |
| 480 | BOOST_TEST(wp2.use_count() == 1); |
| 481 | test_is_Y(p: wp2); |
| 482 | test_nonshared(a: wp1, b: wp2); |
| 483 | |
| 484 | // Scoped to not affect the subsequent use_count() tests. |
| 485 | { |
| 486 | shared_ptr<X, v_allocator_t, y_deleter_t> sp2(wp2); |
| 487 | test_is_nonzero(p: wp2.lock()); |
| 488 | } |
| 489 | |
| 490 | y_weak_ptr wp3 = dynamic_pointer_cast<Y>(r: wp2.lock()); |
| 491 | |
| 492 | BOOST_TEST(wp3.use_count() == 1); |
| 493 | test_shared(a: wp2, b: wp3); |
| 494 | |
| 495 | weak_ptr<X, v_allocator_t, y_deleter_t> wp4(wp3); |
| 496 | |
| 497 | BOOST_TEST(wp4.use_count() == 1); |
| 498 | test_shared(a: wp2, b: wp4); |
| 499 | |
| 500 | wp1 = p2; |
| 501 | test_is_zero(p: wp1.lock()); |
| 502 | |
| 503 | wp1 = p4; |
| 504 | |
| 505 | x_weak_ptr wp5; |
| 506 | |
| 507 | bool b1 = wp1 < wp5; |
| 508 | bool b2 = wp5 < wp1; |
| 509 | |
| 510 | y_shared_ptr p6 = static_pointer_cast<Y>(r: p5); |
| 511 | p5.reset(); |
| 512 | p6.reset(); |
| 513 | |
| 514 | BOOST_TEST(wp1.use_count() == 0); |
| 515 | BOOST_TEST(wp2.use_count() == 0); |
| 516 | BOOST_TEST(wp3.use_count() == 0); |
| 517 | |
| 518 | // Test operator< stability for std::set< weak_ptr<> > |
| 519 | // Thanks to Joe Gottman for pointing this out |
| 520 | BOOST_TEST(b1 == (wp1 < wp5)); |
| 521 | BOOST_TEST(b2 == (wp5 < wp1)); |
| 522 | } |
| 523 | |
| 524 | BOOST_TEST(cnt == 0); |
| 525 | } |
| 526 | shared_memory_object::remove(filename: process_name.c_str()); |
| 527 | return boost::report_errors(); |
| 528 | } |
| 529 | |
| 530 | struct alias_tester |
| 531 | { |
| 532 | int v_; |
| 533 | |
| 534 | explicit alias_tester( int v ): v_( v ) |
| 535 | { |
| 536 | } |
| 537 | |
| 538 | ~alias_tester() |
| 539 | { |
| 540 | v_ = 0; |
| 541 | } |
| 542 | }; |
| 543 | |
| 544 | void test_alias() |
| 545 | { |
| 546 | typedef managed_shared_memory::segment_manager segment_mngr_t; |
| 547 | typedef allocator<void, segment_mngr_t> v_allocator_t; |
| 548 | typedef deleter<int, segment_mngr_t> int_deleter_t; |
| 549 | |
| 550 | typedef shared_ptr<int, v_allocator_t, int_deleter_t> int_shared_ptr; |
| 551 | typedef shared_ptr<const int, v_allocator_t, int_deleter_t> const_int_shared_ptr; |
| 552 | |
| 553 | std::string process_name; |
| 554 | test::get_process_id_name(str&: process_name); |
| 555 | |
| 556 | shared_memory_object::remove(filename: process_name.c_str()); |
| 557 | { |
| 558 | managed_shared_memory shmem(create_only, process_name.c_str(), 10000); |
| 559 | |
| 560 | { |
| 561 | int m = 0; |
| 562 | int_shared_ptr p; |
| 563 | int_shared_ptr p2( p, &m ); |
| 564 | |
| 565 | BOOST_TEST( ipcdetail::to_raw_pointer(p2.get()) == &m ); |
| 566 | BOOST_TEST( p2? true: false ); |
| 567 | BOOST_TEST( !!p2 ); |
| 568 | BOOST_TEST( p2.use_count() == p.use_count() ); |
| 569 | BOOST_TEST( !( p < p2 ) && !( p2 < p ) ); |
| 570 | |
| 571 | p2.reset( r: p, p: static_cast<int*>(0) ); |
| 572 | |
| 573 | BOOST_TEST( p2.get() == 0 ); |
| 574 | |
| 575 | BOOST_TEST( p2? false: true ); |
| 576 | BOOST_TEST( !p2 ); |
| 577 | BOOST_TEST( p2.use_count() == p.use_count() ); |
| 578 | BOOST_TEST( !( p < p2 ) && !( p2 < p ) ); |
| 579 | } |
| 580 | |
| 581 | { |
| 582 | int m = 0; |
| 583 | int_shared_ptr p(make_managed_shared_ptr |
| 584 | (constructed_object: shmem.construct<int>(name: anonymous_instance)(), managed_memory&: shmem)); |
| 585 | const_int_shared_ptr p2( p, &m ); |
| 586 | |
| 587 | BOOST_TEST( ipcdetail::to_raw_pointer(p2.get()) == &m ); |
| 588 | BOOST_TEST( p2? true: false ); |
| 589 | BOOST_TEST( !!p2 ); |
| 590 | BOOST_TEST( p2.use_count() == p.use_count() ); |
| 591 | BOOST_TEST( !( p < p2 ) && !( p2 < p ) ); |
| 592 | int_shared_ptr p_nothrow(make_managed_shared_ptr |
| 593 | (constructed_object: shmem.construct<int>(name: anonymous_instance)(), managed_memory&: shmem, std::nothrow)); |
| 594 | } |
| 595 | } |
| 596 | shared_memory_object::remove(filename: process_name.c_str()); |
| 597 | } |
| 598 | |
| 599 | |
| 600 | struct std_deleter |
| 601 | { |
| 602 | typedef const void* pointer; |
| 603 | |
| 604 | void operator()(const void* p) const; |
| 605 | }; |
| 606 | |
| 607 | struct shared_from_this_tester: enable_shared_from_this< |
| 608 | const shared_from_this_tester, std::allocator<void>, std_deleter |
| 609 | > {}; |
| 610 | |
| 611 | void std_deleter::operator()(const void* p) const |
| 612 | { |
| 613 | delete static_cast<const shared_from_this_tester*>(p); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | void test_const_shared_from_this() |
| 618 | { |
| 619 | shared_ptr<const shared_from_this_tester, std::allocator<void>, std_deleter> cptr( |
| 620 | new shared_from_this_tester() |
| 621 | ); |
| 622 | BOOST_TEST( cptr->shared_from_this().get() == cptr.get() ); |
| 623 | } |
| 624 | |
| 625 | int main() |
| 626 | { |
| 627 | if(0 != simple_test()) |
| 628 | return 1; |
| 629 | |
| 630 | if(0 != string_shared_ptr_vector_insertion_test()) |
| 631 | return 1; |
| 632 | |
| 633 | if(0 != basic_shared_ptr_test()) |
| 634 | return 1; |
| 635 | |
| 636 | test_alias(); |
| 637 | test_const_shared_from_this(); |
| 638 | } |
| 639 | |
| 640 | |