Mercurial > love-experiments
annotate src/modules/event/Event.cpp @ 1448:108ee2e81f65 default tip
Fix a case typo, imageData.h -> ImageData.h
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 18 Jan 2014 13:57:44 +0100 |
| parents | 357697c2e2a8 |
| children |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1096
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 3 * |
| 4 * This software is provided 'as-is', without any express or implied | |
| 5 * warranty. In no event will the authors be held liable for any damages | |
| 6 * arising from the use of this software. | |
| 7 * | |
| 8 * Permission is granted to anyone to use this software for any purpose, | |
| 9 * including commercial applications, and to alter it and redistribute it | |
| 10 * freely, subject to the following restrictions: | |
| 11 * | |
| 12 * 1. The origin of this software must not be misrepresented; you must not | |
| 13 * claim that you wrote the original software. If you use this software | |
| 14 * in a product, an acknowledgment in the product documentation would be | |
| 15 * appreciated but is not required. | |
| 16 * 2. Altered source versions must be plainly marked as such, and must not be | |
| 17 * misrepresented as being the original software. | |
| 18 * 3. This notice may not be removed or altered from any source distribution. | |
| 19 **/ | |
| 20 | |
| 21 #include "Event.h" | |
| 22 | |
| 23 using love::thread::Mutex; | |
| 24 using love::thread::Lock; | |
| 25 | |
| 26 namespace love | |
| 27 { | |
| 28 namespace event | |
| 29 { | |
| 30 | |
|
1418
357697c2e2a8
More internal code cleanup
Alex Szpakowski <slime73@gmail.com>
parents:
1345
diff
changeset
|
31 Message::Message(const std::string &name, Variant *a, Variant *b, Variant *c, Variant *d) |
| 1096 | 32 : name(name) |
| 33 , nargs(0) | |
| 34 { | |
| 35 args[0] = a; | |
| 36 args[1] = b; | |
| 37 args[2] = c; | |
| 38 args[3] = d; | |
| 39 for (int i = 0; i < 4; i++) | |
| 40 { | |
| 41 if (!args[i]) | |
| 42 continue; | |
| 43 args[i]->retain(); | |
| 44 nargs++; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 Message::~Message() | |
| 49 { | |
| 50 for (int i = 0; i < nargs; i++) | |
| 51 args[i]->release(); | |
| 52 } | |
| 53 | |
| 54 int Message::toLua(lua_State *L) | |
| 55 { | |
| 56 luax_pushstring(L, name); | |
| 57 for (int i = 0; i < nargs; i++) | |
| 58 args[i]->toLua(L); | |
| 59 return nargs+1; | |
| 60 } | |
| 61 | |
| 62 Message *Message::fromLua(lua_State *L, int n) | |
| 63 { | |
| 64 std::string name = luax_checkstring(L, n); | |
| 65 n++; | |
| 66 Message *m = new Message(name); | |
| 67 for (int i = 0; i < 4; i++) | |
| 68 { | |
| 69 if (lua_isnoneornil(L, n+i)) | |
| 70 break; | |
| 71 m->args[i] = Variant::fromLua(L, n+i); | |
| 72 if (!m->args[i]) | |
| 73 { | |
| 74 delete m; | |
| 75 luaL_error(L, "Argument %d can't be stored safely\nExpected boolean, number, string or userdata.", n+i); | |
| 76 return NULL; | |
| 77 } | |
| 78 m->nargs++; | |
| 79 } | |
| 80 return m; | |
| 81 } | |
| 82 | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
83 Event::Event() |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
84 { |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
85 mutex = thread::newMutex(); |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
86 } |
|
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
87 |
| 1096 | 88 Event::~Event() |
| 89 { | |
|
1098
a74672ba1320
Merge in channels from love-experiments
Bart van Strien <bart.bes+projects@gmail.com>
parents:
1096
diff
changeset
|
90 delete mutex; |
| 1096 | 91 } |
| 92 | |
| 93 void Event::push(Message *msg) | |
| 94 { | |
| 95 Lock lock(mutex); | |
| 96 msg->retain(); | |
| 97 queue.push(msg); | |
| 98 } | |
| 99 | |
| 100 bool Event::poll(Message *&msg) | |
| 101 { | |
| 102 Lock lock(mutex); | |
| 103 if (queue.empty()) | |
| 104 return false; | |
| 105 msg = queue.front(); | |
| 106 queue.pop(); | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 void Event::clear() | |
| 111 { | |
| 112 Lock lock(mutex); | |
| 113 while (!queue.empty()) | |
| 114 { | |
| 115 queue.back()->release(); | |
| 116 queue.pop(); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 } // event | |
| 121 } // love |
