statemap.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef _H_STATEMAP
  2. #define _H_STATEMAP
  3. /*
  4. * The contents of this file are subject to the Mozilla Public
  5. * License Version 1.1 (the "License"); you may not use this file
  6. * except in compliance with the License. You may obtain a copy of
  7. * the License at http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS
  10. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. * implied. See the License for the specific language governing
  12. * rights and limitations under the License.
  13. *
  14. * The Original Code is State Machine Compiler (SMC).
  15. *
  16. * The Initial Developer of the Original Code is Charles W. Rapp.
  17. *
  18. * Port to C by Francois Perrad, francois.perrad@gadz.org
  19. * Copyright 2004, Francois Perrad.
  20. * All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Description
  25. *
  26. * RCS ID
  27. * $Id: statemap.h,v 1.4 2009/03/01 18:20:40 cwrapp Exp $
  28. *
  29. * Change Log
  30. * $Log: statemap.h,v $
  31. * Revision 1.4 2009/03/01 18:20:40 cwrapp
  32. * Preliminary v. 6.0.0 commit.
  33. *
  34. * Revision 1.3 2008/05/30 21:14:48 fperrad
  35. * - define TRACE only when undefined
  36. *
  37. * Revision 1.2 2007/08/05 12:58:11 cwrapp
  38. * Version 5.0.1 check-in. See net/sf/smc/CODE_README.txt for more information.
  39. *
  40. * Revision 1.1 2005/06/16 18:08:17 fperrad
  41. * Added C, Perl & Ruby.
  42. *
  43. */
  44. #include <stdio.h>
  45. #include <string.h>
  46. #ifndef TRACE
  47. #define TRACE printf
  48. #endif
  49. #define STATE_MEMBERS \
  50. const char *_name; \
  51. int _id;
  52. struct State {
  53. STATE_MEMBERS
  54. };
  55. #define getName(state) \
  56. (state)->_name
  57. #define getId(state) \
  58. (state)->_id
  59. #define State_Default(fsm) \
  60. assert(0)
  61. #define FSM_MEMBERS(app) \
  62. const struct app##State * _state; \
  63. const struct app##State * _previous_state; \
  64. const struct app##State ** _stack_start; \
  65. const struct app##State ** _stack_curr; \
  66. const struct app##State ** _stack_max; \
  67. const char * _transition; \
  68. int _debug_flag;
  69. struct FSMContext {
  70. FSM_MEMBERS(_)
  71. };
  72. #define FSM_INIT(fsm, state) \
  73. (fsm)->_state = (state); \
  74. (fsm)->_previous_state = NULL; \
  75. (fsm)->_stack_start = NULL; \
  76. (fsm)->_stack_curr = NULL; \
  77. (fsm)->_stack_max = NULL; \
  78. (fsm)->_transition = NULL; \
  79. (fsm)->_debug_flag = 0
  80. #define FSM_STACK(fsm, stack) \
  81. (fsm)->_stack_start = &(stack)[0]; \
  82. (fsm)->_stack_curr = &(stack)[0]; \
  83. (fsm)->_stack_max = &(stack)[0] + (sizeof(stack) / sizeof(void*))
  84. #define getState(fsm) \
  85. (fsm)->_state
  86. #define clearState(fsm) \
  87. (fsm)->_previous_state = (fsm)->_state; \
  88. (fsm)->_state = NULL
  89. #define setState(fsm, state) \
  90. (fsm)->_state = (state); \
  91. if ((fsm)->_debug_flag != 0) { \
  92. TRACE("NEW STATE : %s\n", getName(state)); \
  93. }
  94. #define pushState(fsm, state) \
  95. if ((fsm)->_stack_curr >= (fsm)->_stack_max) { \
  96. assert(0 == "STACK OVERFLOW"); \
  97. } \
  98. *((fsm)->_stack_curr) = (fsm)->_state; \
  99. (fsm)->_stack_curr ++; \
  100. (fsm)->_state = state; \
  101. if ((fsm)->_debug_flag != 0) { \
  102. TRACE("PUSH TO STATE: %s\n", getName(state)); \
  103. }
  104. #define popState(fsm) \
  105. (fsm)->_stack_curr --; \
  106. (fsm)->_state = *((fsm)->_stack_curr); \
  107. if ((fsm)->_debug_flag != 0) { \
  108. TRACE("POP TO STATE : %s\n", getName((fsm)->_state)); \
  109. }
  110. #define emptyStateStack(fsm) \
  111. (fsm)->_stack_curr = (fsm)->_stack_start
  112. #define setTransition(fsm, transition) \
  113. (fsm)->_transition = (transition)
  114. #define getTransition(fsm) \
  115. (fsm)->_transition
  116. #define getDebugFlag(fsm) \
  117. (fsm)->_debug_flag
  118. #define setDebugFlag(fsm, flag) \
  119. (fsm)->_debug_flag = (flag)
  120. #endif