[ create a new paste ] login | about

Link: http://codepad.org/DH2Hjx39    [ raw code | output | fork ]

C++, pasted on Dec 14:
#include <vector>

using namespace std;

class IMessageListener
{
public:
	friend void SendMessage(const std::string& message);
	IMessageListener();
	virtual ~IMessageListener();
protected:
	virtual void handle(const std::string& message)=0;
	void hoge(){};
};

typedef std::vector<IMessageListener*> MessageListenerList;
typedef MessageListenerList::iterator MessageListenerListIt;

namespace {
MessageListenerList messageListenerList_;
}

IMessageListener::IMessageListener()
{
	messageListenerList_.push_back(this);
}

IMessageListener::~IMessageListener()
{
	messageListenerList_.erase(remove(messageListenerList_.begin(), messageListenerList_.end(), this), messageListenerList_.end());
}

class Hoge : public IMessageListener
{
public:
	Hoge(int value)
	: value_(value)
	{
	}
protected:
	virtual void handle(const std::string& message)
	{
		cout << "Hoge(" << value_ << ")::handle" << message << endl;
	}
private:
	int value_;
};

class Foo : public IMessageListener
{
protected:
	virtual void handle(const std::string& message)
	{
		cout << "Foo::handle:" << message << endl;
	}
};

class Bar : public IMessageListener
{
public:
	Bar()
	: foo_(new Foo())
	{
	}
        virtual ~Bar()
        {
                delete foo_;
        }
protected:
	virtual void handle(const std::string& message)
	{
		cout << "Bar::handle:" << message << endl;
                delete foo_;
                foo_ = 0;
	}
private:
	Foo* foo_;
};

void SendMessage(const std::string& message)
{
	for_each(
		messageListenerList_.begin(),
		messageListenerList_.end(),
		boost::bind(&IMessageListener::handle,_1,boost::ref(message))
	);
}



int main()
{
	{
		Bar bar;
		Hoge hoge(0);
		Hoge hoge2(1);
		Hoge hoge3(2);
		Hoge hoge4(3);
		Hoge hoge5(4);
		Hoge hoge6(5);

		::SendMessage("test");
                cout << "--------------------" << endl;
		::SendMessage("test2");
	}
        cout << "--------------------" << endl;
        ::SendMessage("test3");
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Bar::handle:test
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/debug/safe_iterator.h:454:
    error: attempt to compare a dereferenceable iterator to a singular     
    iterator.

Objects involved in the operation:
iterator "lhs" @ 0x0xbf9c5684 {
type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPP16IMessageListenerN10__gnu_norm6vectorIS4_SaIS4_EEEEEN15__gnu_debug_def6vectorIS4_S8_EEEE (mutable iterator);
  state = dereferenceable;
  references sequence with type `N15__gnu_debug_def6vectorIP16IMessageListenerSaIS2_EEE' @ 0x0xbf9c5684
}
iterator "rhs" @ 0x0xbf9c565c {
type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPP16IMessageListenerN10__gnu_norm6vectorIS4_SaIS4_EEEEEN15__gnu_debug_def6vectorIS4_S8_EEEE (mutable iterator);
  state = singular;
  references sequence with type `N15__gnu_debug_def6vectorIP16IMessageListenerSaIS2_EEE' @ 0x0xbf9c565c
}

Disallowed system call: SYS_kill


Create a new paste based on this one


Comments: