finish move to Python-handled imports
[Tsunagari.git] / src / timeout.cpp
blob97e97aace970d192e0be76b0f752af80f59627b8
1 /***************************************
2 ** Tsunagari Tile Engine **
3 ** timeout.cpp **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #include <list>
29 #include "formatter.h"
30 #include "python.h"
31 #include "python-bindings-template.cpp"
32 #include "timeout.h"
33 #include "world.h"
35 static std::list<Timeout*> timeouts;
37 /* These require Script::create(boost::python::object) */
39 //static Timeout* pythonSetTimeout(boost::python::object callback, float delay)
40 //{
41 // if (delay < 0.0)
42 // delay = 0.0;
44 // time_t delayi = (time_t)(1000 * delay);
45 // time_t now = World::instance()->time();
46 // time_t end = now + delayi;
48 // // Insert sorted by resolution time.
49 // std::list<Timeout*>::iterator it;
50 // for (it = timeouts.begin(); it != timeouts.end(); it++) {
51 // Timeout* t = *it;
52 // if (end < t->readyTime())
53 // break;
54 // }
56 // Timeout* t = new Timeout(callback, delay);
57 // timeouts.insert(it, t);
58 // return t;
59 //}
61 //Timeout::Timeout(boost::python::object callback, time_t delay)
62 // : callback(Script::create(callback)),
63 // start(World::instance()->time()),
64 // delay(delay),
65 // active(true)
66 //{
67 //}
69 void Timeout::cancel()
71 active = false;
74 bool Timeout::isActive() const
76 return active;
79 bool Timeout::ready(time_t now) const
81 return now > start + delay;
84 time_t Timeout::readyTime() const
86 return start + delay;
89 void Timeout::execute()
91 callback->invoke();
94 std::string Timeout::repr() const
96 time_t now = World::instance()->time();
97 return Formatter("<timeout time_remaining=%dms active=%s />")
98 % (start + delay - now)
99 % (isActive() ? "true" : "false");
102 void updateTimeouts()
104 time_t now = World::instance()->time();
105 bool next = true;
107 while (next && timeouts.size()) {
108 Timeout* t = timeouts.front();
109 if (!t->isActive()) {
110 timeouts.pop_front();
111 delete t;
113 else if (t->ready(now)) {
114 t->execute();
115 timeouts.pop_front();
116 delete t;
118 else {
119 next = false;
124 void exportTimeout()
126 using namespace boost::python;
128 class_<Timeout> ("Timeout", no_init)
129 .def("cancel", &Timeout::cancel)
130 .def("__repr__", &Timeout::repr)
133 // pythonAddFunction("timeout", make_function(pythonSetTimeout,
134 // return_value_policy<reference_existing_object>()));