C++ Pixels Library For Windows
Enable communications with Pixels dice using Bluetooth Low Energy.
Loading...
Searching...
No Matches
GuardedList.h
1#pragma once
2
3#include <iterator>
4#include <list>
5#include <vector>
6#include <mutex>
7
8namespace Systemic
9{
14 template <typename T>
16 {
17 std::list<T> _list{};
18 mutable std::recursive_mutex _mutex{};
19
20 public:
22 using Index = typename std::list<T>::const_iterator;
23
28 std::vector<T> get() const
29 {
30 std::lock_guard lock{ _mutex };
31 return std::vector<T>{ _list.begin(), _list.end() };
32 }
33
39 Index add(const T& item)
40 {
41 std::lock_guard lock{ _mutex };
42 _list.push_back(item);
43 return std::prev(_list.cend());
44 }
45
50 void remove(const Index& index)
51 {
52 std::lock_guard lock{ _mutex };
53 _list.erase(index);
54 }
55 };
56}
A simple thread safe list of items.
Definition: GuardedList.h:16
Index add(const T &item)
Adds the given item to the list and returns its index.
Definition: GuardedList.h:39
std::vector< T > get() const
Gets a copy of the list of items.
Definition: GuardedList.h:28
typename std::list< T >::const_iterator Index
Type of an item index.
Definition: GuardedList.h:22
void remove(const Index &index)
Removes the item at the given index from the list.
Definition: GuardedList.h:50