U E D R , A S I H C RSS

Boost/Smart Pointer

์Šค๋งˆํŠธ ํฌ์ธํ„ฐ ์“ฐ๊ธฐ

์„ค์น˜

  • boost ์— ๊ฐ€์„œ ๋‹ค์šด๋ฐ›๊ณ  ์„ค์น˜ํ•œ๋‹ค.
  • boost ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ—ค๋” ํŒŒ์ผ๋งŒ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ๋“  ๊ตฌํ˜„์„ ํ—ค๋” ํŒŒ์ผ์— ํ•ด ๋†“์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋ฐ”๋กœ ํ•„์‚ด ํ—ค๋”์™€ ๊ตฌํ˜„ ๋ถ„๋ฆฌํ•˜๊ธฐ ๊ท€์ž–์–ด ๊ธฐ์ˆ ....๋ณธ์ธ์€ ์• ์šฉํ•œ๋‹ค...

shared_ptr

  • new ๋ฅผ ํ•ด์ฃผ๊ณ  delete ํ•˜๋Š” ๊ฒƒ ์—์„œ ํ•ด๋ฐฉ์„!
  • ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋„ auto_ptr ์ด๋ผ๊ณ  ๊ฐ™์€ ๊ฐœ๋…์˜ ํด๋ž˜์Šค๊ฐ€ ์žˆ์ง€๋งŒ, ๋ฒ„๊ทธ๊ฐ€ ์žˆ๋‹ค. -_-;; auto_ptr๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ์ง‘์–ด๋„ฃ์œผ๋ฉด ๋ฒ„๊ทธ๋•œ์‹œ ์Šค์Šค๋กœ ์‚ญ์ œ๊ฐ€ ์•ˆ๋˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ƒˆ๋Š” ์•„์ฃผ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ์ด๋‹ค.

~cpp 
...
#include <boost/smart_ptr.hpp>
...
using namespace boost;
...
class Vertex3D {...}
typedef shared_ptr<Vertex3D> Vertex3DSPtr;
typedef vector<Vertex3DSPtr> Vertexs; // ๋‹จ์–ด ํ‹€๋ ธ๋‹ค๋Š”๊ฑฐ ์•Œ์ง€๋งŒ ๊ทธ๋ƒฅ ์”€ -_-
typedef Vertexs::size_type VertexsSize;
typedef Vertexs::iterator VertexsItr;
typedef shared_ptr<Vertexs> VertexsSPtr;
...
{
  VertexsSPtr vertexs(new Vertexs);
  Vertex3DSPtr vertex(new Vertex3D);
  ...
  vertexs->push_back(vertex);
  ...
}
  • new ๋ฅผ ํ–ˆ์ง€๋งŒ delete ๋ฅผ ์•ˆํ•ด๋„ ์ง€๊ฐ€ ์•Œ์•„์„œ ์‚ฌ๋ผ์ง„๋‹ค.
  • ๋ชจ๋“  ๊ฐ์ฒด์— ์Šค๋งˆํŠธํฌ์ธํ„ฐ๋ฅผ ์“ฐ๋„๋ก ํ”„๋กœ๊ทธ๋žจ ์งœ๋ฉด ์ž๋ฐ”๊ฐ™์€ ๋А๋‚Œ์œผ๋กœ ์งค์ˆ˜๋„ ...

shared_ptr_example

shared_ptr_example.cpp
~cpp 
//  Boost shared_ptr_example.cpp  --------------------------------------------//

//  (C) Copyright Beman Dawes 2001. Permission to copy,
//  use, modify, sell and distribute this software is granted provided this
//  copyright notice appears in all copies. This software is provided "as is"
//  without express or implied warranty, and with no claim as to its
//  suitability for any purpose.

//  See http://www.boost.org for most recent version including documentation.

//  Revision History
//  21 May 01  Initial complete version (Beman Dawes)

//  The original code for this example appeared in the shared_ptr documentation.
//  Ray Gallimore pointed out that foo_set was missing a Compare template
//  argument, so would not work as intended.  At that point the code was
//  turned into an actual .cpp file so it could be compiled and tested.

#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <boost/shared_ptr.hpp>

//  The application will produce a series of
//  objects of type Foo which later must be
//  accessed both by occurrence (std::vector)
//  and by ordering relationship (std::set).

struct Foo
{
  Foo( int _x ) : x(_x) {}
  ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
  int x;
  /* ... */
};

typedef boost::shared_ptr<Foo> FooPtr;

struct FooPtrOps
{
  bool operator()( const FooPtr & a, const FooPtr & b )
    { return a->x > b->x; }
  void operator()( const FooPtr & a )
    { std::cout << a->x << "\n"; }
};

int main()
{
  std::vector<FooPtr>         foo_vector;
  std::set<FooPtr,FooPtrOps>  foo_set; // NOT multiset!

  FooPtr foo_ptr( new Foo( 2 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset( new Foo( 1 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset( new Foo( 3 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  foo_ptr.reset ( new Foo( 2 ) );
  foo_vector.push_back( foo_ptr );
  foo_set.insert( foo_ptr );

  std::cout << "foo_vector:\n";
  std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );

  std::cout << "\nfoo_set:\n";
  std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() );
  std::cout << "\n";

//  Expected output:
//
//   foo_vector:
//   2
//   1
//   3
//   2
//
//   foo_set:
//   3
//   2
//   1
//
//   Destructing a Foo with x=2
//   Destructing a Foo with x=1
//   Destructing a Foo with x=3
//   Destructing a Foo with x=2

  return 0;
}

shared_ptr_example2

shared_ptr_example2.hpp (ํ—‰ ์ด๋„ˆํด๋ž˜์Šค๊ฐ€ ๋˜๋„ค.. ์ฒ˜์Œ๋ดค๋‹ค --;;)
~cpp 
//  Boost shared_ptr_example2 header file  -----------------------------------//

#include <boost/shared_ptr.hpp>

//  This example demonstrates the handle/body idiom (also called pimpl and
//  several other names).  It separates the interface (in this header file)
//  from the implementation (in shared_ptr_example2.cpp).

//  Note that even though example::implementation is an incomplete type in
//  some translation units using this header, shared_ptr< implementation >
//  is still valid because the type is complete where it counts - in the
//  shared_ptr_example2.cpp translation unit where functions requiring a
//  complete type are actually instantiated.

class example
{
 public:
  example();
  example( const example & );
  example & operator=( const example & );
  void do_something();
 private:
  class implementation;
  boost::shared_ptr< implementation > _imp; // hide implementation details
};

shared_ptr_example2.cpp
~cpp 
// Boost shared_ptr_example2 implementation file  -----------------------------//

#include "shared_ptr_example2.hpp"
#include <iostream>

class example::implementation
{
 public:
  ~implementation() { std::cout << "destroying implementation\n"; }
};

example::example() : _imp( new implementation ) {}
example::example( const example & s ) : _imp( s._imp ) {}

example & example::operator=( const example & s )
  { _imp = s._imp; return *this; }

void example::do_something()
  { std::cout << "use_count() is " << _imp.use_count() << "\n"; }

shared_ptr_example2_test.cpp
~cpp 
// Boost shared_ptr_example2_test main program  ------------------------------//

#include "shared_ptr_example2.hpp"

int main()
{
  example a;
  a.do_something();
  example b(a);
  b.do_something();
  example c;
  c = a;
  c.do_something();
  return 0;
}


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:38
Processing time 0.0054 sec