Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1008921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:50:47+00:00 2026-05-16T08:50:47+00:00

I’m a bit confused about how to implement my state machine. I already know

  • 0

I’m a bit confused about how to implement my state machine.
I already know it’s hierarchical since some states share the same action.
I determine what I need to do by these parameters:

  • Class (Values are: Base, Derived, Specific)
  • OpCode
  • Parameter 1 – optional
  • Parameter 2 – optional

My hierarchy is determined by the Class and the OpCode represents the action.
Derived can use the OpCodes of Base and Specific can use OpCodes of both Base and Derived.
The naive implementation is the following:

void (*const state_table [MAX_CLASSES][MAX_OPCODES]) (state *) {
  {base_state1, base_state2, NULL, NULL},
  {base_state1, base_state2, derived_state1, NULL},
  {base_state1,base_state2, derived_state1, specific_state3},
};

void dispatch(state *s)
{
  if (state_table[s->Class][s->OpCode] != NULL)
    state_table[s->Class][s->OpCode](s);
}

This will turn unmaintainable really quick.
Is there another way to map the state to a superclass?

EDIT:
Further calcualtion leads me to think that I’ll probably use most if not all OpCodes but I will not use all of the Classes available to me.
Another clarification:
Some OpCodes might be shared through multiple derived and base Classes.
For example:

  • I have a Class called Any
    which is a Base class. It has the
    OpCodes: STATE_ON, STATE_OFF, STATE_SET.
  • I have another Class called
    MyGroup which is a Derived class. It has the OpCodes:
    STATE_FLIP, STATE_FLOP.

  • The third Class is a Specific
    class called ThingInMyGroup which
    has the OpCode:
    STATE_FLIP_FLOP_AND_FLOOP.

So a message with class Any is sent from the server, recieved in all clients and processed.

A message with class MyGroup is sent from the server, recieved in all clients and processed only on clients that belong to MyGroup, any OpCodes that are valid for the Any class are valid for the MyGroup class.

A message with class ThingInMyGroup is sent from the server, recieved in all clients and processed only on clients that belong to MyGroup and are a ThingInMyGroup*, any **OpCodes that are valid for the Any class and MyGroup class are valid for the ThingInMyGroup class.

After a message is received the client will ACK/NACK accordingly.

I prefer not to use switch cases or const arrays as they will become unmaintainable when they get bigger.
I need a flexible design that allows me:

  1. To specify which OpCodes are available
    for each Class.
  2. To specify a superclass for each Class and through that specification to allow me to call the function pointer that is represented by the current OpCode.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:50:48+00:00Added an answer on May 16, 2026 at 8:50 am

    There are several ways to deal with this. Here is one:

    edit — with general purpose hierarchy added

    typedef unsigned op_code_type;
    typedef void (*dispatch_type)(op_code_type);
    typedef struct hierarchy_stack hierarchy_stack;
    struct hierarchy_stack {
           dispatch_type func;
           hierarchy_stack *tail;
    };
    
    void dispatch(state *s, hierarchy_stack *stk) {
        if (!stk) {
              printf("this shouldn't have happened");
        } else {
              stk->func(s, stk->tail);
        }
    }
    
    void Base(state *s, hierarchy_stack *stk ) {
        switch (s->OpCode) {
              case bstate1:
                   base_state1(s);
                   break;
              case bstate2:
                   base_state(2);
                   break;
              default:
                   dispatch(s, stk);
        }
    }
    void Derived(state *s, hierarchy_stack *stk ) {
        switch(s->opcode) {
               case dstate1:
                    deriveds_state1(s);
                    break;
               default:
                    dispatch(s, stk);
        }
    }
    ... 
    

    NOTE : All function calls are tail calls.

    This localizes your “class”es a good bit so that if you decide that Derived needs 100 more methods/opcodes then you only have to edit methods and the enum (or whatever) that you use to define opcodes.

    Another, more dynamic way, to deal with this would be to have a parent pointer within each “class” that pointed to the “class” that would handle anything that it could not handle.

    The 2D table approach is fast and flexible (Derived could have a different handler than Base for opcode 0), but it grows fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.