1 /* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: Attributes.pmod,v 1.1 2008/03/31 13:39:57 exodusd Exp $
22 #include <attributes.h>
23 #include <exception.h>
31 #define WRONG_TYPE(key, expected, data) THROW("Wrong value to attribute "+ key +" (expected "+expected+" got "+sprintf("%O",_typeof(data))+").", E_ERROR)
33 /** @defgroup attribute
43 string a_key; // the name of the attribute
44 int a_classid; // corresponding class id of this Attribute
45 string a_desc; // description of the attribute
46 int a_cntrl; // who/what controls this attribute
48 mixed a_defval; // the default value
51 // value is stored somewhere else ...
52 // if set to a string a function name is meant
53 string|object a_acquire;
59 mixed get_default_value() { return a_defval; }
60 void set_default_value(mixed v) { a_defval = v; }
61 string get_description() { return a_desc; }
62 string|int get_key() { return a_key; }
63 int get_type() { return a_type; }
64 object|function get_acquire() { return a_acquire; }
65 int get_read_event() { return a_event_read; }
66 int get_controler() { return a_cntrl; }
67 int get_class() { return a_classid; }
68 int get_write_event() { return a_event_write; }
69 void set_read_event(int re) { a_event_read = re; }
70 void set_write_event(int we) { a_event_write = we; }
71 void set_acquire(string|object acq) { a_acquire = acq; }
72 void set_converter(object script) { converter = script; }
74 void create(string|int key, string desc, int type, mixed def_val,
75 void|string|object acq, void|int cntrl,
76 void|int read_event, void|int write_event)
84 a_event_read = read_event;
85 if ( zero_type(write_event) )
86 a_event_write = EVENT_ATTRIBUTES_CHANGE;
88 a_event_write = write_event;
90 int check_convert(object obj) {
91 if ( objectp(converter) ) {
92 if ( functionp(converter->convert_attribute) ) {
93 converter->convert_attribute(this_object(), obj);
100 bool check_attribute(mixed data)
104 if ( !intp(data) ) WRONG_TYPE(a_key, "int", data);
107 if ( !floatp(data) ) WRONG_TYPE(a_key, "float", data);
109 case CMD_TYPE_STRING:
110 if ( !stringp(data) ) WRONG_TYPE(a_key, "string", data);
111 if ( !xml.utf8_check(data) )
112 steam_error("Non-utf8 data for Attribute " + a_key + " !");
114 case CMD_TYPE_OBJECT:
115 if ( !objectp(data) ) WRONG_TYPE(a_key, "object", data);
118 if ( !arrayp(data) ) WRONG_TYPE(a_key, "array", data);
120 case CMD_TYPE_MAPPING:
121 if ( !mappingp(data) ) WRONG_TYPE(a_key, "mapping", data);
123 case CMD_TYPE_PROGRAM:
124 if ( !programp(data) ) WRONG_TYPE(a_key, "program", data);
126 case CMD_TYPE_FUNCTION:
127 if ( !programp(data) ) WRONG_TYPE(a_key, "function", data);
133 int `==(Attribute a) {
134 if ( !objectp(a) || !functionp(a->get_default_value) )
137 equal(a_defval, a->get_default_value()) &&
138 a_key == a->get_key() &&
139 a_acquire == a->get_acquire() &&
140 a_type == a->get_type();
142 mapping serialize_coal() {
143 return ([ "key": a_key,
145 "description": a_desc,
146 "eventRead": a_event_read,
147 "eventWrite": a_event_write,
148 "acquire": a_acquire,
151 "converter": converter, ]);
154 mixed `[](int index) {
156 case REGISTERED_TYPE:
158 case REGISTERED_DESC:
160 case REGISTERED_EVENT_READ:
162 case REGISTERED_EVENT_WRITE:
163 return a_event_write;
164 case REGISTERED_ACQUIRE:
166 case REGISTERED_DEFAULT:
168 case REGISTERED_CONTROL:
174 return "Attribute("+a_key+", "+ a_desc + ", acq="+
175 (objectp(a_acquire) ? a_acquire->get_object_id(): a_acquire)+
176 ",default="+ (objectp(a_defval) ? a_defval->get_object_id():
177 (stringp(a_defval) || intp(a_defval) ?
178 a_defval : sprintf("%O", a_defval)))+ ")";
188 "acquire": a_acquire,
192 "event_read": a_event_read,
193 "event_write": a_event_write,
194 "converter": converter,
198 void load(mapping data) {
202 class UserAttribute {
205 void create(string|int key, string desc, int type, mixed def_val)
207 ::create(key, desc, type, def_val,
211 EVENT_ATTRIBUTES_CHANGE);
215 class PositionAttribute {
218 void create(string|int key, string desc, int type, mixed def_val) {
219 ::create(key, desc, type, def_val,
223 EVENT_ARRANGE_OBJECT);
227 class FreeAttribute {
230 void create(string|int key, string desc, int type, mixed def_val)
232 ::create(key, desc, type, def_val,