src/lua_func.h
author Tero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 06:20:59 +0300
changeset 143 1edab39c88a8
child 145 a5582e1a83da
permissions -rw-r--r--
implement lua_args_parse
#ifndef LUA_FUNC_H
#define LUA_FUNC_H

/**
 * @file
 *
 * Convenience functions for working with lua C functions
 */
#include "lua_type.h"
#include <stdbool.h>

/**
 * Lua function argument types
 */
enum lua_arg_type {
    LUA_ARG_INVALID,

    LUA_ARG_STRING,
    LUA_ARG_BOOL,
};

/**
 * Function argument def
 */
struct lua_func_arg {
    /** Argument name */
    const char *name;

    /** Expected type */
    enum lua_arg_type type;

    /** Default value */
    union {
        const char *string;
        int boolean;
    } def;
};

/**
 * Function def
 */
struct lua_func {
    /** Type name, or NULL */
    const char *type;

    /** Function name */
    const char *name;

    /** Help string */
    const char *help;

    /** Arguments */
    const struct lua_func_arg args[];
};

/**
 * Used as the "invalid" default value
 */
#define LUA_ARG_REQUIRED (-1)
#define LUA_ARG_STRING_REQUIRED ((const char *) (-1))

/**
 * Define a function argument
 */
#define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } }
#define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } }

/**
 * Define a function
 */
#define LUA_FUNC(type, name, help, ...) { (type), (name), (help), { __VA_ARGS__, { NULL, 0, { 0 } } } }

/**
 * Parse and return a string argument
 */
const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def);

/**
 * Parse and return a boolean argument
 */
bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def);

/**
 * Parse function arguments as defined
 */
void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...);

#endif