跳到正文
本頁目錄

Base(基礎類型)

ll/api/base/ · 通用

概述

Base 模塊提供了 LeviLamina API 的基礎構建塊:導出宏、C++20 概念(concepts)、類型萃取(type traits)、整數類型、編譯期定長字符串、容器別名和元編程工具。

頭文件

頭文件說明
ll/api/base/Macro.h導出宏(LLAPI、LLNDAPI、LLCAPI)
ll/api/base/Concepts.hC++20 概念,用於類型約束
ll/api/base/TypeTraits.h類型內省萃取
ll/api/base/StdInt.h標準整數類型別名
ll/api/base/FixedString.h編譯期定長字符串
ll/api/base/Containers.h容器類型別名(SmallDenseSet、SmallStringMap 等)
ll/api/base/Meta.h元編程工具(visitIndex 等)
ll/api/base/SourceLocation.h源代碼位置信息
ll/api/base/ToString.h轉字符串工具
ll/api/base/CompilerPredefine.h編譯器檢測及 LL_SHARED_EXPORT/LL_SHARED_IMPORT

導出宏

定義在 Macro.h 中,這些宏控制 LeviLamina 和模組之間的符號可見性:

宏構建 LeviLamina 時的展開模組使用時的展開
LLAPI[[maybe_unused]] __declspec(dllexport)[[maybe_unused]] __declspec(dllimport)
LLNDAPI[[nodiscard]] LLAPI[[nodiscard]] LLAPI
LLCAPIextern "C" LLAPIextern "C" LLAPI

切換由 LL_EXPORT 宏定義控制,在 xmake.lua 中通過 add_defines("LL_EXPORT") 設置。

為你的模組創建導出宏

在你的模組中定義類似的宏對:

C++
#ifdef MY_MOD_EXPORT
#define MY_MOD_API __declspec(dllexport)
#else
#define MY_MOD_API __declspec(dllimport)
#endif

然後在你的 xmake.lua 中添加 add_defines("MY_MOD_EXPORT")。

概念(Concepts)

定義在 Concepts.h 中,這些 C++20 概念在整個 API 中提供類型約束:

概念說明
IsString<T>T 是字符串類型(std::string、std::string_view、char* 等)
IsOneOf<T, Ts...>T 是 Ts... 中的類型之一
IsInTypes<T, U>T 包含在類型列表 U 中
IsExpected<T>T 是類似 expected 的類型,具有 value()、error()、has_value()
IsLeviExpected<T>IsExpected<T> 且 error_type == ll::Error
IsOptional<T>T 是類似 optional 的類型(非 expected)
RangeLoopable<T>T 可以使用範圍 for 循環迭代
Associative<T>T 是具有 key_type 和 mapped_type 的範圍
ArrayLike<T>T 是沒有 mapped_type 的範圍
Specializes<T, Z>T 是模板 Z 的特化
TupleLike<T>T 支持 std::tuple_size 和 std::get
Stringable<T>T 具有 toString() 或 to_string() 方法
Awaitable<T>T 是 C++20 可等待類型
Require<T, Pred>T 滿足謂詞 Pred(用於適配 type traits)

示例

C++
#include "ll/api/base/Concepts.h"

template <ll::concepts::IsString T>
void processString(T const& str) {
    // 可接受 std::string、std::string_view、const char* 等
}

template <ll::concepts::RangeLoopable T>
void iterateOver(T const& range) {
    for (auto const& item : range) {
        // 处理每个元素
    }
}

FixedString

定義在 FixedString.h 中,FixedString 是一種編譯期字符串類型,主要用於模板參數(例如 i18n 的 _tr 字面量)。

C++
#include "ll/api/base/FixedString.h"

template <ll::FixedString Name>
struct NamedType {
    static constexpr auto name = Name;
};

using MyType = NamedType<"hello">;
static_assert(MyType::name.sv() == "hello");

相關模塊