Test: shared_ptr of boost is used in the container class STD: list.

Source: Internet
Author: User

A small test: Using shared_ptr In the STD: List container demonstrates multiple ways to add and delete objects: Using raw pointer and smart pointer) iterator, boost: enable_shared_from_this, etc. The code in this example is only used for demonstration, and only some of the practices can be used in actual use, depending on the needs of the application. Compiled and run in vs2005 and boost_000037_0.

 

# Include <list> <br/> # include <boost/noncopyable. HPP> <br/> # include <boost/enable_shared_from_this.hpp> <br/> # include <boost/shared_ptr.hpp> <br/> # include <algorithm> <br/> # include <iostream> <br/> // task <br/> class task: private boost: noncopyable, public boost: enable_shared_from_this <task> <br/>{< br/> Public: <br/> task (): ID _ (0) {}< br/> task (int id): ID _ (ID) {}< br/> virtual ~ Task () {}< br/> int ID () const {return ID _ ;}< br/> boost: shared_ptr <task> PTR () {return shared_from_this () ;}< br/> Public: <br/> virtual void run () = 0; <br/> protected: <br/> int ID _; <br/>}; <br/> // task1 <br/> class task1: public task <br/>{< br/> public: <br/> task1 (int id): task (ID) {}< br/> Public: <br/> virtual void run () {STD :: cout <"task1 ID:" <ID _ <"running" <STD: Endl ;}< br/>}; <B R/> // task2 <br/> class task2: public task <br/>{< br/> Public: <br/> task2 (int id): task (ID) {}< br/> Public: <br/> virtual void run () {STD: cout <"task2 ID: "<ID _ <" running "<STD: Endl ;}< br/>}; <br/> // typedefs <br/> typedef boost :: shared_ptr <task> taskptr; <br/> typedef STD: List <taskptr> taskptrcontainer; <br/> // ptr_contains_predicate <br/> template <typename T> <br/> struct ptr_contai Ns_predicate <br/>{< br/> ptr_contains_predicate (T * pptr): mptr (pptr) {}< br/> template <typename P> <br/> bool operator () (const P & pptr) const <br/>{< br/> return pptr. get () = mptr; <br/>}< br/> T * mptr; <br/> }; <br/> // ptr_contains <br/> template <typename T> <br/> ptr_contains_predicate <t> ptr_contains (T * pptr) <br/>{< br/> return ptr_contains_predicate <t> (pptr); <br/>}< br/> // shared_equals _ Raw <br/> template <typename T> struct shared_equals_raw <br/>{< br/> shared_equals_raw (T * Raw) <br/>: _ raw (raw) <br/>{}< br/> bool operator () (const boost: shared_ptr <t> & PTR) const <br/>{< br/> return (PTR. get () = _ raw); <br/>}< br/> PRIVATE: <br/> T * const _ raw; <br/> }; <br/> // run task <br/> void run_task (taskptr task) <br/>{< br/> // STD: cout <"task ID: "<task-> ID () <STD: Endl; <br/> task-> RUN (); <Br/>}< br/> // print summary <br/> void print_summary (taskptrcontainer & tasks) <br/>{< br/> STD :: cout <"task count" <tasks. size () <STD: Endl; <br/>}< br/> // application entry <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> taskptrcontainer tasks; <br/> // test data <br/> for (INT I = 0; I <10; I ++) {<br/> if (I % 2 = 0) <br/> tasks. push_back (taskptr (New task1 (I); <br/> else <br/> tasks. P Ush_back (taskptr (New task2 (I); <br/>}< br/> // run the tasks. <br/> STD: for_each (tasks. begin (), tasks. end (), run_task); <br/> print_summary (tasks ); // 10 <br/> // remove by raw pointer <br/> // <br/> task * task = tasks. front (). get (); <br/> tasks. remove_if (ptr_contains (task); <br/> print_summary (tasks); // 9 <br/> taskptr = tasks. front (); <br/> tasks. remove (taskptr); <br/> print_summary (Ta SKS); // 8 <br/> // remove by iterator <br/> task = tasks. front (). get (); <br/> // taskptrcontainer: iterator it = STD: find_if (tasks. begin (), tasks. end (), ptr_contains (task); <br/> // or <br/> taskptrcontainer: iterator it = STD: find_if (tasks. begin (), tasks. end (), shared_equals_raw <task> (task); <br/> If (it! = Tasks. end () {<br/> tasks. erase (it); <br/>}< br/> print_summary (tasks ); // 7 <br/> // smart pointer test <br/> // <br/> taskptr (New task1 (100 )); <br/> tasks. push_back (PTR); <br/> print_summary (tasks); // 8 <br/> // remove by smart pointer <br/> tasks. remove (PTR); <br/> print_summary (tasks); // 7 <br/> // raw pointer test <br/> task * task1 = new task1 (200 ); <br/> tasks. push_back (taskptr (task1); <B R/> print_summary (tasks); // 8 <br/> // donot use in this way. this will cause double Delete. <br/> // tasks. remove (taskptr (task1); // #1 wrong !!! <Br/> tasks. remove_if (ptr_contains (task1); // OK <br/> print_summary (tasks ); // 7 <br/> // use enable_shared_from_this <br/> // <br/> task * task2 = new task2 (300 ); <br/> // donot use in this way. not a shared pointer until now. <br/> // tasks. push_back (task2-> PTR (); // #2 wrong !!! <Br/> tasks. push_back (taskptr (task2); // OK <br/> print_summary (tasks); // 8 <br/> tasks. remove (task2-> PTR (); // #3 correct, problem #1 overcomed <br/> print_summary (tasks); // 7 <br/> getchar (); <br/> return 0; <br/>}< br/>

 

Brief description:

  • #1. If a duplicate deletion problem exists, the solution is #3. Boost: enable_shared_from_this must be used.
  • #2. shared_ptr is not used yet, And shared_ptr cannot be obtained from the internal weak_ptr.
  • #3. solved the problem #1

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.