File: EventBucket.cxx

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (80 lines) | stat: -rw-r--r-- 1,744 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "EventBucket.h"

unsigned long EventBucket::m_GlobalMTime = 1;

EventBucket::EventBucket()
{
  m_MTime = m_GlobalMTime++;
}

EventBucket::~EventBucket()
{
  Clear();
}

void EventBucket::Clear()
{
  m_Lock.Lock();
  // Remove all the event objects
  for(BucketIt it = m_Bucket.begin(); it != m_Bucket.end(); ++it)
    {
    delete(it->first);
    }
  m_Bucket.clear();
  m_Lock.Unlock();

  m_MTime = m_GlobalMTime++;
}

bool EventBucket::HasEvent(const itk::EventObject &evt, const itk::Object *source) const
{
  m_Lock.Lock();
  // Search for the event. Buckets are never too large so a linear search is fine
  for(BucketIt it = m_Bucket.begin(); it != m_Bucket.end(); ++it)
    {
    const BucketEntry &entry = *it;
    std::string echeck = evt.GetEventName();
    std::string eentry = entry.first->GetEventName();
    if(evt.CheckEvent(entry.first) && (source == NULL || source == entry.second))
      {
      m_Lock.Unlock();
      return true;
      }
    }

  m_Lock.Unlock();
  return false;
}

bool EventBucket::IsEmpty() const
{
  return m_Bucket.size() == 0;
}

void EventBucket::PutEvent(const itk::EventObject &evt, const itk::Object *source)
{
  if(!this->HasEvent(evt, source))
    {
    BucketEntry entry;
    entry.first = evt.MakeObject();
    entry.second = source;
    m_Lock.Lock();
    m_Bucket.insert(entry);
    m_Lock.Unlock();
    m_MTime = m_GlobalMTime++;
    }
}

std::ostream& operator<<(std::ostream& sink, const EventBucket& eb)
{
  sink << "EventBucket[";
  for(EventBucket::BucketIt it = eb.m_Bucket.begin(); it != eb.m_Bucket.end();)
    {
    sink << it->first->GetEventName() << "(" << it->second << ")";
    if(++it != eb.m_Bucket.end())
      sink << ", ";
    }
  sink << "]";
  return sink;
}