aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/test_support/kapi/cio.cpp
blob: 4a1cd1f62e638eb7dfd9185f541c922fdfd15607 (plain)
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
#include "kernel/test_support/cio.hpp"

#include <kapi/cio.hpp>

#include "kernel/test_support/log_buffer.hpp"

#include <atomic>
#include <optional>
#include <stdexcept>

namespace
{

  auto constinit is_initialized = std::atomic_flag{};
  auto constinit device = std::optional<kernel::tests::cio::output_device>{};

}  // namespace

namespace kapi::cio
{

  auto init() -> void
  {
    if (is_initialized.test_and_set())
    {
      throw std::logic_error("kapi::cio::init() called more than once");
    }

    device.emplace();
    set_output_device(*device);
  }

}  // namespace kapi::cio

namespace kernel::tests::cio
{

  auto deinit() -> void
  {
    if (!is_initialized.test())
    {
      throw std::logic_error("kapi::cio::deinit() called before kapi::cio::init()");
    }

    device.reset();
    is_initialized.clear();
  }

  auto log_buffer() -> kernel::tests::log_buffer &
  {
    return device->log_buffer();
  }

}  // namespace kernel::tests::cio