FastLED 3.9.15
Loading...
Searching...
No Matches

◆ getOrCreateId()

int fl::IdTracker::getOrCreateId ( void * ptr)

Get existing ID for pointer, or create a new one if not found.

Thread-safe.

Parameters
ptrPointer to track
Returns
Unique integer ID for this pointer

Definition at line 5 of file id_tracker.cpp.

5 {
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}
fl::hash_map< void *, int > mPointerToId
Definition id_tracker.h:92
fl::mutex mMutex
Definition id_tracker.h:89

References mMutex, mNextId, and mPointerToId.