Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
cow.h
Go to the documentation of this file.
1
8
9/*
10Copyright (c) 2026 MrXie1109
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files (the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29*/
30
31#pragma once
32#include <iostream>
33#include <memory>
34#include <type_traits>
35#include <utility>
36
37namespace 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
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}
Cow(T &&t)
从已有对象移动构造 Cow。
Definition cow.h:81
Cow(const T &t)
从已有对象拷贝构造 Cow。
Definition cow.h:75
auto write(F &&f) -> decltype(f(writer()))
写入数据的通用函数。
Definition cow.h:155
Cow()
默认构造函数。
Definition cow.h:60
auto read(F &&f) const -> decltype(f(reader()))
读取数据的通用函数。
Definition cow.h:139
const T & reader() const
读取数据的常量引用。
Definition cow.h:116
Cow(const Cow &)=default
默认的拷贝构造函数。
Cow(Args &&...args)
构造函数,使用可变参数构造内部数据。
Definition cow.h:56
const std::shared_ptr< T > & data() const
获取数据的共享指针。
Definition cow.h:169
Cow & operator=(Cow &&)=default
默认的移动赋值函数。
void detach()
分离内部数据,确保只有一个共享指针。
Definition cow.h:107
Cow & operator=(T &&t)
移动赋值运算符。
Definition cow.h:98
const T & r() const
读取数据的常量引用。
Definition cow.h:118
T & writer()
获取数据的引用,用于写入操作。
Definition cow.h:125
auto r(F &&f) const -> decltype(f(r()))
读取数据的通用函数。
Definition cow.h:144
T & w()
获取数据的引用,用于写入操作。
Definition cow.h:130
std::shared_ptr< T > data_
内部数据指针。
Definition cow.h:45
Cow & operator=(const Cow &)=default
默认的拷贝赋值函数。
Cow & operator=(const T &t)
赋值运算符。
Definition cow.h:88
Cow(Cow &&)=default
默认的移动构造函数。
auto w(F &&f) -> decltype(f(w()))
写入数据的通用函数。
Definition cow.h:160
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:99