source: golgotha/src/test/lisp/class.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 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: 2.5 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#include "main/main.hh"
10#include "init/init.hh"
11#include "lisp/lisp.hh"
12#include "lisp/li_class.hh"
13#include "file/ram_file.hh"
14
15static li_int_class_member x("x"), y("y");
16
17void i4_main(w32 argc, i4_const_str *argv)
18{
19  i4_init();
20
21  // we are going to print everything to a ram file and then dump it all out at the
22  // end with i4_warning
23  char output_buf[3001];
24  i4_ram_file_class fp(output_buf, 3000);
25 
26  li_load("class.scm");  // loads & evaluates from current directory
27
28  // find the type that was created by the script
29  li_type_number type=li_find_type("test_class");
30
31  // create a new class instance from scratch
32  // li_get_type fetch the class with type virtual functions
33  // then we call create with null parameters
34  // finally we cast the result into a li_class
35  li_class *c1=li_class::get(li_get_type(type)->create(0,0),0);
36
37  // go through all the members in the class and print out their values
38  int total_members=li_class_total_members(type);
39  for (int i=0; i<total_members; i++)
40  {
41   
42    fp.printf("member %s : value = ", li_class_get_symbol(type, i)->name()->value());
43
44    li_object *value=c1->value(i);
45    li_get_type(value->type())->print(value, &fp);
46   
47    fp.printf("\n");
48  }
49
50  // now print out the class instance defined in the script file and assigned to "test"
51  li_class *c2=li_class::get(li_get_value("test"),0);
52
53  fp.printf("test = ");
54  li_get_type(type)->print(c2, &fp);
55  fp.printf("\n");
56
57
58  // now let's change a variable in the class using a slower method
59  c2->set_value(c2->member_offset("my_string"), new li_string("Ah ha! A new string."));
60 
61  // now let's change a member using a faster cahching method
62  li_this=c2;     // need to setup this correct before we operate on the object
63  fp.printf("x old value was %d\n", x());
64  x()=y();
65  fp.printf("x new value is %d\n", x());
66
67
68  fp.printf("test = ");
69  li_get_type(type)->print(c2, &fp);
70  fp.printf("\n");
71
72
73
74  // now flush out the ram file
75  output_buf[fp.tell()]=0;
76  i4_warning(output_buf);
77
78  i4_uninit();
79}
80
Note: See TracBrowser for help on using the repository browser.