Attributes._pmod
Go to the documentation of this file.
1 /* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
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.
7  *
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.
12  *
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
16  *
17  * $Id: Attributes.pmod,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19  inherit Attribute;
20  inherit Attribute;
21  inherit Attribute;
22 #include <attributes.h>
23 #include <exception.h>
24 #include <events.h>
25 #include <types.h>
26 #include <macros.h>
27 
28 
29 
30 
31 #define WRONG_TYPE(key, expected, data) THROW("Wrong value to attribute "+ key +" (expected "+expected+" got "+sprintf("%O",_typeof(data))+").", E_ERROR)
32 
33 /** @defgroup attribute
34  * Attribute functions
35  */
36 
37 
38 /**
39  * @ingroup attribute
40  */
41 class Attribute {
42 public:
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
47  int a_type;
48  mixed a_defval; // the default value
49  object converter;
50 
51  // value is stored somewhere else ...
52  // if set to a string a function name is meant
53  string|object a_acquire;
54 
55  // coupled events
56  int a_event_read;
57  int a_event_write;
58 
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; }
73 
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)
77  {
78  a_key = key;
79  a_desc = desc;
80  a_type = type;
81  a_cntrl = cntrl;
82  a_acquire = acq;
83  a_defval = def_val;
84  a_event_read = read_event;
85  if ( zero_type(write_event) )
86  a_event_write = EVENT_ATTRIBUTES_CHANGE;
87  else
88  a_event_write = write_event;
89  }
90  int check_convert(object obj) {
91  if ( objectp(converter) ) {
92  if ( functionp(converter->convert_attribute) ) {
93  converter->convert_attribute(this_object(), obj);
94  return 1;
95  }
96  }
97  return 0;
98  }
99 
100  bool check_attribute(mixed data)
101  {
102  switch(a_type) {
103  case CMD_TYPE_INT:
104  if ( !intp(data) ) WRONG_TYPE(a_key, "int", data);
105  break;
106  case CMD_TYPE_FLOAT:
107  if ( !floatp(data) ) WRONG_TYPE(a_key, "float", data);
108  break;
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 + " !");
113  break;
114  case CMD_TYPE_OBJECT:
115  if ( !objectp(data) ) WRONG_TYPE(a_key, "object", data);
116  break;
117  case CMD_TYPE_ARRAY:
118  if ( !arrayp(data) ) WRONG_TYPE(a_key, "array", data);
119  break;
120  case CMD_TYPE_MAPPING:
121  if ( !mappingp(data) ) WRONG_TYPE(a_key, "mapping", data);
122  break;
123  case CMD_TYPE_PROGRAM:
124  if ( !programp(data) ) WRONG_TYPE(a_key, "program", data);
125  break;
126  case CMD_TYPE_FUNCTION:
127  if ( !programp(data) ) WRONG_TYPE(a_key, "function", data);
128  break;
129  }
130  return true;
131  }
132 
133  int `==(Attribute a) {
134  if ( !objectp(a) || !functionp(a->get_default_value) )
135  return 0;
136  return
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();
141  }
142  mapping serialize_coal() {
143  return ([ "key": a_key,
144  "type": a_type,
145  "description": a_desc,
146  "eventRead": a_event_read,
147  "eventWrite": a_event_write,
148  "acquire": a_acquire,
149  "default": a_defval,
150  "control": a_cntrl,
151  "converter": converter, ]);
152  }
153 
154  mixed `[](int index) {
155  switch(index) {
156  case REGISTERED_TYPE:
157  return a_type;
158  case REGISTERED_DESC:
159  return a_desc;
160  case REGISTERED_EVENT_READ:
161  return a_event_read;
162  case REGISTERED_EVENT_WRITE:
163  return a_event_write;
164  case REGISTERED_ACQUIRE:
165  return a_acquire;
166  case REGISTERED_DEFAULT:
167  return a_defval;
168  case REGISTERED_CONTROL:
169  return a_cntrl;
170  }
171  }
172 
173  string describe() {
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)))+ ")";
179  }
180  string _sprintf() {
181  return describe();
182  }
183 
184  mapping save() {
185  return ([
186  "key": a_key,
187  "desc": a_desc,
188  "acquire": a_acquire,
189  "def": a_defval,
190  "type": a_type,
191  "control": a_cntrl,
192  "event_read": a_event_read,
193  "event_write": a_event_write,
194  "converter": converter,
195  ]);
196  }
197 
198  void load(mapping data) {
199  }
200 }
201 
202 class UserAttribute {
203 public:
204 
205  void create(string|int key, string desc, int type, mixed def_val)
206  {
207  ::create(key, desc, type, def_val,
208  0,
209  CONTROL_ATTR_SERVER,
210  0,
211  EVENT_ATTRIBUTES_CHANGE);
212  }
213 }
214 
215 class PositionAttribute {
216 public:
217 
218  void create(string|int key, string desc, int type, mixed def_val) {
219  ::create(key, desc, type, def_val,
220  0,
221  CONTROL_ATTR_CLIENT,
222  0,
223  EVENT_ARRANGE_OBJECT);
224  }
225 }
226 
227 class FreeAttribute {
228 public:
229 
230  void create(string|int key, string desc, int type, mixed def_val)
231  {
232  ::create(key, desc, type, def_val,
233  0,
234  CONTROL_ATTR_SERVER,
235  0,
236  0);
237  }
238 }
239 
240 
241 };