wrap_Channel.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2006-2010 LOVE Development Team
  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. #include "wrap_Channel.h"
  21. namespace love
  22. {
  23. namespace thread
  24. {
  25. void retainVariant(Channel *c, Variant *v)
  26. {
  27. c->lockMutex();
  28. v->retain();
  29. c->unlockMutex();
  30. }
  31. void releaseVariant(Channel *c, Variant *v)
  32. {
  33. c->lockMutex();
  34. v->release();
  35. c->unlockMutex();
  36. }
  37. Channel *luax_checkchannel(lua_State *L, int idx)
  38. {
  39. return luax_checktype<Channel>(L, idx, "Channel", THREAD_CHANNEL_T);
  40. }
  41. int w_Channel_push(lua_State *L)
  42. {
  43. Channel *c = luax_checkchannel(L, 1);
  44. Variant *var = lua_isnoneornil(L, 2) ? 0 : Variant::fromLua(L, 2);
  45. if (!var)
  46. return luaL_argerror(L, 2, "boolean, number, string, or love type expected");
  47. c->push(var);
  48. releaseVariant(c, var);
  49. return 0;
  50. }
  51. int w_Channel_supply(lua_State *L)
  52. {
  53. Channel *c = luax_checkchannel(L, 1);
  54. Variant *var = lua_isnoneornil(L, 2) ? 0 : Variant::fromLua(L, 2);
  55. if (!var)
  56. return luaL_argerror(L, 2, "boolean, number, string, or love type expected");
  57. c->supply(var);
  58. releaseVariant(c, var);
  59. return 0;
  60. }
  61. int w_Channel_pop(lua_State *L)
  62. {
  63. Channel *c = luax_checkchannel(L, 1);
  64. Variant *var = c->pop();
  65. if (var)
  66. {
  67. var->toLua(L);
  68. releaseVariant(c, var);
  69. }
  70. else
  71. lua_pushnil(L);
  72. return 1;
  73. }
  74. int w_Channel_demand(lua_State *L)
  75. {
  76. Channel *c = luax_checkchannel(L, 1);
  77. Variant *var = c->demand();
  78. var->toLua(L);
  79. releaseVariant(c, var);
  80. return 1;
  81. }
  82. int w_Channel_peek(lua_State *L)
  83. {
  84. Channel *c = luax_checkchannel(L, 1);
  85. Variant *var = c->peek();
  86. if (var)
  87. {
  88. var->toLua(L);
  89. releaseVariant(c, var);
  90. }
  91. else
  92. lua_pushnil(L);
  93. return 1;
  94. }
  95. int w_Channel_getCount(lua_State *L)
  96. {
  97. Channel *c = luax_checkchannel(L, 1);
  98. lua_pushnumber(L, c->getCount());
  99. return 1;
  100. }
  101. int w_Channel_clear(lua_State *L)
  102. {
  103. Channel *c = luax_checkchannel(L, 1);
  104. c->clear();
  105. return 0;
  106. }
  107. static const luaL_Reg type_functions[] = {
  108. { "push", w_Channel_push },
  109. { "supply", w_Channel_supply },
  110. { "pop", w_Channel_pop },
  111. { "demand", w_Channel_demand },
  112. { "peek", w_Channel_peek },
  113. { "getCount", w_Channel_getCount },
  114. { "clear", w_Channel_clear },
  115. { 0, 0 }
  116. };
  117. extern "C" int luaopen_channel(lua_State *L)
  118. {
  119. return luax_register_type(L, "Channel", type_functions);
  120. }
  121. }
  122. }