Giter VIP home page Giter VIP logo

xstate-cpp's Introduction

xstate-cpp

I needed xstate in C++. Here we are

Features

  • Basic stuff (I dunno I'm new here I just wanted a state machine for myself โ˜ƒ)
  • Nested state machines just landed in by #5 - see ./example.cpp for an example.
  • Others coming in real soon. Actually, even sooner, if you wanna contribute:P

Installation

  • platformio
platformio lib install --save XState-cpp

For everything else, you need to compile all .cpp files - makefile coming soon.

  • git + submodules
git submodules add https://github.com/sarpik/xstate-cpp.git
# or:  git submodules add [email protected]/sarpik/xstate-cpp.git

git submodules update --init --recursive

g++ -std=c++11 ./xstate-cpp/src/*.cpp ./xstate-cpp/example.cpp -o example.out
./example.out
  • git
git clone https://github.com/sarpik/xstate-cpp.git
# or:  git clone [email protected]:sarpik/xstate-cpp.git

g++ -std=c++11 ./xstate-cpp/src/*.cpp ./xstate-cpp/example.cpp -o example.out
./example.out

obviously, you subsitute example.cpp with your own source files & get some spicy state machines. ๐ŸŒถ

Usage

See also ./example.cpp

#include <iostream>
#include "xstate.h"

using namespace std;
using namespace xs;

int main() {
  StateMachine machine = {
    .id = "light",
    .initial = "green",
    .states = {
      {
        "green" ,
        { .on = { { "TIMER", "yellow" } } }
      },
      {
        "yellow",
        { .on = { { "TIMER", "red"    } } }
      },
      {
        "red"   ,
        { .on = { { "TIMER", "green"  } } }
      }
    }
  };

  Interpreter *toggleMachine = interpret(machine)
    ->logInfo()
    ->onStart([]() {
      cout << "let's go!\n";
    })
    ->onTransition([](Interpreter *self) {
      self->logInfo();
    })
    ->onStop([]() {
      cout << "oh no we stopped c:\n";
    })
    ->start();

  toggleMachine->send("TIMER");

  toggleMachine->send("TIMER");

  toggleMachine->send("TIMER");

  toggleMachine->stop();

  delete toggleMachine;

  return 0;
}

compile with:

g++ -std=c++11

for example,

g++ -std=c++11 ./src/*.cpp ./example.cpp -o example.out

& run with

./example.out

I use ./go

License

MIT ยฉ Kipras Melnikovas

xstate-cpp's People

Contributors

kiprasmel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xstate-cpp's Issues

Allow state nesting

struct State {
	std::map<const char *, const char *> on;
+	StateMachine states; /** optional for nesting */
};

Parallel state machines

...are on their way.
I've already got'em working,
alongside loads of other changes, thus it's taking a little. brb

Return self once transitioning your state

struct StateMachine {
	const char *id;
	const char *initial;
	States states;
+	const char *value;

-	const char *transition(const char *currentState, const char *event) {
+	StateMachine *transition(const char *currentState, const char *event) {
		const char *nextState = this->states[currentState].on[event];
+		this->value = nextState;
-		return nextState;
+		return this;
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.