Skip to content

Commit c04b433

Browse files
committed
Use a dedicated registry class for timestamp array management
1 parent 584bf8d commit c04b433

1 file changed

Lines changed: 48 additions & 42 deletions

File tree

Source/Processors/GenericProcessor/GenericProcessor.cpp

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,58 @@
4848

4949
namespace
5050
{
51-
using TimestampArrayState = std::unordered_map<const GenericProcessor*, std::map<uint16, std::vector<double>>>;
51+
using TimestampArraysByStream = std::unordered_map<uint16, std::vector<double>>;
5252

53-
TimestampArrayState& getTimestampArrayState()
53+
class TimestampArrayRegistry
5454
{
55-
static TimestampArrayState state;
56-
return state;
57-
}
55+
public:
56+
void clearProcessor (const GenericProcessor* processor)
57+
{
58+
arrays.erase (processor);
59+
}
5860

59-
void clearTimestampArrayState (const GenericProcessor* processor)
60-
{
61-
getTimestampArrayState().erase (processor);
62-
}
61+
void clearStream (const GenericProcessor* processor, uint16 streamId)
62+
{
63+
auto processorIt = arrays.find (processor);
6364

64-
void clearTimestampArrayState (const GenericProcessor* processor, uint16 streamId)
65-
{
66-
auto processorIt = getTimestampArrayState().find (processor);
65+
if (processorIt == arrays.end())
66+
return;
6767

68-
if (processorIt == getTimestampArrayState().end())
69-
return;
68+
processorIt->second.erase (streamId);
7069

71-
processorIt->second.erase (streamId);
70+
if (processorIt->second.empty())
71+
arrays.erase (processorIt);
72+
}
7273

73-
if (processorIt->second.empty())
74-
getTimestampArrayState().erase (processorIt);
75-
}
74+
void store (const GenericProcessor* processor, uint16 streamId, const double* timestamps, uint32 nSamples)
75+
{
76+
auto& streamTimestamps = arrays[processor][streamId];
77+
streamTimestamps.assign (timestamps, timestamps + nSamples);
78+
}
7679

77-
void setTimestampArrayState (const GenericProcessor* processor, uint16 streamId, const double* timestamps, uint32 nSamples)
78-
{
79-
auto& timestampsByStream = getTimestampArrayState()[processor];
80-
auto& streamTimestamps = timestampsByStream[streamId];
81-
streamTimestamps.assign (timestamps, timestamps + nSamples);
82-
}
80+
const std::vector<double>* find (const GenericProcessor* processor, uint16 streamId) const
81+
{
82+
auto processorIt = arrays.find (processor);
8383

84-
const std::vector<double>* findTimestampArrayState (const GenericProcessor* processor, uint16 streamId)
85-
{
86-
auto processorIt = getTimestampArrayState().find (processor);
84+
if (processorIt == arrays.end())
85+
return nullptr;
8786

88-
if (processorIt == getTimestampArrayState().end())
89-
return nullptr;
87+
auto streamIt = processorIt->second.find (streamId);
9088

91-
auto streamIt = processorIt->second.find (streamId);
89+
if (streamIt == processorIt->second.end() || streamIt->second.empty())
90+
return nullptr;
9291

93-
if (streamIt == processorIt->second.end() || streamIt->second.empty())
94-
return nullptr;
92+
return &streamIt->second;
93+
}
9594

96-
return &streamIt->second;
95+
private:
96+
std::unordered_map<const GenericProcessor*, TimestampArraysByStream> arrays;
97+
};
98+
99+
TimestampArrayRegistry& getTimestampArrayRegistry()
100+
{
101+
static TimestampArrayRegistry registry;
102+
return registry;
97103
}
98104

99105
constexpr int timestampArrayOffset = EVENT_BASE_SIZE + 12;
@@ -194,7 +200,7 @@ GenericProcessor::GenericProcessor (const String& name, bool headlessMode_)
194200

195201
GenericProcessor::~GenericProcessor()
196202
{
197-
clearTimestampArrayState (this);
203+
getTimestampArrayRegistry().clearProcessor (this);
198204
editor.reset(); // remove parameter editors before parameters
199205

200206
dataStreamParameters.clear (true);
@@ -717,7 +723,7 @@ void GenericProcessor::clearSettings()
717723

718724
ttlEventChannel = nullptr;
719725

720-
clearTimestampArrayState (this);
726+
getTimestampArrayRegistry().clearProcessor (this);
721727
startTimestampsForBlock.clear();
722728
startSamplesForBlock.clear();
723729
syncStreamIds.clear();
@@ -1313,14 +1319,14 @@ double GenericProcessor::getFirstTimestampForBlock (uint16 streamId) const
13131319

13141320
const double* GenericProcessor::getTimestampsForBlock (uint16 streamId) const
13151321
{
1316-
const std::vector<double>* timestamps = findTimestampArrayState (this, streamId);
1322+
const std::vector<double>* timestamps = getTimestampArrayRegistry().find (this, streamId);
13171323

13181324
return timestamps != nullptr ? timestamps->data() : nullptr;
13191325
}
13201326

13211327
bool GenericProcessor::getTimestampForSample (uint16 streamId, int64 sampleNumber, double& timestamp) const
13221328
{
1323-
const std::vector<double>* timestamps = findTimestampArrayState (this, streamId);
1329+
const std::vector<double>* timestamps = getTimestampArrayRegistry().find (this, streamId);
13241330

13251331
if (timestamps == nullptr)
13261332
return false;
@@ -1349,7 +1355,7 @@ void GenericProcessor::setTimestampAndSamples (int64 sampleNumber,
13491355
uint16 streamId,
13501356
uint16 syncStreamId)
13511357
{
1352-
clearTimestampArrayState (this, streamId);
1358+
getTimestampArrayRegistry().clearStream (this, streamId);
13531359

13541360
HeapBlock<char> data;
13551361
size_t dataSize = SystemEvent::fillTimestampAndSamplesData (data,
@@ -1377,7 +1383,7 @@ void GenericProcessor::setTimestampArrayForBlock (int64 sampleNumber,
13771383
uint16 streamId,
13781384
uint16 syncStreamId)
13791385
{
1380-
clearTimestampArrayState (this, streamId);
1386+
getTimestampArrayRegistry().clearStream (this, streamId);
13811387

13821388
if (timestamps == nullptr || nSamples == 0)
13831389
return;
@@ -1399,7 +1405,7 @@ void GenericProcessor::setTimestampArrayForBlock (int64 sampleNumber,
13991405
syncStreamIds[streamId] = syncStreamId;
14001406
numSamplesInBlock[streamId] = nSamples;
14011407
processStartTimes[streamId] = m_initialProcessTime;
1402-
setTimestampArrayState (this, streamId, timestamps, nSamples);
1408+
getTimestampArrayRegistry().store (this, streamId, timestamps, nSamples);
14031409
}
14041410

14051411
int GenericProcessor::getGlobalChannelIndex (uint16 streamId, int localIndex) const
@@ -1441,7 +1447,7 @@ int GenericProcessor::processEventBuffer()
14411447
uint32 nSamples = *reinterpret_cast<const uint32*> (dataptr + 24);
14421448
int64 initialTicks = *reinterpret_cast<const int64*> (dataptr + 28);
14431449

1444-
clearTimestampArrayState (this, sourceStreamId);
1450+
getTimestampArrayRegistry().clearStream (this, sourceStreamId);
14451451

14461452
startSamplesForBlock[sourceStreamId] = startSample;
14471453
startTimestampsForBlock[sourceStreamId] = startTimestamp;
@@ -1452,7 +1458,7 @@ int GenericProcessor::processEventBuffer()
14521458
if (systemEventType == SystemEvent::Type::TIMESTAMP_ARRAY && nSamples > 0)
14531459
{
14541460
const double* timestamps = reinterpret_cast<const double*> (dataptr + timestampArrayOffset);
1455-
setTimestampArrayState (this, sourceStreamId, timestamps, nSamples);
1461+
getTimestampArrayRegistry().store (this, sourceStreamId, timestamps, nSamples);
14561462
}
14571463
}
14581464
}

0 commit comments

Comments
 (0)