Salsudan’s Blog

Intrusive_ptr

Native pointer 처럼 사용할수 있는 Smart point입니다.

  • 간단한 예제 입니다.

<node.h>

#include <string>
#include <iostream>
#include <boost/detail/atomic_count.hpp>

class node
{
private:
 std::string id_;
 std::string info_;
 mutable boost::detail::atomic_count m_refs;
public:

 friend void intrusive_ptr_add_ref(node const*);
 friend void intrusive_ptr_release(node const*);

 node()
  : id_(“”),info_(“”),m_refs(0)
 {}
 node(std::string id, std::string info)
  : id_(id),
  info_(info),
  m_refs(0)
 {
 }
 void print_node()
 {
  std::cout << id_ << “::” << info_ << std::endl;
 }

};
void intrusive_ptr_add_ref(node const* c)
{
 ++c->m_refs;
}

void intrusive_ptr_release(node const* c)
{
 if (–c->m_refs == 0)
  delete c;
}

==============================================

<main.cpp>

void fun1(boost::intrusive_ptr<node> t)
{
 node* a = t.get();
 t->print_node();

 

}
int main()
{
 boost::intrusive_ptr<node> node_ptr = new node(“1″,”node info of 1″);

 node_ptr->print_node();

 fun1(node_ptr);

 node_ptr->print_node();
 
 node_ptr.reset(new node(“2″,”node info of 2″));
 node_ptr->print_node();
 return 0;
}

 

 

 

No Comments Yet »

댓글이 없습니다.

이 글의 댓글을 위한 RSS 피드 TrackBack URI

댓글 남기기

Blog at WordPress.com.