aboutsummaryrefslogtreecommitdiff
path: root/libk/stdlib.h
blob: 7d89a0cd127d615b583500f6e164a9ed14a3d825 (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
#pragma once

#include <string.h>

class Console {
 public:
   virtual ~Console() = default;
   virtual void write(char c) = 0;
   virtual void write(ViewIterator& msg) = 0;

   static void set(Console* ptr);
   static Console* get();
};

template <typename T>
void printk(const T& a) {
  if (auto* c = Console::get()) {
    if constexpr (is_same<T, char>()) {
      c->write(a);
    } else if constexpr (requires { StringView(a); }) {
      StringView v(a);
      auto iter = v.begin();
      c->write(iter);
    } else if constexpr (requires { IntegerView(a); }) {
      IntegerView v(a);
      auto iter = v.begin();
      c->write(iter);
    } else
      c->write(a);
  }
}

template <typename T, typename... Args>
void printk(const T& a, const Args&... x) {
  printk(a);
  printk(x...);
}

extern "C" __attribute__((__noreturn__)) void abort();