libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
print_api.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstring>
5#include <limits>
6#include <string_view>
7#include <utility>
8
9#include "format_surface.hpp"
10#include "printf.hpp"
11#include "writer.hpp"
12
13namespace LibXR::Print
14{
42template <typename Sink, typename Format, typename... Args>
43requires OutputSink<Sink> && CompiledFormat<std::remove_cvref_t<Format>>
44[[nodiscard]] inline ErrorCode Write(Sink& sink, const Format& format, Args&&... args)
45{
46 using Built = std::remove_cvref_t<Format>;
47 return Writer::template RunArgumentOrder<Sink, Built, Built::ArgumentOrder()>(
48 sink, format, std::forward<Args>(args)...);
49}
50
56template <OutputSink Sink, typename Format, typename... Args>
57[[nodiscard]] inline ErrorCode FormatTo(Sink& sink, const Format& format, Args&&... args)
58{
59 return Write(sink, format, std::forward<Args>(args)...);
60}
61
67template <Text Source, OutputSink Sink, typename... Args>
68[[nodiscard]] inline ErrorCode FormatTo(Sink& sink, Args&&... args)
69{
70 constexpr LibXR::Format<Source> format{};
71 return FormatTo(sink, format, std::forward<Args>(args)...);
72}
73
79template <Text Source, OutputSink Sink, typename... Args>
80[[nodiscard]] inline ErrorCode PrintfTo(Sink& sink, Args&&... args)
81{
82 constexpr auto format = Printf::Build<Source>();
83 return FormatTo(sink, format, std::forward<Args>(args)...);
84}
85
103[[nodiscard]] inline size_t FormatIntoBuffer(char* buffer, size_t capacity, const auto& format,
104 auto&&... args)
105{
106 struct BufferSink
107 {
108 char* buffer = nullptr;
109 size_t retain_limit = 0;
110 size_t retained_size = 0;
111 size_t total_size = 0;
112
113 [[nodiscard]] ErrorCode Write(std::string_view chunk)
114 {
115 total_size += chunk.size();
116
117 if (buffer == nullptr || retained_size >= retain_limit)
118 {
119 return ErrorCode::OK;
120 }
121
122 size_t writable = retain_limit - retained_size;
123 size_t copy_size = chunk.size() < writable ? chunk.size() : writable;
124 if (copy_size > 0)
125 {
126 std::memcpy(buffer + retained_size, chunk.data(), copy_size);
127 retained_size += copy_size;
128 }
129 return ErrorCode::OK;
130 }
131 };
132
133 BufferSink sink{
134 .buffer = buffer,
135 .retain_limit = capacity > 0 ? capacity - 1 : 0,
136 };
137 auto ec = Write(sink, format, std::forward<decltype(args)>(args)...);
138 if (ec != ErrorCode::OK)
139 {
140 if (capacity > 0 && buffer != nullptr)
141 {
142 buffer[0] = '\0';
143 }
144 return 0;
145 }
146
147 if (capacity > 0 && buffer != nullptr)
148 {
149 buffer[sink.retained_size] = '\0';
150 }
151
152 return sink.total_size;
153}
154
161template <Text Source, typename... Args>
162[[nodiscard]] inline size_t FormatIntoBuffer(char* buffer, size_t capacity, Args&&... args)
163{
164 constexpr LibXR::Format<Source> format{};
165 return FormatIntoBuffer(buffer, capacity, format, std::forward<Args>(args)...);
166}
167
174template <Text Source, typename... Args>
175[[nodiscard]] inline size_t PrintfIntoBuffer(char* buffer, size_t capacity, Args&&... args)
176{
177 constexpr auto format = Printf::Build<Source>();
178 return FormatIntoBuffer(buffer, capacity, format, std::forward<Args>(args)...);
179}
180
191[[nodiscard]] inline int SNPrintf(char* buffer, size_t capacity, const auto& format,
192 auto&&... args)
193{
194 auto total_size =
195 FormatIntoBuffer(buffer, capacity, format, std::forward<decltype(args)>(args)...);
196 if (total_size > static_cast<size_t>(std::numeric_limits<int>::max()))
197 {
198 return -1;
199 }
200 return static_cast<int>(total_size);
201}
202
211template <Text Source, typename... Args>
212[[nodiscard]] inline int SNPrintf(char* buffer, size_t capacity, Args&&... args)
213{
214 constexpr auto format = Printf::Build<Source>();
215 return SNPrintf(buffer, capacity, format, std::forward<Args>(args)...);
216}
217
218} // namespace LibXR::Print
static consteval Compiled< Source > Build()
Parses and validates a printf format at compile time.
Definition printf.hpp:95
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful