FastLED 3.9.15
Loading...
Searching...
No Matches
id_tracker.cpp.hpp
Go to the documentation of this file.
2#include "fl/stl/noexcept.h"
3
4namespace fl {
5
7 if (!ptr) {
8 return -1; // Invalid pointer gets invalid ID
9 }
10
11 // Lock for thread safety
12 mMutex.lock();
13
14 // Check if ID already exists
15 auto it = mPointerToId.find(ptr);
16 if (it != mPointerToId.end()) {
17 int id = it->second;
18 mMutex.unlock();
19 return id;
20 }
21
22 // Create new ID
23 int newId = mNextId++;
24 mPointerToId[ptr] = newId;
25
26 mMutex.unlock();
27 return newId;
28}
29
30bool IdTracker::getId(void* ptr, int* outId) FL_NOEXCEPT {
31 if (!ptr || !outId) {
32 return false;
33 }
34
35 // Lock for thread safety
36 mMutex.lock();
37
38 auto it = mPointerToId.find(ptr);
39 bool found = (it != mPointerToId.end());
40 if (found) {
41 *outId = it->second;
42 }
43
44 mMutex.unlock();
45 return found;
46}
47
49 if (!ptr) {
50 return false;
51 }
52
53 // Lock for thread safety
54 mMutex.lock();
55
56 bool removed = mPointerToId.erase(ptr);
57
58 mMutex.unlock();
59 return removed;
60}
61
63 // Lock for thread safety
64 mMutex.lock();
65
66 size_t currentSize = mPointerToId.size();
67
68 mMutex.unlock();
69 return currentSize;
70}
71
73 // Lock for thread safety
74 mMutex.lock();
75
76 mPointerToId.clear();
77 mNextId = 0; // Reset ID counter to start at 0
78
79 mMutex.unlock();
80}
81
82} // namespace fl
void clear() FL_NOEXCEPT
Clear all tracked pointers and reset ID counter.
int getOrCreateId(void *ptr) FL_NOEXCEPT
Get existing ID for pointer, or create a new one if not found.
bool removeId(void *ptr) FL_NOEXCEPT
Remove tracking for a pointer.
fl::flat_map< void *, int > mPointerToId
Definition id_tracker.h:91
bool getId(void *ptr, int *outId) FL_NOEXCEPT
Get existing ID for pointer without creating a new one.
size_t size() FL_NOEXCEPT
Get the current number of tracked pointers.
fl::mutex mMutex
Definition id_tracker.h:88
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT