source: golgotha/src/i4/lisp/li_class.hh @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.0 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef LI_CLASS_HH
10#define LI_CLASS_HH
11
12
13#include "lisp/lisp.hh"
14#include "memory/fixed_array.hh"
15
16
17class li_class_type;
18struct sym_var;
19
20
21struct li_class_member
22{
23  char *name;
24  int offset;
25  li_type_number class_type;
26  li_symbol *sym;
27
28  li_class_member(char *name) : name(name), sym(0), class_type(0) {}
29};
30
31struct li_string_class_member : public li_class_member
32{
33  li_string_class_member(char *name) : li_class_member(name) {}
34  char *operator()();
35};
36
37struct li_int_class_member : public li_class_member
38{
39  li_int_class_member(char *name) : li_class_member(name) {}
40  int &operator()();
41};
42
43
44struct li_float_class_member : public li_class_member
45{
46  li_float_class_member(char *name) : li_class_member(name) {}
47  float &operator()();
48};
49
50
51struct li_symbol_class_member : public li_class_member
52{
53  li_symbol_class_member(char *name) : li_class_member(name) {}
54  li_symbol *&operator()();
55};
56
57
58struct li_object_class_member : public li_class_member
59{
60  li_object_class_member(char *name) : li_class_member(name) {}
61  li_object *&operator()();
62};
63
64
65class li_class : public li_object
66{
67  void **values;
68  li_class_type *get_type() const { return (li_class_type *)li_get_type(type()); }
69public:
70  void mark(int set);
71
72  void save(i4_saver_class *fp, li_environment *env);
73  void load(i4_loader_class *fp, li_type_number *type_remap, li_environment *env);
74  void print(i4_file_class *fp);
75
76  li_class(li_type_number class_type, li_object *params=0, li_environment *env=0);
77
78  void free();
79
80  int member_offset(char *sym) const;    // does not type check
81  int member_offset(li_symbol *sym) const;    // does not type check
82
83  int get_offset(li_class_member &m) const;
84  int get_offset(li_class_member &m, li_type_number _type) const;
85
86  int member_offset(li_class_member &m) const   // inline version checks to see to see if cached
87  {
88    if (m.class_type!=type())
89      get_offset(m);
90    return m.offset;
91  }
92
93  int member_offset(li_class_member &m, li_type_number _type) const
94  {
95    if (m.class_type!=type())
96      get_offset(m, _type);
97    return m.offset;
98  }
99
100
101
102  char *string_value(int member) const { return ((li_string *)values[member])->value(); }
103  int &int_value(int member) { return *(((int *) (values+member))); }
104  float &float_value(int member) { return *(((float *) (values+member))); }
105  li_symbol *&symbol_value(int member)  { return *((li_symbol **)(values+member)); }
106  li_object *&object_value(int member)  { return *((li_object **)(values+member)); }
107
108  char *get(li_string_class_member &c) const { return string_value(member_offset(c, LI_STRING)); }
109  int &get(li_int_class_member &c)  {  return int_value(member_offset(c, LI_INT)); }
110  float &get(li_float_class_member &c) { return float_value(member_offset(c, LI_FLOAT)); }
111  li_symbol *&get(li_symbol_class_member &c) { return symbol_value(member_offset(c, LI_SYMBOL)); }
112  li_object *&get(li_class_member &c) { return object_value(member_offset(c)); }
113
114
115  li_object *value(char *member_name);
116  li_object *value(int member);
117  void set_value(int member, li_object *value);
118  void set(li_class_member &c, li_object *value) { set_value(member_offset(c), value); }
119  li_object *set(char *member_name, li_object *value); // slow, but easy way to access data
120
121#ifdef LI_TYPE_CHECK
122  static li_class *get(li_object *o, li_environment *env);   
123#else
124  static li_class *get(li_object *o, li_environment *env)  { return ((li_class *)o); }
125#endif
126
127
128};
129
130li_object *li_def_class(li_object *fields, li_environment *env);
131
132int li_class_total_members(li_type_number type);
133li_symbol *li_class_get_symbol(li_type_number type, int member_number);
134li_object *li_class_get_default(li_type_number type, li_symbol *sym);
135li_object *li_class_get_property_list(li_type_number type, li_symbol *sym);
136void li_set_class_editor(li_type_edit_class *editor);
137
138extern li_class *li_this;
139
140inline char *li_string_class_member::operator()() { return li_this->get(*this); }
141inline int &li_int_class_member::operator()() { return li_this->get(*this); }
142inline float &li_float_class_member::operator()() { return li_this->get(*this); }
143inline li_symbol *&li_symbol_class_member::operator()() { return li_this->get(*this); }
144inline li_object *&li_object_class_member::operator()() { return li_this->get(*this); }
145
146class li_class_context
147{
148  li_class *old_context;
149public:
150  li_class_context(li_class *current_context)
151  {
152    old_context=li_this;
153    li_this=current_context;
154  }
155
156  ~li_class_context() { li_this=old_context; }
157};
158
159#endif
Note: See TracBrowser for help on using the repository browser.