Mercurial > love-experiments
annotate src/common/Variant.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 | b7f097053d74 |
| 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 "Variant.h" | |
| 22 #include "common/StringMap.h" | |
| 23 | |
| 24 namespace love | |
| 25 { | |
| 26 | |
| 27 extern StringMap<Type, TYPE_MAX_ENUM> types; | |
| 28 | |
| 29 love::Type extractudatatype(lua_State *L, int idx) | |
| 30 { | |
| 31 Type t = INVALID_ID; | |
| 32 if (!lua_isuserdata(L, idx)) | |
| 33 return t; | |
| 34 if (luaL_getmetafield(L, idx, "__tostring") == 0) | |
| 35 return t; | |
| 36 lua_pushvalue(L, idx); | |
| 37 int result = lua_pcall(L, 1, 1, 0); | |
| 38 if (result == 0) | |
| 39 types.find(lua_tostring(L, -1), t); | |
| 40 if (result == 0 || result == LUA_ERRRUN) | |
| 41 lua_pop(L, 1); | |
| 42 return t; | |
| 43 } | |
| 44 | |
| 45 Variant::Variant(bool boolean) | |
| 46 { | |
| 47 type = BOOLEAN; | |
| 48 data.boolean = boolean; | |
| 49 } | |
| 50 | |
| 51 Variant::Variant(double number) | |
| 52 { | |
| 53 type = NUMBER; | |
| 54 data.number = number; | |
| 55 } | |
| 56 | |
| 57 Variant::Variant(const char *string, size_t len) | |
| 58 { | |
| 59 type = STRING; | |
| 60 char *buf = new char[len+1]; | |
| 61 memset(buf, 0, len+1); | |
| 62 memcpy(buf, string, len); | |
| 63 data.string.str = buf; | |
| 64 data.string.len = len; | |
| 65 } | |
| 66 | |
| 67 Variant::Variant(char c) | |
| 68 { | |
| 69 type = CHARACTER; | |
| 70 data.character = c; | |
| 71 } | |
| 72 | |
| 73 Variant::Variant(void *userdata) | |
| 74 { | |
| 75 type = LUSERDATA; | |
| 76 data.userdata = userdata; | |
| 77 } | |
| 78 | |
| 79 Variant::Variant(love::Type udatatype, void *userdata) | |
| 80 { | |
| 81 type = FUSERDATA; | |
| 82 this->udatatype = udatatype; | |
| 83 if (udatatype != INVALID_ID) | |
| 84 { | |
| 85 Proxy *p = (Proxy *) userdata; | |
| 86 flags = p->flags; | |
| 87 data.userdata = p->data; | |
| 88 ((love::Object *) data.userdata)->retain(); | |
| 89 } | |
| 90 else | |
| 91 data.userdata = userdata; | |
| 92 } | |
| 93 | |
| 94 Variant::~Variant() | |
| 95 { | |
| 96 switch (type) | |
| 97 { | |
| 98 case STRING: | |
| 99 delete[] data.string.str; | |
| 100 break; | |
| 101 case FUSERDATA: | |
| 102 ((love::Object *) data.userdata)->release(); | |
| 103 break; | |
| 104 default: | |
| 105 break; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 Variant *Variant::fromLua(lua_State *L, int n) | |
| 110 { | |
| 111 Variant *v = NULL; | |
| 112 size_t len; | |
| 113 const char *str; | |
| 114 switch (lua_type(L, n)) | |
| 115 { | |
| 116 case LUA_TBOOLEAN: | |
| 117 v = new Variant(luax_toboolean(L, n)); | |
| 118 break; | |
| 119 case LUA_TNUMBER: | |
| 120 v = new Variant(lua_tonumber(L, n)); | |
| 121 break; | |
| 122 case LUA_TSTRING: | |
| 123 str = lua_tolstring(L, n, &len); | |
| 124 v = new Variant(str, len); | |
| 125 break; | |
| 126 case LUA_TLIGHTUSERDATA: | |
| 127 v = new Variant(lua_touserdata(L, n)); | |
| 128 break; | |
| 129 case LUA_TUSERDATA: | |
| 130 v = new Variant(extractudatatype(L, n), lua_touserdata(L, n)); | |
| 131 break; | |
| 132 } | |
| 133 return v; | |
| 134 } | |
| 135 | |
| 136 void Variant::toLua(lua_State *L) | |
| 137 { | |
| 138 switch (type) | |
| 139 { | |
| 140 case BOOLEAN: | |
| 141 lua_pushboolean(L, data.boolean); | |
| 142 break; | |
| 143 case CHARACTER: | |
| 144 lua_pushlstring(L, &data.character, 1); | |
| 145 break; | |
| 146 case NUMBER: | |
| 147 lua_pushnumber(L, data.number); | |
| 148 break; | |
| 149 case STRING: | |
| 150 lua_pushlstring(L, data.string.str, data.string.len); | |
| 151 break; | |
| 152 case LUSERDATA: | |
| 153 lua_pushlightuserdata(L, data.userdata); | |
| 154 break; | |
| 155 case FUSERDATA: | |
| 156 if (udatatype != INVALID_ID) | |
| 157 { | |
| 158 const char *name = NULL; | |
| 159 love::types.find(udatatype, name); | |
| 160 ((love::Object *) data.userdata)->retain(); | |
| 161 luax_newtype(L, name, flags, data.userdata); | |
| 162 } | |
| 163 else | |
| 164 lua_pushlightuserdata(L, data.userdata); | |
| 165 // I know this is not the same | |
| 166 // sadly, however, it's the most | |
| 167 // I can do (at the moment). | |
| 168 break; | |
| 169 default: | |
| 170 lua_pushnil(L); | |
| 171 break; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 } // love |
