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