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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
|
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2017 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
/*[[[cog
import cog
cog.outl("#if 0")
]]]*/
/*[[[end]]]*/
#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
/*[[[cog
import cog
cog.outl("#endif")
]]]*/
/*[[[end]]]*/
/*[[[cog
import cog
cog.outl("//***************************************************************************")
cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
cog.outl("//***************************************************************************")
]]]*/
/*[[[end]]]*/
//***************************************************************************
// To generate to header file, run this at the command line.
// Note: You will need Python and COG installed.
//
// python -m cogapp -d -e -ofsm.h -DHandlers=<n> fsm_generator.h
// Where <n> is the number of messages to support.
//
// e.g.
// To generate handlers for up to 16 events...
// python -m cogapp -d -e -ofsm.h -DHandlers=16 fsm_generator.h
//
// See generate.bat
//***************************************************************************
#ifndef ETL_FSM_INCLUDED
#define ETL_FSM_INCLUDED
#include "platform.h"
#include "array.h"
#include "nullptr.h"
#include "error_handler.h"
#include "exception.h"
#include "user_type.h"
#include "message_router.h"
#include "integral_limits.h"
#include "largest.h"
#include <stdint.h>
#include "private/minmax_push.h"
namespace etl
{
class fsm;
class hfsm;
/// Allow alternative type for state id.
#if !defined(ETL_FSM_STATE_ID_TYPE)
typedef uint_least8_t fsm_state_id_t;
#else
typedef ETL_FSM_STATE_ID_TYPE fsm_state_id_t;
#endif
// For internal FSM use.
typedef typename etl::larger_type<etl::message_id_t>::type fsm_internal_id_t;
#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION) // For C++17 and above
template <typename, typename, etl::fsm_state_id_t, typename...>
class fsm_state;
#else
/*[[[cog
import cog
cog.outl("template <typename, typename, etl::fsm_state_id_t,")
cog.out(" ")
for n in range(1, int(Handlers)):
cog.out("typename, ")
if n % 4 == 0:
cog.outl("")
cog.out(" ")
cog.outl("typename>")
cog.outl("class fsm_state;")
]]]*/
/*[[[end]]]*/
#endif
//***************************************************************************
/// Base exception class for FSM.
//***************************************************************************
class fsm_exception : public etl::exception
{
public:
fsm_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: etl::exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for null state pointer.
//***************************************************************************
class fsm_null_state_exception : public etl::fsm_exception
{
public:
fsm_null_state_exception(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:null state", ETL_FSM_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for invalid state id.
//***************************************************************************
class fsm_state_id_exception : public etl::fsm_exception
{
public:
fsm_state_id_exception(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:state id", ETL_FSM_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for incompatible state list.
//***************************************************************************
class fsm_state_list_exception : public etl::fsm_exception
{
public:
fsm_state_list_exception(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:state list", ETL_FSM_FILE_ID"C"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for incompatible order state list.
//***************************************************************************
class fsm_state_list_order_exception : public etl::fsm_exception
{
public:
fsm_state_list_order_exception(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:state list order", ETL_FSM_FILE_ID"D"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for forbidden state changes.
//***************************************************************************
class fsm_state_composite_state_change_forbidden : public etl::fsm_exception
{
public:
fsm_state_composite_state_change_forbidden(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:change in composite state forbidden", ETL_FSM_FILE_ID"E"), file_name_, line_number_)
{
}
};
//***************************************************************************
/// Exception for message received but not started.
//***************************************************************************
class fsm_not_started : public etl::fsm_exception
{
public:
fsm_not_started(string_type file_name_, numeric_type line_number_)
: etl::fsm_exception(ETL_ERROR_TEXT("fsm:not started", ETL_FSM_FILE_ID"F"), file_name_, line_number_)
{
}
};
namespace private_fsm
{
template <typename T = void>
class ifsm_state_helper
{
public:
// Pass this whenever no state change is desired.
// The highest unsigned value of fsm_state_id_t.
static ETL_CONSTANT fsm_state_id_t No_State_Change = etl::integral_limits<fsm_state_id_t>::max;
// Pass this when this event also needs to be passed to the parent.
static ETL_CONSTANT fsm_state_id_t Pass_To_Parent = No_State_Change - 1U;
// Pass this when this event should trigger a self transition.
static ETL_CONSTANT fsm_state_id_t Self_Transition = No_State_Change - 2U;
};
template <typename T>
ETL_CONSTANT fsm_state_id_t ifsm_state_helper<T>::No_State_Change;
template <typename T>
ETL_CONSTANT fsm_state_id_t ifsm_state_helper<T>::Pass_To_Parent;
template <typename T>
ETL_CONSTANT fsm_state_id_t ifsm_state_helper<T>::Self_Transition;
}
//***************************************************************************
/// Interface class for FSM states.
//***************************************************************************
class ifsm_state : public private_fsm::ifsm_state_helper<>
{
public:
/// Allows ifsm_state functions to be private.
friend class etl::fsm;
friend class etl::hfsm;
using private_fsm::ifsm_state_helper<>::No_State_Change;
using private_fsm::ifsm_state_helper<>::Pass_To_Parent;
using private_fsm::ifsm_state_helper<>::Self_Transition;
#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION) // For C++17 and above
template <typename, typename, etl::fsm_state_id_t, typename...>
friend class fsm_state;
#else
/*[[[cog
import cog
cog.outl(" template <typename, typename, etl::fsm_state_id_t,")
cog.out(" ")
for n in range(1, int(Handlers)):
cog.out("typename, ")
if n % 4 == 0:
cog.outl("")
cog.out(" ")
cog.outl("typename>")
]]]*/
/*[[[end]]]*/
friend class etl::fsm_state;
#endif
//*******************************************
/// Gets the id for this state.
//*******************************************
etl::fsm_state_id_t get_state_id() const
{
return state_id;
}
//*******************************************
/// Adds a child to this state.
/// Only of use when part of an HFSM.
//*******************************************
void add_child_state(etl::ifsm_state& state)
{
ETL_ASSERT(state.p_parent == ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
state.p_parent = this;
if (p_default_child == ETL_NULLPTR)
{
p_active_child = &state;
p_default_child = &state;
}
}
//*******************************************
/// Adds a list of child states.
/// Only of use when part of an HFSM.
//*******************************************
template <typename TSize>
void set_child_states(etl::ifsm_state** state_list, TSize size)
{
p_active_child = ETL_NULLPTR;
p_default_child = ETL_NULLPTR;
for (TSize i = 0; i < size; ++i)
{
ETL_ASSERT(state_list[i] != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
add_child_state(*state_list[i]);
}
}
protected:
//*******************************************
/// Constructor.
//*******************************************
ifsm_state(etl::fsm_state_id_t state_id_)
: state_id(state_id_),
p_context(ETL_NULLPTR),
p_parent(ETL_NULLPTR),
p_active_child(ETL_NULLPTR),
p_default_child(ETL_NULLPTR)
{
}
//*******************************************
/// Destructor.
//*******************************************
virtual ~ifsm_state()
{
}
//*******************************************
etl::fsm& get_fsm_context() const
{
return *p_context;
}
private:
virtual fsm_state_id_t process_event(const etl::imessage& message) = 0;
virtual fsm_state_id_t on_enter_state() { return No_State_Change; } // By default, do nothing.
virtual void on_exit_state() {} // By default, do nothing.
//*******************************************
void set_fsm_context(etl::fsm& context)
{
p_context = &context;
}
// The state id.
const etl::fsm_state_id_t state_id;
// A pointer to the FSM context.
etl::fsm* p_context;
// A pointer to the parent.
ifsm_state* p_parent;
// A pointer to the active child.
ifsm_state* p_active_child;
// A pointer to the default active child.
ifsm_state* p_default_child;
// Disabled.
ifsm_state(const ifsm_state&);
ifsm_state& operator =(const ifsm_state&);
};
//***************************************************************************
/// The FSM class.
//***************************************************************************
class fsm : public etl::imessage_router
{
public:
friend class etl::hfsm;
using imessage_router::receive;
//*******************************************
/// Constructor.
//*******************************************
fsm(etl::message_router_id_t id)
: imessage_router(id)
, p_state(ETL_NULLPTR)
, state_list(ETL_NULLPTR)
, number_of_states(0U)
{
}
//*******************************************
/// Set the states for the FSM
//*******************************************
template <typename TSize>
void set_states(etl::ifsm_state** p_states, TSize size)
{
state_list = p_states;
number_of_states = etl::fsm_state_id_t(size);
ETL_ASSERT(number_of_states > 0, ETL_ERROR(etl::fsm_state_list_exception));
ETL_ASSERT(number_of_states < ifsm_state::No_State_Change, ETL_ERROR(etl::fsm_state_list_exception));
for (etl::fsm_state_id_t i = 0; i < size; ++i)
{
ETL_ASSERT(state_list[i] != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
ETL_ASSERT(state_list[i]->get_state_id() == i, ETL_ERROR(etl::fsm_state_list_order_exception));
state_list[i]->set_fsm_context(*this);
}
}
//*******************************************
/// Starts the FSM.
/// Can only be called once.
/// Subsequent calls will do nothing.
///\param call_on_enter_state If true will call on_enter_state() for the first state. Default = true.
//*******************************************
virtual void start(bool call_on_enter_state = true)
{
// Can only be started once.
if (p_state == ETL_NULLPTR)
{
p_state = state_list[0];
ETL_ASSERT(p_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
if (call_on_enter_state)
{
etl::fsm_state_id_t next_state_id;
etl::ifsm_state* p_last_state;
do
{
p_last_state = p_state;
next_state_id = p_state->on_enter_state();
if (next_state_id != ifsm_state::No_State_Change)
{
ETL_ASSERT(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
p_state = state_list[next_state_id];
}
} while (p_last_state != p_state);
}
}
}
//*******************************************
/// Top level message handler for the FSM.
//*******************************************
void receive(const etl::imessage& message) ETL_OVERRIDE
{
if (is_started())
{
etl::fsm_state_id_t next_state_id = p_state->process_event(message);
if (have_changed_state(next_state_id))
{
ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
etl::ifsm_state* p_next_state = state_list[next_state_id];
do
{
p_state->on_exit_state();
p_state = p_next_state;
next_state_id = p_state->on_enter_state();
if (have_changed_state(next_state_id))
{
ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
p_next_state = state_list[next_state_id];
}
} while (p_next_state != p_state); // Have we changed state again?
}
else if (is_self_transition(next_state_id))
{
p_state->on_exit_state();
p_state->on_enter_state();
}
}
else
{
ETL_ASSERT_FAIL(ETL_ERROR(etl::fsm_not_started));
}
}
using imessage_router::accepts;
//*******************************************
/// Does this FSM accept the message id?
/// Yes, it accepts everything!
//*******************************************
bool accepts(etl::message_id_t) const ETL_OVERRIDE
{
return true;
}
//*******************************************
/// Gets the current state id.
//*******************************************
etl::fsm_state_id_t get_state_id() const
{
ETL_ASSERT(p_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
return p_state->get_state_id();
}
//*******************************************
/// Gets a reference to the current state interface.
//*******************************************
ifsm_state& get_state()
{
ETL_ASSERT(p_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
return *p_state;
}
//*******************************************
/// Gets a const reference to the current state interface.
//*******************************************
const ifsm_state& get_state() const
{
ETL_ASSERT(p_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
return *p_state;
}
//*******************************************
/// Checks if the FSM has been started.
//*******************************************
bool is_started() const
{
return p_state != ETL_NULLPTR;
}
//*******************************************
/// Reset the FSM to pre-started state.
///\param call_on_exit_state If true will call on_exit_state() for the current state. Default = false.
//*******************************************
virtual void reset(bool call_on_exit_state = false)
{
if ((p_state != ETL_NULLPTR) && call_on_exit_state)
{
p_state->on_exit_state();
}
p_state = ETL_NULLPTR;
}
//********************************************
ETL_DEPRECATED bool is_null_router() const ETL_OVERRIDE
{
return false;
}
//********************************************
bool is_producer() const ETL_OVERRIDE
{
return true;
}
//********************************************
bool is_consumer() const ETL_OVERRIDE
{
return true;
}
private:
//********************************************
bool have_changed_state(etl::fsm_state_id_t next_state_id) const
{
return (next_state_id != p_state->get_state_id()) &&
(next_state_id != ifsm_state::No_State_Change) &&
(next_state_id != ifsm_state::Self_Transition);
}
//********************************************
bool is_self_transition(etl::fsm_state_id_t next_state_id) const
{
return (next_state_id == ifsm_state::Self_Transition);
}
etl::ifsm_state* p_state; ///< A pointer to the current state.
etl::ifsm_state** state_list; ///< The list of added states.
etl::fsm_state_id_t number_of_states; ///< The number of states.
};
//*************************************************************************************************
// For C++17 and above.
//*************************************************************************************************
#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION) // For C++17 and above
//***************************************************************************
// The definition for all types.
//***************************************************************************
template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_, typename... TMessageTypes>
class fsm_state : public ifsm_state
{
public:
static ETL_CONSTANT etl::fsm_state_id_t STATE_ID = STATE_ID_;
fsm_state()
: ifsm_state(STATE_ID)
{
}
protected:
~fsm_state()
{
}
TContext& get_fsm_context() const
{
return static_cast<TContext&>(ifsm_state::get_fsm_context());
}
private:
//********************************************
struct result_t
{
bool was_handled;
etl::fsm_state_id_t state_id;
};
//********************************************
etl::fsm_state_id_t process_event(const etl::imessage& message)
{
etl::fsm_state_id_t new_state_id;
const bool was_handled = (process_event_type<TMessageTypes>(message, new_state_id) || ...);
if (!was_handled || (new_state_id == Pass_To_Parent))
{
new_state_id = (p_parent != nullptr) ? p_parent->process_event(message) : static_cast<TDerived*>(this)->on_event_unknown(message);
}
return new_state_id;
}
//********************************************
template <typename TMessage>
bool process_event_type(const etl::imessage& msg, etl::fsm_state_id_t& new_state_id)
{
if (TMessage::ID == msg.get_message_id())
{
new_state_id = static_cast<TDerived*>(this)->on_event(static_cast<const TMessage&>(msg));
return true;
}
else
{
return false;
}
}
};
/// Definition of STATE_ID
template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_, typename... TMessageTypes>
ETL_CONSTANT etl::fsm_state_id_t fsm_state<TContext, TDerived, STATE_ID_, TMessageTypes...>::STATE_ID;
#else
//*************************************************************************************************
// For C++14 and below.
//*************************************************************************************************
/*[[[cog
import cog
################################################
# The first definition for all of the events.
################################################
cog.outl("//***************************************************************************")
cog.outl("// The definition for all %s message types." % Handlers)
cog.outl("//***************************************************************************")
cog.outl("template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_, ")
cog.out(" ")
for n in range(1, int(Handlers)):
cog.out("typename T%s = void, " % n)
if n % 4 == 0:
cog.outl("")
cog.out(" ")
cog.outl("typename T%s = void>" % Handlers)
cog.outl("class fsm_state : public ifsm_state")
cog.outl("{")
cog.outl("public:")
cog.outl("")
cog.outl(" static ETL_CONSTANT etl::fsm_state_id_t STATE_ID = STATE_ID_;")
cog.outl("")
cog.outl(" fsm_state()")
cog.outl(" : ifsm_state(STATE_ID)")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl("protected:")
cog.outl("")
cog.outl(" ~fsm_state()")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl(" TContext& get_fsm_context() const")
cog.outl(" {")
cog.outl(" return static_cast<TContext&>(ifsm_state::get_fsm_context());")
cog.outl(" }")
cog.outl("")
cog.outl("private:")
cog.outl("")
cog.outl(" etl::fsm_state_id_t process_event(const etl::imessage& message)")
cog.outl(" {")
cog.outl(" etl::fsm_state_id_t new_state_id;")
cog.outl(" etl::message_id_t event_id = message.get_message_id();")
cog.outl("")
cog.outl(" switch (event_id)")
cog.outl(" {")
for n in range(1, int(Handlers) + 1):
cog.out(" case T%d::ID:" % n)
cog.out(" new_state_id = static_cast<TDerived*>(this)->on_event(static_cast<const T%d&>(message));" % n)
cog.outl(" break;")
cog.out(" default:")
cog.out(" new_state_id = p_parent ? p_parent->process_event(message) : static_cast<TDerived*>(this)->on_event_unknown(message);")
cog.outl(" break;")
cog.outl(" }")
cog.outl("")
cog.outl(" return (new_state_id != Pass_To_Parent) ? new_state_id : (p_parent ? p_parent->process_event(message) : No_State_Change);")
cog.outl(" }")
cog.outl("};")
####################################
# All of the other specialisations.
####################################
for n in range(int(Handlers) - 1, 0, -1):
cog.outl("")
cog.outl("//***************************************************************************")
if n == 1:
cog.outl("// Specialisation for %d message type." % n)
else:
cog.outl("// Specialisation for %d message types." % n)
cog.outl("//***************************************************************************")
cog.outl("template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_, ")
cog.out(" ")
for t in range(1, n):
cog.out("typename T%d, " % t)
if t % 4 == 0:
cog.outl("")
cog.out(" ")
cog.outl("typename T%d>" % n)
cog.out("class fsm_state<TContext, TDerived, STATE_ID_, ")
for t in range(1, n + 1):
cog.out("T%d, " % t)
if t % 16 == 0:
cog.outl("")
cog.out(" ")
for t in range(n + 1, int(Handlers)):
cog.out("void, ")
if t % 16 == 0:
cog.outl("")
cog.out(" ")
cog.outl("void> : public ifsm_state")
cog.outl("{")
cog.outl("public:")
cog.outl("")
cog.outl(" static ETL_CONSTANT etl::fsm_state_id_t STATE_ID = STATE_ID_;")
cog.outl("")
cog.outl(" fsm_state()")
cog.outl(" : ifsm_state(STATE_ID)")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl("protected:")
cog.outl("")
cog.outl(" ~fsm_state()")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl(" TContext& get_fsm_context() const")
cog.outl(" {")
cog.outl(" return static_cast<TContext&>(ifsm_state::get_fsm_context());")
cog.outl(" }")
cog.outl("")
cog.outl("private:")
cog.outl("")
cog.outl(" etl::fsm_state_id_t process_event(const etl::imessage& message)")
cog.outl(" {")
cog.outl(" etl::fsm_state_id_t new_state_id;")
cog.outl(" etl::message_id_t event_id = message.get_message_id();")
cog.outl("")
cog.outl(" switch (event_id)")
cog.outl(" {")
for n in range(1, n + 1):
cog.out(" case T%d::ID:" % n)
cog.out(" new_state_id = static_cast<TDerived*>(this)->on_event(static_cast<const T%d&>(message));" % n)
cog.outl(" break;")
cog.out(" default:")
cog.out(" new_state_id = p_parent ? p_parent->process_event(message) : static_cast<TDerived*>(this)->on_event_unknown(message);")
cog.outl(" break;")
cog.outl(" }")
cog.outl("")
cog.outl(" return (new_state_id != Pass_To_Parent) ? new_state_id : (p_parent ? p_parent->process_event(message) : No_State_Change);")
cog.outl(" }")
cog.outl("};")
####################################
# Specialisation for zero messages.
####################################
cog.outl("")
cog.outl("//***************************************************************************")
cog.outl("// Specialisation for 0 message types.")
cog.outl("//***************************************************************************")
cog.outl("template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_>")
cog.out("class fsm_state<TContext, TDerived, STATE_ID_, ")
for t in range(1, int(Handlers)):
cog.out("void, ")
if t % 16 == 0:
cog.outl("")
cog.out(" ")
cog.outl("void> : public ifsm_state")
cog.outl("{")
cog.outl("public:")
cog.outl("")
cog.outl(" static ETL_CONSTANT etl::fsm_state_id_t STATE_ID = STATE_ID_;")
cog.outl("")
cog.outl(" fsm_state()")
cog.outl(" : ifsm_state(STATE_ID)")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl("protected:")
cog.outl("")
cog.outl(" ~fsm_state()")
cog.outl(" {")
cog.outl(" }")
cog.outl("")
cog.outl(" TContext& get_fsm_context() const")
cog.outl(" {")
cog.outl(" return static_cast<TContext&>(ifsm_state::get_fsm_context());")
cog.outl(" }")
cog.outl("private:")
cog.outl("")
cog.outl(" etl::fsm_state_id_t process_event(const etl::imessage& message)")
cog.outl(" {")
cog.outl(" return p_parent ? p_parent->process_event(message) : static_cast<TDerived*>(this)->on_event_unknown(message);")
cog.outl(" }")
cog.outl("};")
cog.outl("")
cog.outl("template <typename TContext, typename TDerived, etl::fsm_state_id_t STATE_ID_, ")
cog.out(" ")
for n in range(1, int(Handlers)):
cog.out("typename T%s, " % n)
if n % 4 == 0:
cog.outl("")
cog.out(" ")
cog.outl("typename T%s>" % Handlers)
cog.out("ETL_CONSTANT etl::fsm_state_id_t fsm_state<TContext, TDerived, STATE_ID_, ")
for n in range(1, int(Handlers)):
cog.out("T%s, " % n)
cog.outl("T%s>::STATE_ID;" % Handlers)
]]]*/
/*[[[end]]]*/
#endif
}
#include "private/minmax_pop.h"
#endif
|