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
6
7#include <atomic>
8#include <string>
9#include <utility>
10
11namespace ds
12{
13 template <typename T>
15 {
16 const std::string label;
17 std::atomic<T> state;
18
19 public:
20 StateMachine(std::string label_, T state_) :
21 label(std::move(label_)),
22 state(state_)
23 {}
24
25 void expect(T state_) const
26 {
27 auto state_snapshot = state.load();
28 if (state_ != state_snapshot)
29 {
30 throw std::logic_error(fmt::format(
31 "[{}] State is {}, but expected {}", label, state_snapshot, state_));
32 }
33 }
34
35 [[nodiscard]] bool check(T state_) const
36 {
37 return state_ == state.load();
38 }
39
40 [[nodiscard]] T value() const
41 {
42 return state.load();
43 }
44
45 void advance(T state_)
46 {
47 LOG_DEBUG_FMT("[{}] Advancing to state {}", label, state_);
48 state.store(state_);
49 }
50 };
51}
Definition state_machine.h:15
void expect(T state_) const
Definition state_machine.h:25
T value() const
Definition state_machine.h:40
void advance(T state_)
Definition state_machine.h:45
StateMachine(std::string label_, T state_)
Definition state_machine.h:20
bool check(T state_) const
Definition state_machine.h:35
#define LOG_DEBUG_FMT
Definition internal_logger.h:14
Definition dl_list.h:9
STL namespace.