DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
UnboundedMessageBlock.hpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
5
6 Copyright 2022, 2023 Chris M. (RealTimeChris)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26/// UnboundedMessageBlock.hpp - Header file for the "UnboundedMessageBlock" stuff.
27/// Dec 12, 2021
28/// https://discordcoreapi.com
29/// \file UnboundedMessageBlock.hpp
30#pragma once
31
33
34namespace discord_core_api {
35
36 template<typename value_type>
37 concept copyable_or_movable = std::copyable<jsonifier_internal::unwrap_t<value_type>> || std::movable<jsonifier_internal::unwrap_t<value_type>>;
38
39 /// @brief A thread-safe messaging block for data-structures.
40 /// @tparam value_type the type of object that will be sent over the message block.
41 template<copyable_or_movable value_type_new> class unbounded_message_block {
42 public:
43 using value_type = value_type_new;
44
45 DCA_INLINE unbounded_message_block(){};
46
47 DCA_INLINE unbounded_message_block& operator=(unbounded_message_block&& other) noexcept {
48 if (this != &other) {
49 std::swap(queue, other.queue);
50 }
51 return *this;
52 }
53
54 DCA_INLINE unbounded_message_block& operator=(const unbounded_message_block&) = delete;
55 DCA_INLINE unbounded_message_block(const unbounded_message_block&) = delete;
56
57 template<copyable_or_movable value_type_newer> DCA_INLINE void send(value_type_newer&& object) {
58 std::unique_lock lock{ accessMutex };
59 queue.emplace_back(std::forward<value_type_newer>(object));
60 }
61
62 DCA_INLINE void clearContents() {
63 std::unique_lock lock{ accessMutex };
64 queue.clear();
65 }
66
67 DCA_INLINE bool tryReceive(value_type& object) {
68 std::unique_lock lock{ accessMutex };
69 if (queue.size() > 0) {
70 object = std::move(queue.front());
71 queue.pop_front();
72 return true;
73 } else {
74 return false;
75 }
76 }
77
78 DCA_INLINE uint64_t size() {
79 std::unique_lock lock{ accessMutex };
80 return queue.size();
81 }
82
83 DCA_INLINE ~unbounded_message_block() = default;
84
85 protected:
86 std::deque<value_type> queue{};
87 std::mutex accessMutex{};
88 };
89
90 template<typename value_type> DCA_INLINE bool waitForTimeToPass(unbounded_message_block<jsonifier_internal::unwrap_t<value_type>>& outBuffer, value_type& argOne, uint64_t timeInMsNew) {
91 stop_watch<milliseconds> stopWatch{ milliseconds{ timeInMsNew } };
92 stopWatch.reset();
93 while (!outBuffer.tryReceive(argOne)) {
94 std::this_thread::sleep_for(1ms);
95 if (stopWatch.hasTimeElapsed()) {
96 return true;
97 }
98 };
99 return false;
100 }
101
102}
A thread-safe messaging block for data-structures.
The main namespace for the forward-facing interfaces.