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 1048969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:32:28+00:00 2026-05-16T16:32:28+00:00

I have a state machine design that needs to support playback. We have states

  • 0

I have a state machine design that needs to support playback. We have states that perform actions and sometimes need to generate random numbers. In case the program shuts down while in the middle of the FSM’s execution, the program needs to playback the whole FSM using the same random numbers as before.

For a basic example, let’s say I had three states: A, B, and C. The FSM will call a state’s Execute() function. At the end of the function, the state will post an event, and the FSM will determine which state to go to next. In state A, it will call rand(). If the number is even, it will post an event to go to state B, otherwise state C should be the next state.

void StateA::Execute(IEventQueue& rQueue)
{
    int num = rand();
    if( num % 2 == 0 )
    {
        rQueue.PostEvent("GoToStateB");
    }
    else
    {
        rQueue.PostEvent("GoToStateC");
    }
}

If the random number is 69, then it should go to state C. While in state C, it’s possible that the program might quit. When the program starts up again, it should playback the state machine. Obviously, for this to work correctly, it can’t generate a completely new random number, it needs to use 69 again for accurate playback.

I have a file stream interface that I can use for saving data to a file, but the code is a little ugly:

void StateA::Execute(IEventQueue& rQueue, IFileStream& rStream)
{

    int num = 0;

    // fails if there's no more data to read
    bool bSuccess = rStream.ReadInt(num);
    if (!bSucess)
    {
        num = rand();
        rStream.WriteInt(num);
    }

    // same code as before
}

My only problem with this solution is that I don’t care for having to check the stream for data first and then conditionally write to the same stream.

I thought about hiding it like this:

void StateA::Execute(IEventQueue& rQueue, IStream& rStream)
{

    int num = 0;

    num = rand();
    rStream & num;

    // same code as before
}

Inside IStream, operator& (probably not the best use of overloading) would actually try to read an int from the stream. If that stream was empty, it would then write it instead. Like before, the behavior would be: read first until the end of stream, and then start appending.

So I guess my question is: is there a common idiom for this type of playback that I might be overlooking? Does anyone have alternate suggestions? I feel like I’m starting to over-complicate the design a bit.

Thanks!

  • 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-16T16:32:29+00:00Added an answer on May 16, 2026 at 4:32 pm

    Why have the states interact directly with the filestream? Single Responsibility says we should have a class who’s job it is to provide the proper number based on some logic.

    struct INumberSource {
        virtual int GenNextNumber() = 0;
    }
    
    // My job is to provide numbers from an RNG
    struct RNGNumberSource : public INumberSource {
        virtual int GenNextNumber() {
            return rand();
        }
    }
    
    // My job is to write any numbers sourced through me to a file
    // I delegate to another source to get an actual number
    class FileStreamTrackingNumberSource : INumberSource {
    public:
        FileStreamTrackingNumberSource(INumberSource& source, IFileStream& stream)
            : altSource(source), fileStream(stream) { }
    
        virtual int GenNextNumber() {
            int num = altSource.GenNextNumber();
            fileStream.WriteInt(num);
            return num;
        }
    private:
        INumberSource altSource;
        IFileStream& fileStream;
    }
    
    // My job is to source numbers from a file stream delegating to an
    // alternate source when I run out
    class FileStreamNumberSource : public INumberSource {
    public:
        FileStreamNumberSource(INumberSource& source, IFileStream& stream)
            : altSource(source), fileStream(stream), failedRead(false) { }
    
        virtual int GenNextNumber() {
            int num = 0;
    
            if(failedRead || !(failedRead = fileStream.ReadInt(num))) {
                num = altSource.GenNextNumber();
            }
    
            return num;
        }
    
    private:
        INumberSource& altSource;
        IFileStream& fileStream;
        bool failedRead;
    }
    

    So in your case you would provide an IFileStream and RNGNumberSource to a FileStreamTrackingNumberSource and provide that and the same IFileStream to a FileStreamNumberSource. That FileStreamNumberSource is what you would give to your State’s INumberSource parameter.

    Assuming you only needed the number to choose the next state then your state code could look like this:

    void StateA::Execute(IEventQueue& rQueue, INumberSource& numberSource)
    {
        if( numberSource.GenNextNumber() % 2 == 0 )
        {
            rQueue.PostEvent("GoToStateB");
        }
        else
        {
            rQueue.PostEvent("GoToStateC");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ state machine implemented using the State design pattern. Each state
I have a little problem that involves modeling a state machine. I have managed
I have an application that's a mix of Java and C++ on Solaris. The
I work for a large state government agency that is a tad behind the
I've recently returned to C++ development after a hiatus, and have a question regarding
I am new to WCF services. I have been working with WCF for over
I am grappling with a database design issue at the moment, I will present
I'm revisiting a communications protocol parser design for a stream of bytes (serial data,
In an embedded application programmed on C on ARM7 (with portability requirements), currently using
I've been using WWF for a while as part of an internal call center

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.