File: fixed_stack.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (126 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "caf/all.hpp"
#include <cassert>
#include <cstdint>
#include <iostream>

enum class fixed_stack_errc : uint8_t {
  push_to_full = 1,
  pop_from_empty,
};

CAF_BEGIN_TYPE_ID_BLOCK(fixed_stack, first_custom_type_id)

  CAF_ADD_TYPE_ID(fixed_stack, (fixed_stack_errc))

  CAF_ADD_ATOM(fixed_stack, pop_atom)
  CAF_ADD_ATOM(fixed_stack, push_atom)

CAF_END_TYPE_ID_BLOCK(fixed_stack)

CAF_ERROR_CODE_ENUM(fixed_stack_errc)

std::string to_string(fixed_stack_errc x) {
  switch (x) {
    case fixed_stack_errc::push_to_full:
      return "push_to_full";
    case fixed_stack_errc::pop_from_empty:
      return "pop_from_empty";
    default:
      return "-unknown-error-";
  }
}

bool from_string(caf::string_view in, fixed_stack_errc& out) {
  if (in == "push_to_full") {
    out = fixed_stack_errc::push_to_full;
    return true;
  } else if (in == "pop_from_empty") {
    out = fixed_stack_errc::pop_from_empty;
    return true;
  } else {
    return false;
  }
}

bool from_integer(uint8_t in, fixed_stack_errc& out) {
  if (in > 0 && in < 3) {
    out = static_cast<fixed_stack_errc>(in);
    return true;
  } else {
    return false;
  }
}

template <class Inspector>
bool inspect(Inspector& f, fixed_stack_errc& x) {
  return caf::default_enum_inspect(f, x);
}

using std::endl;
using namespace caf;

class fixed_stack : public event_based_actor {
public:
  fixed_stack(actor_config& cfg, size_t stack_size)
    : event_based_actor(cfg), size_(stack_size) {
    full_.assign( //
      [=](push_atom, int) -> error { return fixed_stack_errc::push_to_full; },
      [=](pop_atom) -> int {
        auto result = data_.back();
        data_.pop_back();
        become(filled_);
        return result;
      });
    filled_.assign( //
      [=](push_atom, int what) {
        data_.push_back(what);
        if (data_.size() == size_)
          become(full_);
      },
      [=](pop_atom) -> int {
        auto result = data_.back();
        data_.pop_back();
        if (data_.empty())
          become(empty_);
        return result;
      });
    empty_.assign( //
      [=](push_atom, int what) {
        data_.push_back(what);
        become(filled_);
      },
      [=](pop_atom) -> error { return fixed_stack_errc::pop_from_empty; });
  }

  behavior make_behavior() override {
    assert(size_ >= 2);
    return empty_;
  }

private:
  size_t size_;
  std::vector<int> data_;
  behavior full_;
  behavior filled_;
  behavior empty_;
};

void caf_main(actor_system& system) {
  scoped_actor self{system};
  auto st = self->spawn<fixed_stack>(5u);
  // fill stack
  for (int i = 0; i < 10; ++i)
    self->send(st, push_atom_v, i);
  // drain stack
  aout(self) << "stack: { ";
  bool stack_empty = false;
  while (!stack_empty) {
    self->request(st, std::chrono::seconds(10), pop_atom_v)
      .receive([&](int x) { aout(self) << x << "  "; },
               [&](const error&) { stack_empty = true; });
  }
  aout(self) << "}" << endl;
  self->send_exit(st, exit_reason::user_shutdown);
}

CAF_MAIN(id_block::fixed_stack)