1 /***************************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
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
29 #include "formatter.h"
31 #include "python-bindings-template.cpp"
35 static std::list
<Timeout
*> timeouts
;
37 /* These require Script::create(boost::python::object) */
39 //static Timeout* pythonSetTimeout(boost::python::object callback, float delay)
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++) {
52 // if (end < t->readyTime())
56 // Timeout* t = new Timeout(callback, delay);
57 // timeouts.insert(it, t);
61 //Timeout::Timeout(boost::python::object callback, time_t delay)
62 // : callback(Script::create(callback)),
63 // start(World::instance()->time()),
69 void Timeout::cancel()
74 bool Timeout::isActive() const
79 bool Timeout::ready(time_t now
) const
81 return now
> start
+ delay
;
84 time_t Timeout::readyTime() const
89 void Timeout::execute()
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();
107 while (next
&& timeouts
.size()) {
108 Timeout
* t
= timeouts
.front();
109 if (!t
->isActive()) {
110 timeouts
.pop_front();
113 else if (t
->ready(now
)) {
115 timeouts
.pop_front();
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>()));