Reputation: 11
In my basic c++ book, there is no class declaration like below. the strange code for me is ...
boost::signals2::signal<bool (const std::string& message,
const std::string& caption, unsigned int style),
boost::signals2::last_value<bool> > ThreadSafeMessageBox;
the things in round brackets (const std:::string
...) are not a typename but instance. How could it be possible? The code above compiles fine.
p.s. template class (signal
) code is
template<typename Signature,
typename Combiner = optional_last_value<typename boost::function_traits<Signature>::result_type>,
typename Group = int,
typename GroupCompare = std::less<Group>,
typename SlotFunction = function<Signature>,
typename ExtendedSlotFunction = typename detail::extended_signature<function_traits<Signature>::arity, Signature>::function_type,
typename Mutex = mutex >
class signal: public detail::signalN<function_traits<Signature>::arity,
Signature, Combiner, Group, GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex>::type
{ /*...*};
Upvotes: 1
Views: 273
Reputation: 41100
Check out the documentation for Boost.Signals2:
The Boost.Signals2 library is an implementation of a managed signals and slots system. Signals represent callbacks with multiple targets
So we know that a "signal" has something to do with "callbacks". A callback is a function to call at a later time.
So, then check out the "Hello World" example in the documentation:
struct HelloWorld
{
void operator()() const
{
std::cout << "Hello, World!" << std::endl;
}
};
// ...
// Signal with no arguments and a void return value
boost::signals2::signal<void ()> sig;
// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);
// Call all of the slots
sig();
First, we create a signal
sig
, a signal that takes no arguments and has avoid
return value. Next, we connect thehello
function object to the signal using theconnect
method. Finally, use the signalsig
like a function to call the slots, which in turns invokesHelloWorld::operator()
to print "Hello, World!".
After reading all this, what can we deduce? We can deduce that the template argument to a signal is a function type. It indicates the kind of function that can be connected to the signal.
So, in your example
boost::signals2::signal<bool (const std::string& message,
const std::string& caption,
unsigned int style),
boost::signals2::last_value<bool>
> ThreadSafeMessageBox;
ThreadSafeMessageBox
is a signal that can connect to a function that:
bool
const std::string&
const std::string&
unsigned int
(The second template argument we can ignore for the purposes of this question, it is not a required template argument nor is it part of the callback function signature, but rather something called a Combiner)
Upvotes: 3
Reputation: 2788
The type expected as template parameter Signature
is a function signature, that is the specification of the expected function parameters number, types and return type.
In your case
boost::signals2::signal<bool (const std::string& message, const std::string& caption, unsigned int style), boost::signals2::last_value<bool> > ThreadSafeMessageBox;
the first parameter for the template boost::signals2::signal
is the function signature:
bool (const std::string& message, const std::string& caption, unsigned int style)
that is a function with 3 parameters (of type string
, string
and unsigned int
) and returning a bool
.
Upvotes: 0