Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
cow.h
Go to the documentation of this file.
1
8
9
/*
10
Copyright (c) 2026 MrXie1109
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files (the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions:
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
29
*/
30
31
#pragma once
32
#include <iostream>
33
#include <memory>
34
#include <type_traits>
35
#include <utility>
36
37
namespace
console
{
43
template
<
class
T>
44
class
Cow
{
45
std::shared_ptr<T>
data_
;
46
47
public
:
53
template
<
class
... Args,
54
class
=
typename
std::enable_if<
55
std::is_constructible<
T
, Args...>::value>::type>
56
explicit
Cow
(Args &&...args) :
57
data_
(std::make_shared<
T
>(std::forward<Args>(args)...)) {}
58
60
explicit
Cow
() :
data_
(std::make_shared<
T
>()) {}
61
63
Cow
(
const
Cow
&) =
default
;
65
Cow
(
Cow
&&) =
default
;
67
Cow
&
operator=
(
const
Cow
&) =
default
;
69
Cow
&
operator=
(
Cow
&&) =
default
;
70
75
Cow
(
const
T &t) :
data_
(std::make_shared<
T
>(t)) {}
76
81
Cow
(T &&t) :
data_
(std::make_shared<
T
>(std::move(t))) {}
82
88
Cow
&
operator=
(
const
T &t) {
89
data_
= std::make_shared<T>(t);
90
return
*
this
;
91
}
92
98
Cow
&
operator=
(T &&t) {
99
data_
= std::make_shared<T>(std::move(t));
100
return
*
this
;
101
}
102
107
void
detach
() {
108
if
(
data_
.unique())
return
;
109
std::make_shared<T>(*data_).swap(
data_
);
110
}
111
116
const
T
&
reader
()
const
{
return
*
data_
; }
118
const
T
&
r
()
const
{
return
*
data_
; }
119
125
T
&
writer
() {
126
detach
();
127
return
*
data_
;
128
}
129
130
T
&
w
() {
return
writer
(); }
131
138
template
<
class
F>
139
auto
read
(F &&f)
const
->
decltype
(f(
reader
())) {
140
return
f(
reader
());
141
}
142
143
template
<
class
F>
144
auto
r
(F &&f)
const
->
decltype
(f(
r
())) {
145
return
f(
r
());
146
}
147
154
template
<
class
F>
155
auto
write
(F &&f) ->
decltype
(f(
writer
())) {
156
return
f(
writer
());
157
}
158
159
template
<
class
F>
160
auto
w
(F &&f) ->
decltype
(f(
w
())) {
161
return
f(
w
());
162
}
163
169
const
std::shared_ptr<T> &
data
()
const
{
return
data_
; }
170
};
171
}
console::Cow::Cow
Cow(T &&t)
从已有对象移动构造 Cow。
Definition
cow.h:81
console::Cow::Cow
Cow(const T &t)
从已有对象拷贝构造 Cow。
Definition
cow.h:75
console::Cow::write
auto write(F &&f) -> decltype(f(writer()))
写入数据的通用函数。
Definition
cow.h:155
console::Cow::Cow
Cow()
默认构造函数。
Definition
cow.h:60
console::Cow::read
auto read(F &&f) const -> decltype(f(reader()))
读取数据的通用函数。
Definition
cow.h:139
console::Cow::reader
const T & reader() const
读取数据的常量引用。
Definition
cow.h:116
console::Cow::Cow
Cow(const Cow &)=default
默认的拷贝构造函数。
console::Cow::Cow
Cow(Args &&...args)
构造函数,使用可变参数构造内部数据。
Definition
cow.h:56
console::Cow::data
const std::shared_ptr< T > & data() const
获取数据的共享指针。
Definition
cow.h:169
console::Cow::operator=
Cow & operator=(Cow &&)=default
默认的移动赋值函数。
console::Cow::detach
void detach()
分离内部数据,确保只有一个共享指针。
Definition
cow.h:107
console::Cow::operator=
Cow & operator=(T &&t)
移动赋值运算符。
Definition
cow.h:98
console::Cow::r
const T & r() const
读取数据的常量引用。
Definition
cow.h:118
console::Cow::writer
T & writer()
获取数据的引用,用于写入操作。
Definition
cow.h:125
console::Cow::r
auto r(F &&f) const -> decltype(f(r()))
读取数据的通用函数。
Definition
cow.h:144
console::Cow::w
T & w()
获取数据的引用,用于写入操作。
Definition
cow.h:130
console::Cow::data_
std::shared_ptr< T > data_
内部数据指针。
Definition
cow.h:45
console::Cow::operator=
Cow & operator=(const Cow &)=default
默认的拷贝赋值函数。
console::Cow::operator=
Cow & operator=(const T &t)
赋值运算符。
Definition
cow.h:88
console::Cow::Cow
Cow(Cow &&)=default
默认的移动构造函数。
console::Cow::w
auto w(F &&f) -> decltype(f(w()))
写入数据的通用函数。
Definition
cow.h:160
console
本库所有组件所在的顶层命名空间。
console::Key::T
@ T
Definition
kb.h:99
include
container
cow.h
Generated on
for Console Library by
1.18.0