Annotate

core/objectmanager.lua @ 1015:9e31e9397cff

util.logger: Revamped logger library, but backwards-compatible for users of logger.init()
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Apr 2009 03:18:13 +0100
parent 889:bb959588bbc4
child 1522:569d58d21612
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
889
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local new_multitable = require "util.multitable".new;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local t_insert = table.insert;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local t_concat = table.concat;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local tostring = tostring;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local unpack = unpack;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local pairs = pairs;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local error = error;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local type = type;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local _G = _G;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local data = new_multitable();
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 module "objectmanager"
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 function set(...)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 return data:set(...);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 function remove(...)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 return data:remove(...);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 function get(...)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 return data:get(...);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local function get_path(path)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if type(path) == "table" then return path; end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local s = {};
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 for part in tostring(path):gmatch("[%w_]+") do
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 t_insert(s, part);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return s;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 function get_object(path)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 path = get_path(path)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 return data:get(unpack(path)), path;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 function set_object(path, object)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 path = get_path(path);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 data:set(unpack(path), object);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 data:set("ls", function(_dir)
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 local obj, dir = get_object(_dir);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if not obj then error("object not found: " .. t_concat(dir, '/')); end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 local r = {};
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 if type(obj) == "table" then
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 for key, val in pairs(obj) do
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 r[key] = type(val);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 return r;
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 end);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 data:set("get", get_object);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 data:set("set", set_object);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 data:set("echo", function(...) return {...}; end);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 data:set("_G", _G);
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
bb959588bbc4 Added core.objectmanager
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 return _M;