|
// FD.Delegate library examples
// Document/View sample for Boost.Signals
// Copyright Keith MacDonald 2005. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// For more information, see http://www.boost.org
// Copyright (C) 2007 JaeWook Choi
// , modified from Boost.Signals doc_view.cpp
#define FD_DELEGATE
#include <iostream>
#include <string>
#ifndef FD_DELEGATE
#include <boost/signal.hpp>
#else
#include <fd/delegate.hpp>
#endif
#include <boost/bind.hpp>
class Document
{
public:
#ifndef FD_DELEGATE
typedef boost::signal<void (bool)> signal_t;
typedef boost::signals::connection connection_t;
#else
typedef fd::delegate<void (bool)> signal_t;
typedef fd::multicast::token connection_t;
#endif
public:
Document()
{}
#ifndef FD_DELEGATE
connection_t connect(signal_t::slot_function_type subscriber)
{
return m_sig.connect(subscriber);
}
void disconnect(connection_t subscriber)
{
subscriber.disconnect();
}
#else
connection_t connect(signal_t subscriber)
{
return m_sig.add(subscriber);
}
void disconnect(connection_t subscriber)
{
subscriber.remove();
}
#endif
void append(const char* s)
{
m_text += s;
m_sig(true);
}
const std::string& getText() const
{
return m_text;
}
private:
signal_t m_sig;
std::string m_text;
};
class View
{
public:
View(Document& m)
: m_document(m), m_connection( m_document.connect(boost::bind(&View::refresh, this, _1)) )
{
}
virtual ~View()
{
m_document.disconnect(m_connection);
}
virtual void refresh(bool bExtended) const = 0;
protected:
Document& m_document;
private:
Document::connection_t m_connection;
};
class TextView : public View
{
public:
TextView(Document& doc)
: View(doc)
{}
virtual void refresh(bool bExtended) const
{
std::cout << "TextView: " << m_document.getText() << std::endl;
}
};
class HexView : public View
{
public:
HexView(Document& doc)
: View(doc)
{}
virtual void refresh(bool bExtended) const
{
const std::string& s = m_document.getText();
std::cout << "HexView:";
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
std::cout << ' ' << std::hex << static_cast<int>(*it);
std::cout << std::endl;
}
};
int main(int argc, char* argv[])
{
Document doc;
TextView v1(doc);
HexView v2(doc);
doc.append(argc == 2 ? argv[1] : "Hello world!");
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.