1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <list>
#include <limits>
#include <boost/signals2.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
class Point
{
public:
friend std::ostream& operator<< (std::ostream& os, const Point& p);
Point(int x, int y)
:m_x(x), m_y(y)
{}
private:
int m_x;
int m_y;
};
std::ostream& operator<< (std::ostream& os, const Point& p)
{
return os << "[" << p.m_x << "," << p.m_y << "]";
}
typedef boost::shared_ptr<Point> point_ptr;
typedef boost::weak_ptr<Point> point_wptr;
class Interaction
{
public:
Interaction() {}
void AddPoint(point_ptr p)
{
m_points.push_back(p);
}
void ApplyToAll()
{
std::cout << "---List of Points----" << std::endl;
for (std::list<point_wptr>::iterator iter = m_points.begin(); iter != m_points.end();)
{
point_ptr p = iter->lock();
if (p)
{
std::cout << *p << std::endl;
++iter;
}
else
{
iter = m_points.erase(iter);
}
}
std::cout << std::endl;
}
private:
std::list<point_wptr> m_points;
};
int main()
{
Interaction inter;
point_ptr p1 (new Point(2, 3));
inter.AddPoint(p1);
inter.ApplyToAll();
{
point_ptr p2 (new Point(4,5));
inter.AddPoint(p2);
inter.ApplyToAll();
}
inter.ApplyToAll();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
} |
Partager