DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
ObjectCache.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/// ObjectCache.hpp - Header file for the "object_cache" stuff.
27/// Dec 12, 2021
28/// https://discordcoreapi.com
29/// \file ObjectCache.hpp
30#pragma once
31
35
36namespace discord_core_api {
37
38 /// @brief A template class representing an object cache.
39 /// @tparam value_type the type of values stored in the cache.
40 template<typename value_type> class object_cache {
41 public:
42 using mapped_type = value_type;
43 using reference = mapped_type&;
44 using const_reference = const mapped_type&;
45 using pointer = mapped_type*;
46
47 /// @brief Default constructor for the object_cache class.
48 inline object_cache() : cacheMap{} {};
49
50 /// @brief Move assignment operator for the object_cache class.
51 /// @param other another object_cache instance to be moved.
52 /// @return reference to the current object_cache instance.
53 inline object_cache& operator=(object_cache&& other) noexcept {
54 if (this != &other) {
55 std::unique_lock lock01{ other.cacheMutex };
56 std::unique_lock lock02{ cacheMutex };
57 std::swap(cacheMap, other.cacheMap);
58 }
59 return *this;
60 }
61
62 /// @brief Move constructor for the object_cache class.
63 /// @param other another object_cache instance to be moved.
64 inline object_cache(object_cache&& other) noexcept {
65 *this = std::move(other);
66 }
67
68 /// @brief Add an object to the cache.
69 /// @tparam mapped_type_new the type of the object to be added.
70 /// @param object the object to be added to the cache.
71 /// @return an iterator pointing to the newly added object in the cache.
72 template<typename mapped_type_new> inline auto emplace(mapped_type_new&& object) {
73 std::unique_lock lock(cacheMutex);
74 return cacheMap.emplace(makeUnique<std::remove_cvref_t<mapped_type_new>>(std::forward<mapped_type_new>(object)));
75 }
76
77 /// @brief Access an object in the cache using a key.
78 /// @tparam mapped_type_new the type of the key used for access.
79 /// @param key the key used for accessing the object in the cache.
80 /// @return reference to the object associated with the provided key.
81 template<typename mapped_type_new> inline reference operator[](mapped_type_new&& key) {
82 std::shared_lock lock(cacheMutex);
83 if (!cacheMap.contains(key)) {
84 cacheMap.emplace(makeUnique<mapped_type>());
85 }
86 return *cacheMap.operator[](key);
87 }
88
89 /// @brief Check if the cache contains an object with a given key.
90 /// @tparam mapped_type_new the type of the key to be checked.
91 /// @param key the key to check for existence in the cache.
92 /// @return `true` if the cache contains the key, `false` otherwise.
93 template<typename mapped_type_new> inline bool contains(mapped_type_new&& key) {
94 std::shared_lock lock(cacheMutex);
95 return cacheMap.contains(std::forward<mapped_type_new>(key));
96 }
97
98 /// @brief Remove an object from the cache using a key.
99 /// @tparam mapped_type_new the type of the key used for removal.
100 /// @param key the key used to remove the object from the cache.
101 template<typename mapped_type_new> inline void erase(mapped_type_new&& key) {
102 std::unique_lock lock(cacheMutex);
103 cacheMap.erase(std::forward<mapped_type_new>(key));
104 }
105
106 /// @brief Get the number of objects currently in the cache.
107 /// @return the number of objects in the cache.
108 inline uint64_t count() {
109 std::shared_lock lock(cacheMutex);
110 return cacheMap.size();
111 }
112
113 /// @brief Get an iterator to the beginning of the cache.
114 /// @return an iterator to the beginning of the cache.
115 inline auto begin() {
116 return cacheMap.begin();
117 }
118
119 /// @brief Get an iterator to the end of the cache.
120 /// @return an iterator to the end of the cache.
121 inline auto end() {
122 return cacheMap.end();
123 }
124
125 /// @brief Destructor for the object_cache class.
126 inline ~object_cache(){};
127
128 protected:
129 unordered_set<unique_ptr<mapped_type>> cacheMap{};///< The underlying container for storing objects.
130 std::shared_mutex cacheMutex{};///< Mutex for ensuring thread-safe access to the cache.
131 };
132
133
134}
A template class representing an object cache.
Definition: ObjectCache.hpp:40
auto begin()
Get an iterator to the beginning of the cache.
object_cache()
Default constructor for the object_cache class.
Definition: ObjectCache.hpp:48
object_cache(object_cache &&other) noexcept
Move constructor for the object_cache class.
Definition: ObjectCache.hpp:64
auto emplace(mapped_type_new &&object)
Add an object to the cache.
Definition: ObjectCache.hpp:72
auto end()
Get an iterator to the end of the cache.
unordered_set< unique_ptr< mapped_type > > cacheMap
The underlying container for storing objects.
std::shared_mutex cacheMutex
Mutex for ensuring thread-safe access to the cache.
uint64_t count()
Get the number of objects currently in the cache.
~object_cache()
Destructor for the object_cache class.
void erase(mapped_type_new &&key)
Remove an object from the cache using a key.
object_cache & operator=(object_cache &&other) noexcept
Move assignment operator for the object_cache class.
Definition: ObjectCache.hpp:53
bool contains(mapped_type_new &&key)
Check if the cache contains an object with a given key.
Definition: ObjectCache.hpp:93
reference operator[](mapped_type_new &&key)
Access an object in the cache using a key.
Definition: ObjectCache.hpp:81
unique_ptr< value_type, deleter > makeUnique(arg_types &&... args)
Helper function to create a unique_ptr for a non-array object.
Definition: UniquePtr.hpp:321
The main namespace for the forward-facing interfaces.