[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / stdio / printf_core / writer.h
blobb5c36bf38e8141bef57eb23e9d45203d4cb55061
1 //===-- Writer definition for printf ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H
10 #define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H
12 #include "src/__support/CPP/string_view.h"
13 #include <stddef.h>
15 namespace __llvm_libc {
16 namespace printf_core {
18 using WriteStrFunc = int (*)(void *, cpp::string_view);
19 using WriteCharsFunc = int (*)(void *, char, size_t);
20 using WriteCharFunc = int (*)(void *, char);
22 class Writer final {
23 // output is a pointer to the string or file that the writer is meant to write
24 // to.
25 void *output;
27 // raw_write is a function that, when called on output with a char* and
28 // length, will copy the number of bytes equal to the length from the char*
29 // onto the end of output. It should return a positive number or zero on
30 // success, or a negative number on failure.
31 WriteStrFunc str_write;
32 WriteCharsFunc chars_write;
33 WriteCharFunc char_write;
35 int chars_written = 0;
37 public:
38 Writer(void *init_output, WriteStrFunc init_str_write,
39 WriteCharsFunc init_chars_write, WriteCharFunc init_char_write)
40 : output(init_output), str_write(init_str_write),
41 chars_write(init_chars_write), char_write(init_char_write) {}
43 // write will copy new_string into output using str_write. It increments
44 // chars_written by the length of new_string. It returns the result of
45 // str_write.
46 int write(cpp::string_view new_string);
48 // this version of write will copy length copies of new_char into output using
49 // chars_write. This is primarily used for padding. It returns the result of
50 // chars_write.
51 int write(char new_char, size_t len);
53 // this version of write will copy just new_char into output. This is often
54 // used for negative signs. It returns the result of chars_write.
55 int write(char new_char);
57 int get_chars_written() { return chars_written; }
60 } // namespace printf_core
61 } // namespace __llvm_libc
63 #endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITER_H