Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
group.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 <atomic>
33#include <condition_variable>
34#include <exception>
35#include <functional>
36#include <mutex>
37
38namespace console {
45 class Group {
46 int count_{0};
47 std::function<void()> callback_;
48 bool done_{false};
49 mutable std::mutex mutex_;
50 mutable std::condition_variable cv_;
51
53 static void no_op() {}
54
55 public:
61 Group(size_t count = 0, std::function<void()> callback = no_op) :
62 count_(count), callback_(callback) {}
63
69 std::lock_guard<std::mutex> lock(mutex_);
70 if (count_ != 0 || !done_) std::terminate();
71 }
72
78 void add(size_t count) {
79 std::lock_guard<std::mutex> lock(mutex_);
80 if (done_) std::terminate();
81 count_ += count;
82 }
83
89 void done() {
90 std::lock_guard<std::mutex> lock(mutex_);
91 if (count_ <= 0) std::terminate();
92 if (--count_ == 0) {
93 done_ = true;
94 callback_();
95 cv_.notify_all();
96 }
97 }
98
102 void wait() const {
103 std::unique_lock<std::mutex> lock(mutex_);
104 cv_.wait(lock, [this] { return count_ == 0 && done_; });
105 }
106
111 std::function<void()> callback() const {
112 std::lock_guard<std::mutex> lock(mutex_);
113 return callback_;
114 }
115
120 void connect(std::function<void()> callback) {
121 std::lock_guard<std::mutex> lock(mutex_);
123 }
124
126 Group(const Group &) = delete;
128 Group &operator=(const Group &) = delete;
130 Group(Group &&) = delete;
132 Group &operator=(Group &&) = delete;
133 };
134}
Group & operator=(const Group &)=delete
删除复制赋值函数。
std::mutex mutex_
互斥锁
Definition group.h:49
void wait() const
等待所有任务完成。
Definition group.h:102
void connect(std::function< void()> callback)
连接回调函数。
Definition group.h:120
~Group()
析构函数,确保所有任务完成。
Definition group.h:68
Group(size_t count=0, std::function< void()> callback=no_op)
构造函数,初始化任务计数器和回调函数。
Definition group.h:61
std::function< void()> callback_
任务完成回调
Definition group.h:47
void done()
任务完成。
Definition group.h:89
static void no_op()
空操作函数,用于默认回调。
Definition group.h:53
std::condition_variable cv_
条件变量
Definition group.h:50
int count_
任务计数器
Definition group.h:46
bool done_
任务完成标志
Definition group.h:48
Group(const Group &)=delete
删除复制构造函数。
Group & operator=(Group &&)=delete
删除移动赋值函数。
void add(size_t count)
添加任务数量。
Definition group.h:78
std::function< void()> callback() const
获取回调函数。
Definition group.h:111
Group(Group &&)=delete
删除移动构造函数。
本库所有组件所在的顶层命名空间。