Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
params.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <type_traits>
35#include <utility>
36
37namespace console {
47 template <class... Ts>
48 class Params;
49
55 template <>
56 class Params<> {
57 public:
61 Params() = default;
62
69 template <class F>
70 auto apply(F f) const -> decltype(f()) {
71 return f();
72 }
73 };
74
81 template <class T>
82 class Params<T> {
84
85 public:
90 Params(const T &value) : value(value) {}
91
99 template <class F>
100 auto apply(F f) const -> decltype(f(value)) {
101 return f(value);
102 };
103 };
104
113 template <class T, class... Rest>
114 class Params<T, Rest...> {
116 Params<Rest...> rest;
117
125 template <class F>
126 struct Helper {
127 F f;
128 const T &value;
129
135 Helper(F f, const T &value) : f(f), value(value) {}
136
144 template <class... Args>
145 auto operator()(const Args &...args) const
146 -> decltype(f(value, args...)) {
147 return f(value, args...);
148 }
149 };
150
151 public:
157 Params(const T &value, const Rest &...rest) :
158 value(value), rest(rest...) {}
159
168 template <class F>
169 auto apply(F f) const -> decltype(rest.apply(Helper<F>(f, value))) {
170 return rest.apply(Helper<F>(f, value));
171 }
172 };
173
184 template <class... Ts>
186 return {std::forward<Ts>(values)...};
187 }
188}
Params< Rest... > rest
剩余参数的包装器
Definition params.h:116
auto apply(F f) const -> decltype(rest.apply(Helper< F >(f, value)))
将存储的所有参数应用于可调用对象。
Definition params.h:169
Params(const T &value, const Rest &...rest)
从多个值构造多参数 Params。
Definition params.h:157
T value
当前参数值
Definition params.h:115
auto apply(F f) const -> decltype(f(value))
将存储的参数应用于可调用对象。
Definition params.h:100
Params(const T &value)
从给定值构造单参数 Params。
Definition params.h:90
T value
存储的参数值
Definition params.h:83
auto apply(F f) const -> decltype(f())
将存储的参数应用于可调用对象。
Definition params.h:70
Params()=default
无参构造函数。
递归参数包包装器,将多个参数存储为值类型,并支持通过 apply 方法延迟调用。
Definition params.h:48
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:99
Params< typename std::decay< Ts >::type... > params(Ts &&...values)
便捷函数,用于构造 Params 对象。
Definition params.h:185
辅助函数对象,用于在 apply 过程中将当前参数与剩余参数合并。
Definition params.h:126
const T & value
当前参数值的引用
Definition params.h:128
F f
用户提供的可调用对象
Definition params.h:127
auto operator()(const Args &...args) const -> decltype(f(value, args...))
调用辅助函数对象。
Definition params.h:145
Helper(F f, const T &value)
构造 Helper 对象。
Definition params.h:135