Welcome to mirror list, hosted at ThFree Co, Russian Federation.

controller.cpp « anim - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ef8022078fbec6d803a3537bc3a599386974236d (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "anim/controller.hpp"
#include "anim/task.hpp"

#include "base/assert.hpp"
#include "base/timer.hpp"

#include "std/algorithm.hpp"


namespace anim
{
  Controller::Guard::Guard(Controller * controller)
    : m_controller(controller)
  {
    m_controller->Lock();
  }

  Controller::Guard::~Guard()
  {
    m_controller->Unlock();
  }

  void Controller::AddTask(TTaskPtr const & task)
  {
    m_tasks.ProcessList([&] (TTasks & taskList)
    {
      taskList.push_back(task);
      task->SetController(this);
    });
  }

  bool Controller::HasTasks()
  {
    return !m_tasks.Empty();
  }

  bool Controller::HasVisualTasks()
  {
    return m_hasVisualTasks;
  }

  void Controller::Lock()
  {
    ++m_LockCount;
  }

  void Controller::Unlock()
  {
    --m_LockCount;
  }

  int Controller::LockCount()
  {
    ASSERT(m_LockCount >=0, ("Lock/Unlock is unbalanced! LockCount < 0!"));
    return m_LockCount;
  }

  void Controller::PerformStep()
  {
    m_tasks.ProcessList([this] (TTasks & from)
    {
      m_tasksList.clear();
      swap(from, m_tasksList);
    });

    double ts = GetCurrentTime();

    TTasks resultList;

    for (TTaskPtr const & task : m_tasksList)
    {
      task->Lock();

      if (task->IsReady())
      {
        task->Start();
        task->OnStart(ts);
      }
      if (task->IsRunning())
        task->OnStep(ts);

      if (task->IsRunning())
        resultList.push_back(task);
      else
      {
        if (task->IsCancelled())
          task->OnCancel(ts);
        if (task->IsEnded())
          task->OnEnd(ts);
      }

      task->Unlock();
    }

    m_hasVisualTasks = false;
    m_tasks.ProcessList([&] (TTasks & to)
    {
      for_each(resultList.begin(), resultList.end(), [&] (TTaskPtr const & task)
      {
        m_hasVisualTasks |= task->IsVisual();
        to.push_back(task);
      });
    });
  }

  double Controller::GetCurrentTime() const
  {
    return my::Timer::LocalTime();
  }
}