CCF
Loading...
Searching...
No Matches
state_machine.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/logger.h"
6
7#include <atomic>
8#include <string>
9
10namespace ds
11{
12 template <typename T>
14 {
15 const std::string label;
16 std::atomic<T> state;
17
18 public:
19 StateMachine(const std::string& label_, T state_) :
20 label(label_),
21 state(state_)
22 {}
23
24 void expect(T state_) const
25 {
26 auto state_snapshot = state.load();
27 if (state_ != state_snapshot)
28 {
29 throw std::logic_error(fmt::format(
30 "[{}] State is {}, but expected {}", label, state_snapshot, state_));
31 }
32 }
33
34 bool check(T state_) const
35 {
36 return state_ == state.load();
37 }
38
39 T value() const
40 {
41 return state.load();
42 }
43
44 void advance(T state_)
45 {
46 LOG_DEBUG_FMT("[{}] Advancing to state {}", label, state_);
47 state.store(state_);
48 }
49 };
50}
Definition state_machine.h:14
void expect(T state_) const
Definition state_machine.h:24
T value() const
Definition state_machine.h:39
void advance(T state_)
Definition state_machine.h:44
StateMachine(const std::string &label_, T state_)
Definition state_machine.h:19
bool check(T state_) const
Definition state_machine.h:34
#define LOG_DEBUG_FMT
Definition logger.h:357
Definition dl_list.h:9