proxygen
StateMachine.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
12 #include <glog/logging.h>
13 #include <tuple>
14 
15 namespace proxygen {
16 
17 template <typename T>
18 class StateMachine {
19  public:
20  using State = typename T::State;
21  using Event = typename T::Event;
22 
23  static State getNewInstance() {
24  return T::getInitialState();
25  }
26 
27  static bool transit(State& state, Event event) {
28  bool ok;
29  State newState;
30 
31  std::tie(newState, ok) = T::find(state, event);
32  if (!ok) {
33  LOG(ERROR) << T::getName() << ": invalid transition tried: " << state
34  << " " << event;
35  return false;
36  } else {
37  VLOG(6) << T::getName() << ": transitioning from " << state << " to "
38  << newState;
39  state = newState;
40  return true;
41  }
42  }
43 
44  static bool canTransit(const State state, Event event) {
45  bool ok;
46 
47  std::tie(std::ignore, ok) = T::find(state, event);
48  return ok;
49  }
50 };
51 
52 }
static State getNewInstance()
Definition: StateMachine.h:23
typename T::State State
Definition: StateMachine.h:20
State
See Core for details.
Definition: Core.h:43
static bool transit(State &state, Event event)
Definition: StateMachine.h:27
typename T::Event Event
Definition: StateMachine.h:21
static bool canTransit(const State state, Event event)
Definition: StateMachine.h:44
Event
Definition: Events.h:15
state
Definition: http_parser.c:272