shared_ptr_test.cpp
来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 634 行 · 第 1/2 页
CPP
634 行
////////////////////////////////////////////////////////////////////////////////// (C) Copyright Peter Dimov 2002-2005, 2007.// (C) Copyright Ion Gaztanaga 2006-2008.// Distributed under the Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// See http://www.boost.org/libs/interprocess for documentation.////////////////////////////////////////////////////////////////////////////////#include <boost/interprocess/detail/config_begin.hpp>#include <boost/interprocess/offset_ptr.hpp>#include <boost/interprocess/smart_ptr/shared_ptr.hpp>#include <boost/interprocess/smart_ptr/weak_ptr.hpp>#include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp>#include <boost/interprocess/managed_shared_memory.hpp>#include <boost/interprocess/allocators/allocator.hpp>#include <boost/interprocess/containers/string.hpp>#include <boost/interprocess/containers/vector.hpp>#include <boost/interprocess/smart_ptr/deleter.hpp>#include <boost/interprocess/smart_ptr/scoped_ptr.hpp>#include <boost/detail/lightweight_test.hpp>#include <string>#include "get_process_id_name.hpp"using namespace boost::interprocess;class base_class{ public: virtual ~base_class() {}};class derived_class : public base_class{ public: virtual ~derived_class() {}};int simple_test(){ typedef allocator<base_class, managed_shared_memory::segment_manager> base_class_allocator; typedef deleter<base_class, managed_shared_memory::segment_manager> base_deleter_t; typedef shared_ptr<base_class, base_class_allocator, base_deleter_t> base_shared_ptr; typedef weak_ptr<base_class, base_class_allocator, base_deleter_t> base_weak_ptr; std::string process_name; test::get_process_id_name(process_name); shared_memory_object::remove(process_name.c_str()); { managed_shared_memory shmem(create_only, process_name.c_str(), 10000); { base_shared_ptr s_ptr(base_shared_ptr::pointer(0), base_class_allocator(shmem.get_segment_manager()), base_deleter_t(shmem.get_segment_manager())); base_shared_ptr s_ptr2(shmem.construct<base_class>("base_class")(), base_class_allocator(shmem.get_segment_manager()), base_deleter_t(shmem.get_segment_manager())); base_shared_ptr s_ptr3(offset_ptr<derived_class>(shmem.construct<derived_class>("derived_class")()), base_class_allocator(shmem.get_segment_manager()), base_deleter_t(shmem.get_segment_manager())); if(s_ptr3.get_deleter() == 0){ return 1; } //if(s_ptr3.get_allocator() == 0){ //return 1; //} base_shared_ptr s_ptr_empty; if(s_ptr_empty.get_deleter() != 0){ return 1; } //if(s_ptr_empty.get_allocator() != 0){ //return 1; //} } } shared_memory_object::remove(process_name.c_str()); return 0;}int string_shared_ptr_vector_insertion_test(){ //Allocator of chars typedef allocator<char, managed_shared_memory::segment_manager > char_allocator_t; //A shared memory string class typedef basic_string<char, std::char_traits<char>, char_allocator_t> string_t; //A shared memory string allocator typedef allocator<string_t, managed_shared_memory::segment_manager> string_allocator_t; //A deleter for shared_ptr<> that erases a shared memory string typedef deleter<string_t, managed_shared_memory::segment_manager> string_deleter_t; //A shared pointer that points to a shared memory string and its instantiation typedef shared_ptr<string_t, string_allocator_t, string_deleter_t> string_shared_ptr_t; //An allocator for shared pointers to a string in shared memory typedef allocator<string_shared_ptr_t, managed_shared_memory::segment_manager> string_shared_ptr_allocator_t; //A weak pointer that points to a shared memory string and its instantiation typedef weak_ptr<string_t, string_allocator_t, string_deleter_t> string_weak_ptr_t; //An allocator for weak pointers to a string in shared memory typedef allocator<string_weak_ptr_t, managed_shared_memory::segment_manager > string_weak_ptr_allocator_t; //A vector of shared pointers to strings (all in shared memory) and its instantiation typedef vector<string_shared_ptr_t, string_shared_ptr_allocator_t> string_shared_ptr_vector_t; //A vector of weak pointers to strings (all in shared memory) and its instantiation typedef vector<string_weak_ptr_t, string_weak_ptr_allocator_t> string_weak_ptr_vector_t; std::string process_name; test::get_process_id_name(process_name); //A shared memory managed memory classes shared_memory_object::remove(process_name.c_str()); { managed_shared_memory shmem(create_only, process_name.c_str(), 20000); { const int NumElements = 100; //Construct the allocator of strings string_allocator_t string_allocator(shmem.get_segment_manager()); //Construct the allocator of a shared_ptr to string string_shared_ptr_allocator_t string_shared_ptr_allocator(shmem.get_segment_manager()); //Construct the allocator of a shared_ptr to string string_weak_ptr_allocator_t string_weak_ptr_allocator(shmem.get_segment_manager()); //This is a string deleter using destroy_ptr() function of the managed_shared_memory string_deleter_t deleter(shmem.get_segment_manager()); //Create a string in shared memory, to avoid leaks with exceptions use //scoped ptr until we store this pointer in the shared ptr scoped_ptr<string_t, string_deleter_t> scoped_string (shmem.construct<string_t>(anonymous_instance)(string_allocator), deleter); //Now construct a shared pointer to a string string_shared_ptr_t string_shared_ptr (scoped_string.get(), string_shared_ptr_allocator, deleter); //Check use count is just one if(!string_shared_ptr.unique()){ return 1; } //We don't need the scoped_ptr anonymous since the raw pointer is in the shared ptr scoped_string.release(); //Now fill a shared memory vector of shared_ptrs to a string string_shared_ptr_vector_t my_sharedptr_vector(string_shared_ptr_allocator); my_sharedptr_vector.insert(my_sharedptr_vector.begin(), NumElements, string_shared_ptr); //Insert in the middle to test movability my_sharedptr_vector.insert(my_sharedptr_vector.begin() + my_sharedptr_vector.size()/2, NumElements, string_shared_ptr); //Now check the shared count is the objects contained in the //vector plus string_shared_ptr if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ return 1; } //Now create a weak ptr from the shared_ptr string_weak_ptr_t string_weak_ptr (string_shared_ptr); //Use count should remain the same if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ return 1; } //Now reset the local shared_ptr and check use count string_shared_ptr.reset(); if(string_weak_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size())){ return 1; } //Now reset the local shared_ptr's use count should be zero if(string_shared_ptr.use_count() != 0){ return 1; } //Now recreate the shared ptr from the weak ptr //and recheck use count string_shared_ptr = string_shared_ptr_t(string_weak_ptr); if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ return 1; } //Now fill a vector of weak_ptr-s string_weak_ptr_vector_t my_weakptr_vector(string_weak_ptr_allocator); my_weakptr_vector.insert(my_weakptr_vector.begin(), NumElements, string_weak_ptr); //The shared count should remain the same if(string_shared_ptr.use_count() != static_cast<long>(my_sharedptr_vector.size()+1)){ return 1; } //So weak pointers should be fine string_weak_ptr_vector_t::iterator beg = my_weakptr_vector.begin(), end = my_weakptr_vector.end(); for(;beg != end; ++beg){ if(beg->expired()){ return 1; } //The shared pointer constructed from weak ptr should //be the same as the original, since all weak pointer //point the the same object if(string_shared_ptr_t(*beg) != string_shared_ptr){ return 1; } } //Now destroy all the shared ptr-s of the shared ptr vector my_sharedptr_vector.clear(); //The only alive shared ptr should be the local one if(string_shared_ptr.use_count() != 1){ return 1; } //Now we invalidate the last alive shared_ptr string_shared_ptr.reset(); //Now all weak pointers should have expired beg = my_weakptr_vector.begin(); end = my_weakptr_vector.end(); for(;beg != end; ++beg){ if(!beg->expired()){ return 1; } bool success = false; //Now this should throw try{ string_shared_ptr_t dummy(*beg); //We should never reach here return 1; } catch(const boost::interprocess::bad_weak_ptr &){ success = true; } if(!success){ return 1; } } //Clear weak ptr vector my_weakptr_vector.clear(); //Now lock returned shared ptr should return null if(string_weak_ptr.lock().get()){ return 1; } //Reset weak_ptr string_weak_ptr.reset(); } } shared_memory_object::remove(process_name.c_str()); return 0;}//// This part is taken from shared_ptr_basic_test.cpp//// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.// Copyright (c) 2006 Ion Gaztanaga//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//static int cnt = 0;struct X{ X(){ ++cnt; } // virtual destructor deliberately omitted ~X(){ --cnt; } virtual int id() const { return 1; } private: X(X const &); X & operator= (X const &);};struct Y: public X{ Y(){ ++cnt; } ~Y(){ --cnt; } virtual int id() const { return 2; } private: Y(Y const &); Y & operator= (Y const &);};int * get_object(){ ++cnt; return &cnt; }void release_object(int * p){ BOOST_TEST(p == &cnt); --cnt; }template<class T, class A, class D> void test_is_X(shared_ptr<T, A, D> const & p){ BOOST_TEST(p->id() == 1); BOOST_TEST((*p).id() == 1);}template<class T, class A, class D> void test_is_X(weak_ptr<T, A, D> const & p){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?