57 virtual void print(std::ostream &)
const = 0;
58 virtual void print(std::wostream &)
const = 0;
59 virtual std::string
str()
const = 0;
60 virtual std::wstring
wstr()
const = 0;
61 virtual const std::type_info &
type()
const = 0;
95 void print(std::wostream &os)
const override {
97 os << std::wstring(s.begin(), s.end());
104 std::string
str()
const override {
105 std::ostringstream oss;
110 std::wstring
wstr()
const override {
112 return std::wstring(s.begin(), s.end());
119 const std::type_info &
type()
const override {
return typeid(
T); }
134 class =
typename std::enable_if<
135 !std::is_same<typename std::decay<T>::type,
Item>::value>::type>
138 std::forward<
T>(value))) {}
145 ptr(other.
ptr ? other.
ptr->clone() : nullptr) {}
151 Item(
Item &&other) noexcept :
ptr(other.ptr) { other.ptr =
nullptr; }
163 throw TypeError(std::string(
"type mismatch: ")
190 throw TypeError(std::string(
"type mismatch: ")
191 +
typeid(
T).name() +
" and "
213 if (
this != &other) {
227 if (
this != &other) {
274 class Box :
public std::vector<Item> {
281 template <
class... Args>
282 explicit Box(Args &&...args) {
283 int _[] = {0, (emplace_back(std::forward<Args>(args)), 0)...};
297 if (index >= size())
throw IndexError(
"index out of range");
298 return std::vector<Item>::operator[](index).get<
T>();
310 const T &
get(
size_t index)
const {
311 if (index >= size())
throw IndexError(
"index out of range");
312 return std::vector<Item>::operator[](index).get<
T>();
324 return std::vector<Item>::operator[](index).unsafe_get<
T>();
336 return std::vector<Item>::operator[](index).unsafe_get<
T>();
345 template <
class... Args>
347 if (
sizeof...(Args) != size())
348 throw TypeError(
"unpack: argument count mismatch");
350 int _[] = {0, ((args =
get<Args>(i++)), 0)...};
360 template <
class... Args>
374 if (box.empty())
return os <<
"()";
375 auto it = box.begin();
377 while (++it != box.end()) os <<
", " << *it;
388 if (box.empty())
return os <<
L"()";
389 auto it = box.begin();
391 while (++it != box.end()) os <<
L", " << *it;
void unpack(Args &...args) const
将 Box 中的元素按顺序解包到多个变量中(类型安全)。
Definition box.h:346
T & get(size_t index)
类型安全地获取指定索引处的元素(非常量版本)。
Definition box.h:296
Box(Args &&...args)
从任意数量、任意类型的值构造 Box。
Definition box.h:282
friend std::wostream & operator<<(std::wostream &os, const Box &box)
将 Box 输出到宽字符流。
Definition box.h:387
friend std::ostream & operator<<(std::ostream &os, const Box &box)
将 Box 输出到流,格式类似于元组 (elem1, elem2, ...)。
Definition box.h:373
void unsafe_unpack(Args &...args) const
将 Box 中的元素按顺序解包到多个变量中(不安全版本)。
Definition box.h:361
const T & get(size_t index) const
类型安全地获取指定索引处的元素(常量版本)。
Definition box.h:310
T & unsafe_get(size_t index)
不安全地获取指定索引处的元素(非常量版本)。
Definition box.h:323
const T & unsafe_get(size_t index) const
不安全地获取指定索引处的元素(常量版本)。
Definition box.h:335
IndexError(const std::string &msg)
构造 IndexError。
Definition csexc.h:153
可存储任意类型单个对象的类型擦除包装器。
Definition box.h:51
T & unsafe_get()
不安全地获取存储值的引用(非常量版本)。
Definition box.h:176
Item(Item &&other) noexcept
移动构造函数。
Definition box.h:151
friend std::ostream & operator<<(std::ostream &os, const Item &item)
将 Item 输出到流。
Definition box.h:241
~Item()
析构函数,释放内部堆内存。
Definition box.h:265
Item()
默认构造一个空 Item(ptr 为 nullptr)。
Definition box.h:126
const T & get() const
类型安全地获取存储值的引用(常量版本)。
Definition box.h:187
Item(const Item &other)
拷贝构造函数。
Definition box.h:144
T & get()
类型安全地获取存储值的引用(非常量版本)。
Definition box.h:160
const Item & operator=(const Item &other)
拷贝赋值运算符。
Definition box.h:212
std::string str() const
返回 Item 的字符串表示。
Definition box.h:261
const Item & operator=(Item &&other) noexcept
移动赋值运算符。
Definition box.h:226
std::wstring wstr() const
Definition box.h:262
friend std::wostream & operator<<(std::wostream &os, const Item &item)
将 Item 输出到宽字符流。
Definition box.h:252
const T & unsafe_get() const
不安全地获取存储值的引用(常量版本)。
Definition box.h:203
Base * ptr
指向实际存储数据的指针(堆上对象)。
Definition box.h:122
Item(T &&value)
从任意类型构造 Item(万能引用)。
Definition box.h:136
TypeError(const std::string &msg)
构造 TypeError。
Definition csexc.h:102
定义 console 库使用的自定义异常类层次结构。
std::enable_if< is_string< typenamestd::decay< T >::type >::value >::type repr(T &&value, std::basic_ostream< CharT, Traits > &os=std::cout)
输出字符串类型(std::string, const char* 等)的表示,带双引号。
Definition repr.h:97
std::string tiname(const std::type_info &ti)
tiname_impl 的 char 版别名。
Definition repr.h:74
提供类似 Python 的 repr() 函数,用于生成对象的可读字符串表示。
虚基类,定义了所有派生类必须实现的接口。
Definition box.h:54
virtual std::wstring wstr() const =0
virtual const std::type_info & type() const =0
virtual void print(std::ostream &) const =0
virtual void print(std::wostream &) const =0
virtual Base * clone() const =0
virtual std::string str() const =0
virtual ~Base()
Definition box.h:55
模板派生类,具体存储类型 T 的值。
Definition box.h:69
void print(std::wostream &os) const override
Definition box.h:95
std::string str() const override
返回存储值的字符串表示。
Definition box.h:104
T value
实际存储的值
Definition box.h:70
Base * clone() const override
克隆当前对象(堆上分配)。
Definition box.h:88
Derived(const T &v)
从左值构造 Derived。
Definition box.h:76
void print(std::ostream &os) const override
将存储的值输出到流(使用 repr 函数)。
Definition box.h:94
const std::type_info & type() const override
获取存储值的类型信息。
Definition box.h:119
Derived(T &&v)
从右值构造 Derived。
Definition box.h:82
std::wstring wstr() const override
Definition box.h:110