aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/kapi/cio.cpp
blob: 66493b6b0a29c590ffbc4903f00446e6a7ef1ffd (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
55
56
57
#include "kapi/cio.hpp"

#include <optional>
#include <string_view>
#include <utility>

namespace teachos::cio
{
  namespace
  {
    struct null_device final : public output_device
    {
      null_device static instance;

      auto write(std::string_view) -> void override {}
      auto writeln(std::string_view) -> void override {}

      auto write_error(std::string_view) -> void override {}
      auto writeln_error(std::string_view) -> void override {}
    };

    constinit null_device null_device::instance;
  }  // namespace

  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
  constinit auto active_device = static_cast<output_device *>(&null_device::instance);

  auto set_output_device(output_device & device) -> std::optional<output_device *>
  {
    if (&device == active_device)
    {
      return {};
    }
    return std::exchange(active_device, &device);
  }

  auto print(std::string_view text) -> void
  {
    active_device->write(text);
  }

  auto println(std::string_view text) -> void
  {
    active_device->writeln(text);
  }

  auto print_error(std::string_view text) -> void
  {
    active_device->write_error(text);
  }

  auto println_error(std::string_view text) -> void
  {
    active_device->writeln_error(text);
  }

}  // namespace teachos::cio