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

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

namespace kapi::cio
{
  namespace
  {
    struct null_device final : public output_device
    {
      null_device static instance;
      auto write(output_stream, std::string_view) -> void override {}
    };

    constinit null_device null_device::instance;
  }  // namespace

  constinit auto static 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 write(output_stream stream, std::string_view text) -> void
  {
    active_device->write(stream, text);
  }

}  // namespace kapi::cio