⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doc_view.cpp

📁 An article on the implementation of a fast C++ delegate with many advanced features.
💻 CPP
字号:
// 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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -