Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #67206
| From | "Rick C. Hodgin" <rick.c.hodgin@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Tricky! |
| Date | 2019-07-30 11:20 -0400 |
| Organization | Liberty Software Foundation |
| Message-ID | <qhpn2d$9es$1@dont-email.me> (permalink) |
| References | <qhmmgr$dih$1@news.albasani.net> <7995a414-14ec-47ae-8afc-9ebcc520e30d@googlegroups.com> <qhn54k$dk6$1@dont-email.me> <86imrj8szn.fsf@mailhub.linuxsc.com> |
On 7/30/2019 11:03 AM, Tim Rentsch wrote:
> That depends on what is meant by "practical". It can be done
> like this:
>
> #include <iostream>
>
> class State {
> using Nexter = ...;
>
> public:
> State(){}
> Nexter r0(){ return &State::r1; }
> Nexter r1(){ return &State::r2; }
> Nexter r2(){ return &State::rn; }
> Nexter rn(){ return 0; }
> };
>
> void
> run_state_machine(){
> State *s = new State;
>
> for( auto f = &State::r0; f; f = (s->*f)() ){
> ::std::cout << "Got one\n";
> }
> }
A better solution might be a state machine that can iterate N
times setup at instantiation, and then optionally reset when
exhausted so it can be re-entered the second time (untested,
but generally speaking this concept should work):
#include <iostream>
class State
{
using Nexter = ...;
public:
State(int nStateCount)
{
nState_ = -1;
nStateMax_ = max(1, nStateCount);
}
Nexter next(bool lReset = false)
{
if (++nState < nStateMax)
return &this->next; // Return self
// When we get here, we're done with this state traversal
if (lReset)
nState = -1; // Prepare for the next one
// Indicate this state is exhausted
return null;
}
void reset(int nState = -1)
{
nState_ = max(-1, min(nState, nStateMax));
}
int state(void) { return nState_; }
int max(void) { return nStateMax_; }
private:
int nState_;
int nStateMax_;
};
void run_state_machine()
{
State* s = new State(4);
...
--
Rick C. Hodgin
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Tricky! Bonita Montero <Bonita.Montero@gmail.com> - 2019-07-29 13:49 +0200
Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-07-29 06:30 -0700
Re: Tricky! "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-07-29 17:59 +0200
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-07-30 08:03 -0700
Re: Tricky! "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-07-30 11:20 -0400
Re: Tricky! "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-07-30 18:00 +0200
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-10 14:19 -0700
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-07-30 07:31 -0700
Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-07-30 09:22 -0700
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-10 14:45 -0700
Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-08-11 03:23 -0700
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-17 22:02 -0700
Re: Tricky! Öö Tiib <ootiib@hot.ee> - 2019-08-17 23:53 -0700
Re: Tricky! Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-18 05:22 -0700
Re: Tricky! Sam <sam@email-scan.com> - 2019-07-29 20:51 -0400
Re: Tricky! Sam <sam@email-scan.com> - 2019-07-29 20:52 -0400
Re: Tricky! Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-07-30 13:58 +0100
Re: Tricky! Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-11 09:06 +0200
csiph-web