如何使用BOOST 信号(一)
信号与插槽-概念
signal :Signals represents callbacks with multiple targets, and are also called publisher or events in similar systems.
slot : Signals are connected to some set of slots, which are callback receivers(also called event targets or subscribers), which are called when the signal is "emitted."
Exp 1 : call a single slot.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
struct Hello_World
{
void operator ()() const
{
wcout << " Hello World " << endl;
}
};
int main()
{
boost::signal<void ()> sig;
boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
Hello_World hw;
sig.connect(hw);
sig();
return 0;
}
Exp 2 :call multiple slots.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
struct Hello
{
void operator ()() const
{
cout << " HELLO " << endl;
}
};
struct World
{
void operator ()() const
{
cout << " World " << endl;
}
};
int main()
{
boost::signal<void ()> sig;
Hello hello;
World world;
sig.connect(1, world); // Ordering slot call groups.
sig.connect(0, hello); //
boost::this_thread::sleep(boost::posix_time::milliseconds(2000));
sig();
return 0;
}
Exp 3: how signals propagate arguments to each of the slots they call.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
float print_sum(float x, float y)
{
std::cout << "The sum is " << x+y << std::endl;
return x+y;
}
float print_product(float x, float y)
{
std::cout << "The product is " << x*y << std::endl;
return x*y;
}
float print_difference(float x, float y)
{
std::cout << "The difference is " << x-y << std::endl;
return x-y;
}
float print_quotient(float x, float y)
{
std::cout << "The quotient is " << x/y << std::endl;
return x/y;
}
int main()
{
boost::signal<float (float , float )> sig;
sig.connect(0, &print_sum);
sig.connect(1, &print_product);
sig.connect(2, &print_difference);
sig.connect(3, &print_quotient);
// Output 1.6667 because return by the last slot called.
cout << sig(5, 3) << endl;
return 0;
}
Exp 4: return a maximum result by provide another argument.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
float print_sum(float x, float y)
{
std::cout << "The sum is " << x+y << std::endl;
return x+y;
}
float print_product(float x, float y)
{
std::cout << "The product is " << x*y << std::endl;
return x*y;
}
float print_difference(float x, float y)
{
std::cout << "The difference is " << x-y << std::endl;
return x-y;
}
float print_quotient(float x, float y)
{
std::cout << "The quotient is " << x/y << std::endl;
return x/y;
}
template <typename T>
struct Maximum
{
typedef T result_type;
template <typename InputIterator>
T operator ()(InputIterator first, InputIterator last) const
{
if ( first == last )
return T();
T max_value = *first++;
while ( first != last )
{
if ( max_value < *first )
max_value = *first;
++first;
}
return max_value;
}
};
int main()
{
// installing Maximum as a combiner for our signal.
boost::signal< float (float , float ), Maximum<float > > sig;
sig.connect(0, &print_sum);
sig.connect(1, &print_product);
sig.connect(2, &print_difference);
sig.connect(3, &print_quotient);
// Output 15.
cout << sig(5, 3) << endl;
return 0;
}
Exp 5: return all of the values computed by the slots together, in one large data structure.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace boost;
float print_sum(float x, float y)
{
std::cout << "The sum is " << x+y << std::endl;
return x+y;
}
float print_product(float x, float y)
{
std::cout << "The product is " << x*y << std::endl;
return x*y;
}
float print_difference(float x, float y)
{
std::cout << "The difference is " << x-y << std::endl;
return x-y;
}
float print_quotient(float x, float y)
{
std::cout << "The quotient is " << x/y << std::endl;
return x/y;
}
template <typename T>
struct Maximum
{
typedef T result_type;
template <typename InputIterator>
T operator ()(InputIterator first, InputIterator last) const
{
if ( first == last )
return T();
T max_value = *first++;
while ( first != last )
{
if ( max_value < *first )
max_value = *first;
++first;
}
return max_value;
}
};
template <typename Container>
struct aggregate_values
{
typedef Container result_type;
template <typename InputIterator>
Container operator ()(InputIterator first, InputIterator last) const
{
return Container(first, last);
}
};
int main()
{
boost::signal< float (float , float ),
aggregate_values< std::vector<float > > >sig;
sig.connect(0, &print_sum);
sig.connect(1, &print_product);
sig.connect(2, &print_difference);
sig.connect(3, &print_quotient);
std::vector<float > results = sig(5, 3);
// Output 8 15 2 1.66667
std::copy(results.begin(), results.end(),
std::ostream_iterator<float >(cout, " " ));
return 0;
}