/usr/include/itpp/protocol/signals_slots.h is in libitpp-dev 4.3.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | /*!
* \file
* \brief Definitions of Signals and Slots classes
* \author Anders Persson
*
* -------------------------------------------------------------------------
*
* Copyright (C) 1995-2010 (see AUTHORS file for a list of contributors)
*
* This file is part of IT++ - a C++ library of mathematical, signal
* processing, speech processing, and communications classes and functions.
*
* IT++ is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with IT++. If not, see <http://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef SIGNAL_SLOT_H
#define SIGNAL_SLOT_H
#include <itpp/itexports.h>
#if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB) && !(defined(itpp_EXPORTS) || defined(itpp_debug_EXPORTS)))
#ifndef ITPP_PROTOCOL_EXCLUDED
#define ITPP_PROTOCOL_EXCLUDED
#pragma message( "PROTOCOL definitions are not available for MSVC shared builds" )
#endif
#else
#include <itpp/protocol/events.h>
#include <list>
#include <iostream>
namespace itpp
{
//! \addtogroup protocol
//@{
class Base_Signal;
template<class DataType> class Signal;
template<class DataType> class Base_Slot;
template<class ObjectType, class DataType> class Slot;
/*!
\brief Signals and slots
A simple example where to objects A and B are communicating through signals and slots. Each object has one signal
and one slot. The A_signal is used to send a signal to the B_slot and vice versa. When a signal is received by the B_slot
it is forwarded to the function forward(). The class definition includes the definition of the signals, slots and
forward functions including a name and the type of data to transmit.
\code
#include "signals_slots.h"
class A;
class B;
class A {
public:
A(){
A_slot.forward(this, &A::member);
A_signal.set_name("A_signal");
A_signal.set_debug(true);
A_slot.set_name("A_slot");
N = 10;
}
Signal<int> A_signal;
Slot<A, double> A_slot;
private:
int N;
void member(const double x) {
if(N)
A_signal.arm(3.4, N--);
}
};
class B {
public:
B(){
B_slot.forward(this, &B::member);
B_signal.set_name("B_signal");
B_signal.set_debug();
B_slot.set_name("B_slot");
}
Signal<double> B_signal;
Slot<B, int> B_slot;
private:
void member(const int k){ B_signal.arm(23.2, M_PI); }
};
int main(){
A a; // class A does not know anything about class B.
B b; // class B does not know anything about class A.
a.A_signal.connect(&b.B_slot); // Connect a to b.
b.B_signal.connect(&a.A_slot); // Connect b to a.
a.A_signal.arm(56.2, 3); // First event in 56.2 seconds carrying data = 3
Event_Queue::start(); // start the event-based simulation
}
\endcode
*/
template<class DataType>
class Signal
{
public:
friend class Base_Slot<DataType>;
//! Default constructor
Signal(const std::string signal_name = "Unamed Signal", const bool single_shot = false, const bool enable_debug = false);
// Signal(const std::string signal_name = "Unamed Signal", const bool single_shot = false, const bool enable_debug = true);
//! Destructor
~Signal();
//! Connect a slot to the signal
void connect(Base_Slot<DataType>* slot);
//! Disconnect the slot from the signal
void disconnect(Base_Slot<DataType>* slot = NULL);
// Base_Event* arm(const Ttype delta_time, DataType signal); // Signal will trigger in 'delta_time' time units carrying data signal.
//! Issue a signal
Base_Event* operator()(DataType signal, const Ttype delta_time = 0);
//! cancel signal
void cancel();
//! set name of signal
void set_name(const std::string &signal_name);
//! Set debug mode. If true all signals are printed to stdout
void set_debug(const bool enable_debug = true);
//! ADD DOCUMENTATION HERE
void trigger(DataType u);
protected:
//! ADD DOCUMENTATION HERE
typedef typename std::list<Base_Slot<DataType>*, std::allocator< Base_Slot<DataType>* > >::iterator Base_Slot_Iterator;
//! ADD DOCUMENTATION HERE
void _disconnect(Base_Slot<DataType>* slot);
//! ADD DOCUMENTATION HERE
std::list<Base_Slot<DataType>*, std::allocator<Base_Slot<DataType>* > > connected_slots;
//! ADD DOCUMENTATION HERE
std::string name;
private:
bool armed;
bool debug;
bool single;
Data_Event<Signal, DataType> *e;
};
/*!
\brief Base Slot class
*/
template<class DataType>
class Base_Slot
{
public:
friend class Signal<DataType>;
//! Default Constructor
Base_Slot(const std::string slot_name = "Unamed Base_Slot");
//! Desctuctor
virtual ~Base_Slot();
//! set slot name
void set_name(const std::string &slot_name);
//! ADD DOCUMENTATION HERE
virtual void operator()(DataType signal) = 0;
protected:
// virtual void exec(DataType signal) = 0;
//! ADD DOCUMENTATION HERE
typedef typename std::list<Signal<DataType>*, std::allocator< Signal<DataType>* > >::iterator Signal_Iterator;
//! ADD DOCUMENTATION HERE
std::string name;
//! ADD DOCUMENTATION HERE
void _connect(Signal<DataType>* signal);
//! ADD DOCUMENTATION HERE
void _disconnect(Signal<DataType>* signal);
//! ADD DOCUMENTATION HERE
std::list<Signal<DataType>*, std::allocator<Signal<DataType>* > > connected_signals;
};
/*!
\brief Slot Class
*/
template<class ObjectType, class DataType>
class Slot : public Base_Slot<DataType>
{
public:
//! Default constructor
Slot(const std::string _name = "Unamed Slot");
//! ADD DOCUMENTATION HERE
void forward(ObjectType *object_pointer, void(ObjectType::*object_function_pointer)(DataType u));
//! Destructor
~Slot();
//! ADD DOCUMENTATION HERE
void operator()(DataType u);
//void exec(DataType signal);
private:
ObjectType *po;
void(ObjectType::*pm)(DataType signal);
};
/*!
*/
template<class ObjectType, class DataType>
class ATimer
{
public:
//! Default constructor
ATimer(const std::string Name = "Unamed ATimer") {
time_out_signal = new Signal<DataType>(Name, true);
time_out_slot = new Slot<ObjectType, DataType>(Name);
time_out_signal->connect(time_out_slot);
set_name(Name);
}
//! ADD DOCUMENTATION HERE
void forward(ObjectType *po, void(ObjectType::*pm)(DataType u)) { time_out_slot->forward(po, pm); }
//! ADD DOCUMENTATION HERE
void set(DataType u, const Ttype delta_t) {
time_out_signal->operator()(u, delta_t);
}
//! ADD DOCUMENTATION HERE
void cancel() { time_out_signal->cancel(); }
//! ADD DOCUMENTATION HERE
void set_name(const std::string Name) {
name = Name;
time_out_signal->set_name(name);
time_out_slot->set_name(name);
}
protected:
//! ADD DOCUMENTATION HERE
std::string name;
private:
Signal<DataType> *time_out_signal;
Slot<ObjectType, DataType> *time_out_slot;
};
/*!
TTimer is a class that can be set in order to be
remembered at a future instance of time. The
difference to "generic event" is the easy usage
that already take care about posting and canceling
events
@ingroup EventHandling
*/
template <class THandler>
class TTimer
{
public:
//! Default constructor
TTimer(THandler & handler, void (THandler::*handlerFunction)(Ttype time)) :
signal("timer_signal", true) {
fPending = false;
fExpirationTime = 0;
registered_handler = &handler;
registered_handler_function = handlerFunction;
slot.forward(this, &TTimer<THandler>::HandleProcessEvent);
slot.set_name("timer_slot");
signal.set_debug(false);
signal.connect(&slot);
}
//! Destructor
virtual ~TTimer() {
if (fPending)
signal.cancel();
}
//! ADD DOCUMENTATION HERE
void Set(Ttype time, bool relative = true) {
if (fPending)
signal.cancel();
fPending = true;
double current_time = Event_Queue::now();
double delta_time;
if (relative) {
fExpirationTime = current_time + time;
delta_time = time;
}
else {
fExpirationTime = time;
delta_time = time - current_time;
}
signal(fExpirationTime, delta_time);
}
//! ADD DOCUMENTATION HERE
void Reset() {
if (fPending) {
signal.cancel();
fPending = false; // TODO: Added this myself. Didn't work otherwise.
}
}
//! ADD DOCUMENTATION HERE
Ttype ExpirationTime() const {
it_assert(fPending, "TTimer<>::ExpirationTime: timer not set");
return fExpirationTime;
}
//! ADD DOCUMENTATION HERE
bool IsPending() const { return fPending; }
protected:
//! ADD DOCUMENTATION HERE
virtual void HandleProcessEvent(Ttype currentTime) {
fPending = false;
(*registered_handler.*registered_handler_function)(currentTime);
}
//! ADD DOCUMENTATION HERE
virtual void HandleCancelEvent(Ttype) {
if (fPending)
signal.cancel();
fPending = false;
}
//! Flag denoting if timer is set
bool fPending;
//! ADD DOCUMENTATION HERE
Ttype fExpirationTime;
private:
THandler *registered_handler;
void(THandler::*registered_handler_function)(Ttype expiry_time);
Signal<double> signal; // Used internally
Slot<TTimer, double> slot; // Used internally
};
// -----------------------------------------------------------------------------------------------
template<class DataType>
Signal<DataType>::Signal(const std::string signal_name, const bool single_shot, const bool enable_debug)
{
armed = false;
e = NULL;
single = single_shot;
set_name(signal_name);
set_debug(enable_debug);
}
template<class DataType>
Signal<DataType>::~Signal()
{
Base_Slot_Iterator
begin = connected_slots.begin(),
end = connected_slots.end(),
i;
for (i = begin; i != end; i++)
(*i)->_disconnect(this);
connected_slots.clear();
if (e != NULL) // Cancel a possibly pending event since we are about to die!
e->cancel();
}
template<class DataType>
void Signal<DataType>::set_name(const std::string &signal_name)
{
name = signal_name;
}
template<class DataType>
void Signal<DataType>::set_debug(const bool enable_debug)
{
debug = enable_debug;
}
template<class DataType>
void Signal<DataType>::connect(Base_Slot<DataType>* slot)
{
Base_Slot_Iterator
begin = connected_slots.begin(),
end = connected_slots.end(),
i;
bool is_already_connected = false;
for (i = begin; i != end; i++)
if ((*i) == slot)
is_already_connected = true;
if (!is_already_connected) { // Multiple connections is meaningless.
connected_slots.push_back(slot);
slot->_connect(this); // Needed if a connected slot is deleted during run time.
}
else {
std::cout << "Signal '" << name << "' and Slot '" << slot->name << "' are already connected. Multiple connections have no effect!" << std::endl;
}
}
template<class DataType>
void Signal<DataType>::disconnect(Base_Slot<DataType>* slot)
{
Base_Slot_Iterator
begin = connected_slots.begin(),
end = connected_slots.end(),
i;
for (i = begin; i != end; i++)
if ((*i) == slot) {
(*i)->_disconnect(this);
connected_slots.erase(i);
break;
}
}
template<class DataType>
Base_Event* Signal<DataType>::operator()(DataType signal, const Ttype delta_time)
{
// Signal will trigger in 'delta_time' time units.
if (single) { // We are operating in single-shot mode.
if (armed) { // Cancel and schedule again with the new 'delta_time'.
if (debug)
std::cout << "Warning: Changing time for Signal '" << name << "'." << std::endl;
cancel();
operator()(signal, delta_time);
}
else {
e = new Data_Event<Signal, DataType>(this, &Signal<DataType>::trigger, signal, delta_time);
armed = true;
Event_Queue::add(e);
}
}
else { // Continious mode (cancel() has no effect).
e = new Data_Event<Signal, DataType>(this, &Signal<DataType>::trigger, signal, delta_time);
armed = true;
Event_Queue::add(e);
}
return e;
}
template<class DataType>
void Signal<DataType>::cancel()
{
if (armed && single) {
e->cancel();
e = NULL;
armed = false;
}
}
template<class DataType>
void Signal<DataType>::trigger(DataType u)
{
armed = false;
e = NULL;
Base_Slot_Iterator
begin = connected_slots.begin(),
end = connected_slots.end(),
i;
for (i = begin; i != end; i++) { // Execute all the functions of the connected slots.
if (debug)
std::cout << "Time = " << Event_Queue::now() << ". Signal '" << name << "' was sent to Slot '" << (*i)->name << "'." << std::endl;
(*i)->operator()(u);
}
}
template<class DataType>
void Signal<DataType>::_disconnect(Base_Slot<DataType>* slot)
{
Base_Slot_Iterator
begin = connected_slots.begin(),
end = connected_slots.end(),
i;
for (i = begin; i != end; i++)
if ((*i) == slot) {
connected_slots.erase(i);
break;
}
}
template<class DataType>
Base_Slot<DataType>::Base_Slot(const std::string slot_name)
{
set_name(slot_name);
}
template<class DataType>
void Base_Slot<DataType>::set_name(const std::string &slot_name)
{
name = slot_name;
}
template<class DataType>
Base_Slot<DataType>::~Base_Slot()
{ // Notify all signals connect that we are being deleted ...
Signal_Iterator
begin = connected_signals.begin(),
end = connected_signals.end(),
i;
for (i = begin; i != end; i++)
(*i)->_disconnect(this);
connected_signals.clear();
}
template<class DataType>
void Base_Slot<DataType>::_connect(Signal<DataType>* signal)
{ // A signal is being connected to us.
connected_signals.push_back(signal);
}
template<class DataType>
void Base_Slot<DataType>::_disconnect(Signal<DataType>* signal)
{ // A signal is being disconnected from us.
Signal_Iterator
begin = connected_signals.begin(),
end = connected_signals.end(),
i;
for (i = begin; i != end; i++)
if ((*i) == signal) {
connected_signals.erase(i);
break;
}
}
template<class ObjectType, class DataType>
Slot<ObjectType, DataType>::Slot(const std::string slot_name) : Base_Slot<DataType>(slot_name)
{
pm = NULL;
po = NULL;
}
template<class ObjectType, class DataType>
Slot<ObjectType, DataType>::~Slot() {}
template<class ObjectType, class DataType>
void Slot<ObjectType, DataType>::forward(ObjectType *object_pointer, void(ObjectType::*object_function_pointer)(DataType u))
{
pm = object_function_pointer;
po = object_pointer;
}
// template<class ObjectType, class DataType>
// void Slot<ObjectType, DataType>::exec(DataType signal){
// if(pm&&po)
// (*po.*pm)(signal);
// }
template<class ObjectType, class DataType>
void Slot<ObjectType, DataType>::operator()(DataType signal)
{
if (pm&&po)
(*po.*pm)(signal);
}
//@}
} // namespace itpp
#endif
#endif // #ifndef SIGNAL_SLOT_H
|