Added flag loding load valued to avoid error for nonexistant values (for options)
[tagua/yd.git] / src / luaapi / loader.h
blobe494193918e26fc40d24babb815e86f1d662579c
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef LUAAPI__LOADER_H
12 #define LUAAPI__LOADER_H
14 #include <map>
15 #include <vector>
16 #include <boost/variant.hpp>
17 #include <QDir>
18 #include <QImage>
19 #include "loader/image.h"
20 #include "option.h"
22 class lua_State;
23 class luaL_Reg;
25 namespace Loader {
26 class Context;
29 namespace LuaApi {
31 class RectLess {
32 public:
33 bool operator()(const QRect& a, const QRect& b) {
34 return a.x() != b.x() ? a.x() < b.x() :
35 a.y() != b.y() ? a.y() < b.y() :
36 a.width() != b.width() ? a.width() < b.width() :
37 a.height() < b.height();
41 typedef std::map<QRect, QImage, RectLess> ImageMap;
42 typedef boost::variant<QImage, ImageMap> ImageOrMap;
44 class Loader {
45 bool m_error;
46 QString m_error_string;
48 lua_State* m_state;
49 QDir m_curr_dir;
51 static const luaL_Reg lualibs[];
53 template<typename T> struct create_value_data;
54 template<typename T> static void retrieve(create_value_data<T>*, lua_State *l, int pos);
55 template<typename T> static int create_value_func(lua_State *l);
57 static int import_func(lua_State *l);
59 public:
60 Loader(::Loader::Context *ctx);
61 ~Loader();
63 lua_State* state() const { return m_state; }
64 bool runFile(const QString& file, bool set_dir = true);
66 template<typename T> T getValue(const QString& key, int size = 0, bool allow_nil = false);
68 bool error(){ return m_error; }
69 void clearError(){ m_error = false; }
70 QString errorString(){ return m_error_string; }
73 } // namespace LuaLoader
76 #endif // LUAAPI__LOADERAPI_H