Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BoostSignals2Example2

 

BoostQt CreatorLubuntu

 

Boost.Signals2 example 2: emitting this is a Boost.Signals2 example that shows how to emit this.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppBoostSignals2Example2/CppBoostSignals2Example2.pro

 

include(../../ConsoleApplication.pri)
include(../../Libraries/Boost.pri)

SOURCES += main.cpp

 

 

 

 

 

./CppBoostSignals2Example2/main.cpp

 

#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/signals2.hpp>
#pragma GCC diagnostic pop

struct Thing
{
  Thing(const int id) : m_signal{}, m_id(id) {}
  boost::signals2::signal<void (const Thing*)> m_signal;
  const int m_id;
  void EmitMe() const { m_signal(this); }
};

struct Manager
{
  Manager(): m_v{}
  {
    for (int i=0; i!=5; ++i)
    {
      boost::shared_ptr<Thing> t(new Thing(i));
      //Do not forget the placeholder!
      t->m_signal.connect(boost::bind(&Manager::OnSignal,this, boost::lambda::_1));
      m_v.push_back(t);
    }
  }
  void EmitRandom()
  {
    const int i = std::rand() % 5;
    m_v[i]->EmitMe();
  }

  private:

  std::vector<boost::shared_ptr<Thing> > m_v;

  void OnSignal(const Thing* thing)
  {
    std::cout << thing->m_id << '\n';
  }
};

int main()
{
  Manager m;
  for (int i=0; i!=10; ++i) m.EmitRandom();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml