aboutsummaryrefslogtreecommitdiff
path: root/kapi/src/cio.cpp
blob: 210e58e80df3a907e7a230821b61ab51b15ba60e (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
#include "kapi/cio.hpp"

#include <optional>
#include <utility>

namespace teachos::cio
{
  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
  auto constinit null_device = output_device{};
  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
  auto constinit active_device = &null_device;

  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