libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::RuntimeStringView< Source, Args > Class Template Reference

运行期构造、长期保留的 NUL 结尾字符串视图。 Runtime-built retained NUL-terminated string view. More...

#include <libxr_string.hpp>

Public Member Functions

constexpr RuntimeStringView ()=default
 构造一个空的有效视图 / Constructs an empty valid view.
 
 RuntimeStringView (const RuntimeStringView &)=delete
 
RuntimeStringView & operator= (const RuntimeStringView &)=delete
 
 RuntimeStringView (RuntimeStringView &&other) noexcept
 移动构造并接管保留存储句柄 / Move-constructs by taking the retained storage handle.
 
RuntimeStringView & operator= (RuntimeStringView &&)=delete
 
 RuntimeStringView (std::string_view text)
 拷贝文本到保留存储 / Copies text into retained storage.
 
template<size_t N>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
 RuntimeStringView (char(&text)[N])
 按文本语义拷贝一个有界可变字符数组 / Copies one bounded mutable char array as text.
 
template<size_t N>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
 RuntimeStringView (const char(&text)[N])
 按文本语义拷贝一个有界只读字符数组 / Copies one bounded const char array as text.
 
template<typename CharPtr >
requires (Source.Size() == 0 && sizeof...(Args) == 0 && (std::is_same_v<CharPtr, char*> || std::is_same_v<CharPtr, const char*>))
 RuntimeStringView (CharPtr text)
 
 RuntimeStringView (std::nullptr_t text)
 
template<typename First , typename Second , typename... Rest>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
 RuntimeStringView (First &&first, Second &&second, Rest &&... rest)
 拼接多个文本片段到保留存储 / Concatenates text parts into retained storage.
 
template<typename... CallArgs>
requires ((Source.Size() != 0 || sizeof...(Args) != 0) && Detail::RuntimeStringArgumentTypes<Args...>::template matches<CallArgs...>)
ErrorCode Reformat (CallArgs &&... args)
 使用绑定的 brace-style 格式重写当前内容。 Rewrite current content with the bound brace-style format.
 
template<typename... CallArgs>
requires ((Source.Size() != 0 || sizeof...(Args) != 0) && Detail::RuntimeStringArgumentTypes<Args...>::template matches<CallArgs...>)
ErrorCode Reprintf (CallArgs &&... args)
 使用绑定的 printf-style 格式重写当前内容。 Rewrite current content with the bound printf-style format.
 
constexpr std::string_view View () const
 返回当前只读视图 / Returns the current read-only view.
 
const char * CStr () const
 返回 NUL 结尾 C 字符串 / Returns a NUL-terminated C string.
 
constexpr size_t Size () const
 返回不含结尾 NUL 的文本长度 / Returns the text size excluding the trailing NUL.
 
constexpr bool Empty () const
 返回当前可见文本是否为空 / Returns whether the current visible text is empty.
 
constexpr ErrorCode Status () const
 返回最近一次操作状态 / Returns the latest operation status.
 
constexpr operator std::string_view () const
 转换为只读字符串视图 / Converts to a read-only string view.
 
 operator const char * () const
 转换为 NUL 结尾 C 字符串 / Converts to a NUL-terminated C string.
 

Detailed Description

template<Print::Text Source = "", typename... Args>
class LibXR::RuntimeStringView< Source, Args >

运行期构造、长期保留的 NUL 结尾字符串视图。 Runtime-built retained NUL-terminated string view.

文本构造用于模块名、topic 名、后缀拼接等只构造一次的名字。 格式化构造绑定字面量和参数类型,第一次 Reformat() / Reprintf() 前从编译后的格式元数据计算最大容量,之后只复用该容量。对象析构时 不释放已分配存储。

Text construction is for retained names such as module names and topic suffixes. Formatted instances bind the literal and argument types, compute maximum capacity from compiled format metadata before the first rewrite, then reuse that capacity. Allocated storage is intentionally not released by the object destructor.

Definition at line 250 of file libxr_string.hpp.

Constructor & Destructor Documentation

◆ RuntimeStringView() [1/7]

template<Print::Text Source = "", typename... Args>
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( RuntimeStringView< Source, Args > && other)
inlinenoexcept

移动构造并接管保留存储句柄 / Move-constructs by taking the retained storage handle.

Definition at line 269 of file libxr_string.hpp.

270 : data_(other.data_),
271 size_(other.size_),
272 capacity_(other.capacity_),
273 status_(other.status_)
274 {
275 other.data_ = nullptr;
276 other.size_ = 0;
277 other.capacity_ = 0;
278 other.status_ = ErrorCode::OK;
279 }
@ OK
操作成功 / Operation successful

◆ RuntimeStringView() [2/7]

template<Print::Text Source = "", typename... Args>
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( std::string_view text)
inlineexplicit

拷贝文本到保留存储 / Copies text into retained storage.

Definition at line 284 of file libxr_string.hpp.

286 {
287 static_cast<void>(AssignCopy(text));
288 }

◆ RuntimeStringView() [3/7]

template<Print::Text Source = "", typename... Args>
template<size_t N>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( char(&) text[N])
inlineexplicit

按文本语义拷贝一个有界可变字符数组 / Copies one bounded mutable char array as text.

Definition at line 292 of file libxr_string.hpp.

294 {
295 static_cast<void>(AssignCopy(std::string_view(text, BoundedTextLength(text, N))));
296 }

◆ RuntimeStringView() [4/7]

template<Print::Text Source = "", typename... Args>
template<size_t N>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( const char(&) text[N])
inlineexplicit

按文本语义拷贝一个有界只读字符数组 / Copies one bounded const char array as text.

Definition at line 300 of file libxr_string.hpp.

302 {
303 static_cast<void>(AssignCopy(std::string_view(text, BoundedTextLength(text, N))));
304 }

◆ RuntimeStringView() [5/7]

template<Print::Text Source = "", typename... Args>
template<typename CharPtr >
requires (Source.Size() == 0 && sizeof...(Args) == 0 && (std::is_same_v<CharPtr, char*> || std::is_same_v<CharPtr, const char*>))
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( CharPtr text)
inlineexplicit

拷贝 NUL 结尾 C 字符串指针到保留存储。 Copies a NUL-terminated C-string pointer into retained storage.

Definition at line 311 of file libxr_string.hpp.

312 {
313 static_cast<void>(AssignCopy(text));
314 }

◆ RuntimeStringView() [6/7]

template<Print::Text Source = "", typename... Args>
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( std::nullptr_t text)
inlineexplicit

保留裸 nullptr 入口以维持运行期空指针检查语义。 Preserves the old bare-nullptr entry for runtime null checks.

Definition at line 318 of file libxr_string.hpp.

320 {
321 static_cast<void>(AssignCopy(static_cast<const char*>(text)));
322 }

◆ RuntimeStringView() [7/7]

template<Print::Text Source = "", typename... Args>
template<typename First , typename Second , typename... Rest>
requires (Source.Size() == 0 && sizeof...(Args) == 0)
LibXR::RuntimeStringView< Source, Args >::RuntimeStringView ( First && first,
Second && second,
Rest &&... rest )
inlineexplicit

拼接多个文本片段到保留存储 / Concatenates text parts into retained storage.

Definition at line 326 of file libxr_string.hpp.

328 {
329 static_cast<void>(AssignConcat(std::forward<First>(first),
330 std::forward<Second>(second),
331 std::forward<Rest>(rest)...));
332 }

Member Function Documentation

◆ CStr()

template<Print::Text Source = "", typename... Args>
const char * LibXR::RuntimeStringView< Source, Args >::CStr ( ) const
inlinenodiscard

返回 NUL 结尾 C 字符串 / Returns a NUL-terminated C string.

Definition at line 389 of file libxr_string.hpp.

389{ return data_ == nullptr ? "" : data_; }

◆ Empty()

template<Print::Text Source = "", typename... Args>
bool LibXR::RuntimeStringView< Source, Args >::Empty ( ) const
inlinenodiscardconstexpr

返回当前可见文本是否为空 / Returns whether the current visible text is empty.

Definition at line 393 of file libxr_string.hpp.

393{ return size_ == 0; }

◆ operator const char *()

template<Print::Text Source = "", typename... Args>
LibXR::RuntimeStringView< Source, Args >::operator const char * ( ) const
inlinenodiscard

转换为 NUL 结尾 C 字符串 / Converts to a NUL-terminated C string.

Definition at line 399 of file libxr_string.hpp.

399{ return CStr(); }
const char * CStr() const
返回 NUL 结尾 C 字符串 / Returns a NUL-terminated C string.

◆ operator std::string_view()

template<Print::Text Source = "", typename... Args>
LibXR::RuntimeStringView< Source, Args >::operator std::string_view ( ) const
inlinenodiscardconstexpr

转换为只读字符串视图 / Converts to a read-only string view.

Definition at line 397 of file libxr_string.hpp.

397{ return View(); }
constexpr std::string_view View() const
返回当前只读视图 / Returns the current read-only view.

◆ Reformat()

template<Print::Text Source = "", typename... Args>
template<typename... CallArgs>
requires ((Source.Size() != 0 || sizeof...(Args) != 0) && Detail::RuntimeStringArgumentTypes<Args...>::template matches<CallArgs...>)
ErrorCode LibXR::RuntimeStringView< Source, Args >::Reformat ( CallArgs &&... args)
inlinenodiscard

使用绑定的 brace-style 格式重写当前内容。 Rewrite current content with the bound brace-style format.

Definition at line 339 of file libxr_string.hpp.

342 {
343 static_assert(LibXR::Format<Source>::template Matches<Args...>(),
344 "LibXR::RuntimeStringView format argument types do not match "
345 "the format");
346 using Built = typename LibXR::Format<Source>::template Compiled<Args...>;
347 static constexpr size_t max_size =
348 Detail::RuntimeStringMaxFormattedSize<Built>(Source.Size());
349 static_assert(max_size != Detail::runtime_string_unbounded_capacity,
350 "LibXR::RuntimeStringView cannot retain unbounded formatted "
351 "runtime strings");
352
353 return AssignFormatted(
354 max_size, [&](auto& sink)
355 { return Print::FormatTo<Source>(sink, std::forward<CallArgs>(args)...); });
356 }

◆ Reprintf()

template<Print::Text Source = "", typename... Args>
template<typename... CallArgs>
requires ((Source.Size() != 0 || sizeof...(Args) != 0) && Detail::RuntimeStringArgumentTypes<Args...>::template matches<CallArgs...>)
ErrorCode LibXR::RuntimeStringView< Source, Args >::Reprintf ( CallArgs &&... args)
inlinenodiscard

使用绑定的 printf-style 格式重写当前内容。 Rewrite current content with the bound printf-style format.

Definition at line 363 of file libxr_string.hpp.

366 {
367 static_assert(Print::Printf::Matches<Source, Args...>(),
368 "LibXR::RuntimeStringView printf argument types do not match "
369 "the format");
370 using Built = Print::Printf::Compiled<Source>;
371 static constexpr size_t max_size =
372 Detail::RuntimeStringMaxFormattedSize<Built>(Source.Size());
373 static_assert(max_size != Detail::runtime_string_unbounded_capacity,
374 "LibXR::RuntimeStringView cannot retain unbounded formatted "
375 "runtime strings");
376
377 return AssignFormatted(
378 max_size, [&](auto& sink)
379 { return Print::PrintfTo<Source>(sink, std::forward<CallArgs>(args)...); });
380 }
static consteval bool Matches()
判断 Args... 是否与启用的转换项精确匹配 / Return whether Args... exactly match the enabled conversions
Definition printf.hpp:138

◆ Size()

template<Print::Text Source = "", typename... Args>
size_t LibXR::RuntimeStringView< Source, Args >::Size ( ) const
inlinenodiscardconstexpr

返回不含结尾 NUL 的文本长度 / Returns the text size excluding the trailing NUL.

Definition at line 391 of file libxr_string.hpp.

391{ return size_; }

◆ Status()

template<Print::Text Source = "", typename... Args>
ErrorCode LibXR::RuntimeStringView< Source, Args >::Status ( ) const
inlinenodiscardconstexpr

返回最近一次操作状态 / Returns the latest operation status.

Definition at line 395 of file libxr_string.hpp.

395{ return status_; }

◆ View()

template<Print::Text Source = "", typename... Args>
std::string_view LibXR::RuntimeStringView< Source, Args >::View ( ) const
inlinenodiscardconstexpr

返回当前只读视图 / Returns the current read-only view.

Definition at line 383 of file libxr_string.hpp.

384 {
385 return data_ == nullptr ? std::string_view{} : std::string_view(data_, size_);
386 }

The documentation for this class was generated from the following file: