2 algorithm library version
9 static int liba_version_tostring(lua_State
*L
)
11 a_version
const *const ctx
= (a_version
const *)lua_touserdata(L
, 1);
15 a_version_tostr(ctx
, str
, sizeof(str
));
16 lua_pushstring(L
, str
);
22 static int liba_version_init_(lua_State
*L
, a_version
*ctx
, int arg
, int top
)
32 ctx
->extra
= (unsigned int)luaL_checkinteger(L
, arg
+ 4);
35 ctx
->third
= (unsigned int)luaL_checkinteger(L
, arg
+ 3);
38 ctx
->minor
= (unsigned int)luaL_checkinteger(L
, arg
+ 2);
41 if (lua_type(L
, arg
+ 1) == LUA_TSTRING
)
43 a_version_parse(ctx
, lua_tostring(L
, arg
+ 1));
46 ctx
->major
= (unsigned int)luaL_checkinteger(L
, arg
+ 1);
54 constructor for algorithm library version
55 @tparam[opt] integer major version major number
56 @tparam[opt] integer minor version minor number
57 @tparam[opt] integer third version third number
58 @tparam[opt] integer extra version extra number
59 @treturn a.version algorithm library version userdata
62 int liba_version_new(lua_State
*L
)
64 int const top
= lua_gettop(L
);
65 a_version
*const ctx
= lua_newclass(L
, a_version
);
66 a_zero(ctx
, sizeof(a_version
));
67 lua_registry_get(L
, liba_version_new
);
68 lua_setmetatable(L
, -2);
69 return liba_version_init_(L
, ctx
, 0, top
);
73 initialize for algorithm library version
74 @tparam a.version ctx algorithm library version userdata
75 @tparam[opt] integer major version major number
76 @tparam[opt] integer minor version minor number
77 @tparam[opt] integer third version third number
78 @tparam[opt] integer extra version extra number
79 @treturn a.version algorithm library version userdata
82 int liba_version_init(lua_State
*L
)
84 int const top
= lua_gettop(L
);
85 luaL_checktype(L
, 1, LUA_TUSERDATA
);
86 a_version
*const ctx
= (a_version
*)lua_touserdata(L
, 1);
88 return liba_version_init_(L
, ctx
, 1, top
- 1);
92 parse version string to version
93 @tparam a.version ctx algorithm library version userdata
94 @tparam string version string to be parsed
95 @treturn a.version algorithm library version userdata
98 int liba_version_parse(lua_State
*L
)
100 a_version
*const ctx
= (a_version
*)lua_touserdata(L
, 1);
103 a_version_parse(ctx
, lua_tostring(L
, 2));
111 algorithm library version check
112 @tparam integer major required major number
113 @tparam integer minor required minor number
114 @tparam integer patch required patch number
115 @treturn integer <0 library version is higher than required version
116 @treturn integer >0 library version is lower than required version
117 @treturn integer 0 library version is equal to required version
120 static int liba_version_check(lua_State
*L
)
122 a_version v
= A_VERSION_0();
123 switch (lua_gettop(L
) & 0x3)
126 v
.third
= (unsigned int)luaL_checkinteger(L
, 3);
129 v
.minor
= (unsigned int)luaL_checkinteger(L
, 2);
132 v
.major
= (unsigned int)luaL_checkinteger(L
, 1);
137 lua_pushinteger(L
, a_version_check(v
.major
, v
.minor
, v
.third
));
142 compare the version lhs with the version rhs
143 @tparam a.version lhs version userdata on the left
144 @tparam a.version rhs version userdata on the right
145 @treturn integer <0 version lhs < version rhs
146 @treturn integer >0 version lhs > version rhs
147 @treturn integer 0 version lhs == version rhs
150 int liba_version_cmp(lua_State
*L
)
152 luaL_checktype(L
, 1, LUA_TUSERDATA
);
153 luaL_checktype(L
, 2, LUA_TUSERDATA
);
154 a_version
const *const lhs
= (a_version
const *)lua_touserdata(L
, 1);
155 a_version
const *const rhs
= (a_version
const *)lua_touserdata(L
, 2);
156 lua_pushinteger(L
, a_version_cmp(lhs
, rhs
));
162 int liba_version_##func(lua_State *L) \
164 luaL_checktype(L, 1, LUA_TUSERDATA); \
165 luaL_checktype(L, 2, LUA_TUSERDATA); \
166 a_version *lhs = (a_version *)lua_touserdata(L, 1); \
167 a_version *rhs = (a_version *)lua_touserdata(L, 2); \
168 lua_pushboolean(L, a_version_##func(lhs, rhs)); \
172 version lhs is less than version rhs
173 @tparam a.version lhs version userdata on the left
174 @tparam a.version rhs version userdata on the right
175 @treturn bool result of comparison
180 version lhs is greater than version rhs
181 @tparam a.version lhs version userdata on the left
182 @tparam a.version rhs version userdata on the right
183 @treturn bool result of comparison
188 version lhs is less than or equal to version rhs
189 @tparam a.version lhs version userdata on the left
190 @tparam a.version rhs version userdata on the right
191 @treturn bool result of comparison
196 version lhs is greater than or equal to version rhs
197 @tparam a.version lhs version userdata on the left
198 @tparam a.version rhs version userdata on the right
199 @treturn bool result of comparison
204 version lhs is equal to version rhs
205 @tparam a.version lhs version userdata on the left
206 @tparam a.version rhs version userdata on the right
207 @treturn bool result of comparison
212 version lhs is not equal to version rhs
213 @tparam a.version lhs version userdata on the left
214 @tparam a.version rhs version userdata on the right
215 @treturn bool result of comparison
220 static int liba_version_set(lua_State
*L
)
222 a_version
*const ctx
= (a_version
*)lua_touserdata(L
, 1);
223 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
225 case 0x86720331: // major
226 ctx
->major
= (unsigned int)luaL_checkinteger(L
, 3);
228 case 0x87857C2D: // minor
229 ctx
->minor
= (unsigned int)luaL_checkinteger(L
, 3);
231 case 0x0241DD17: // third
232 ctx
->third
= (unsigned int)luaL_checkinteger(L
, 3);
234 case 0xFD1BE968: // extra
235 ctx
->extra
= (unsigned int)luaL_checkinteger(L
, 3);
237 case 0xB5485B9E: // alpha
239 char const *alpha
= luaL_checklstring(L
, 3, 0);
240 a_version_set_alpha(ctx
, alpha
);
243 case 0x0CD3E494: // __lt
244 case 0x0CD3E485: // __le
245 case 0x0CD3E0FC: // __eq
246 case 0xE8859EEB: // __name
247 case 0xE70C48C6: // __call
248 case 0xA65758B2: // __index
249 case 0xAEB551C6: // __newindex
250 case 0x5DA21A54: // __tostring
253 lua_getmetatable(L
, 1);
260 static int liba_version_get(lua_State
*L
)
262 a_version
const *const ctx
= (a_version
const *)lua_touserdata(L
, 1);
263 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
265 case 0x86720331: // major
266 lua_pushinteger(L
, (lua_Integer
)ctx
->major
);
268 case 0x87857C2D: // minor
269 lua_pushinteger(L
, (lua_Integer
)ctx
->minor
);
271 case 0x0241DD17: // third
272 lua_pushinteger(L
, (lua_Integer
)ctx
->third
);
274 case 0xFD1BE968: // extra
275 lua_pushinteger(L
, (lua_Integer
)ctx
->extra
);
277 case 0xB5485B9E: // alpha
279 char alpha
[sizeof(ctx
->alpha_
) + 1];
280 a_version_alpha(ctx
, alpha
);
281 lua_pushstring(L
, alpha
);
284 case 0xA65758B2: // __index
285 lua_registry_get(L
, liba_version_new
);
286 lua_int_set(L
, -1, "major", (lua_Integer
)ctx
->major
);
287 lua_int_set(L
, -1, "minor", (lua_Integer
)ctx
->minor
);
288 lua_int_set(L
, -1, "third", (lua_Integer
)ctx
->third
);
289 lua_int_set(L
, -1, "extra", (lua_Integer
)ctx
->extra
);
290 lua_str_set(L
, -1, "alpha", ".");
293 lua_getmetatable(L
, 1);
300 static int liba_version_(lua_State
*L
)
302 lua_pushcfunction(L
, liba_version_new
);
304 lua_call(L
, lua_gettop(L
) - 1, 1);
308 int luaopen_liba_version(lua_State
*L
)
311 algorithm library version number
312 @field MAJOR algorithm library version major
313 @field MINOR algorithm library version minor
314 @field PATCH algorithm library version patch
315 @field TWEAK algorithm library version tweak
318 static lua_int
const enums
[] = {
319 {"MAJOR", A_VERSION_MAJOR
},
320 {"MINOR", A_VERSION_MINOR
},
321 {"PATCH", A_VERSION_PATCH
},
322 {"TWEAK", A_VERSION_TWEAK
},
324 static lua_fun
const funcs
[] = {
325 {"check", liba_version_check
},
326 {"parse", liba_version_parse
},
327 {"init", liba_version_init
},
328 {"new", liba_version_new
},
329 {"cmp", liba_version_cmp
},
330 {"lt", liba_version_lt
},
331 {"gt", liba_version_gt
},
332 {"le", liba_version_le
},
333 {"ge", liba_version_ge
},
334 {"eq", liba_version_eq
},
335 {"ne", liba_version_ne
},
337 lua_createtable(L
, 0, A_LEN(enums
) + A_LEN(funcs
));
338 lua_int_reg(L
, -1, enums
, A_LEN(enums
));
339 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
340 lua_createtable(L
, 0, 1);
341 lua_fun_set(L
, -1, "__call", liba_version_
);
342 lua_setmetatable(L
, -2);
344 static lua_fun
const metas
[] = {
345 {"__tostring", liba_version_tostring
},
346 {"__newindex", liba_version_set
},
347 {"__index", liba_version_get
},
348 {"__call", liba_version_init
},
349 {"__eq", liba_version_eq
},
350 {"__lt", liba_version_lt
},
351 {"__le", liba_version_le
},
353 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
));
354 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
355 lua_fun_reg(L
, -1, funcs
+ 1, A_LEN(funcs
) - 1);
356 lua_str_set(L
, -1, "__name", "a.version");
357 lua_registry_set(L
, liba_version_new
);