ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/cfperl.h
Revision: 1.6
Committed: Sat Aug 26 08:44:06 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +67 -10 lines
Log Message:
many, many cleanups

File Contents

# User Rev Content
1 root 1.1 //
2     // cfperl.h perl interface
3     //
4 root 1.2 #ifndef CFPERL_H__
5     #define CFPERL_H__
6    
7 root 1.6 struct pl;
8     struct object;
9     struct mapstruct;
10    
11 root 1.2 // could have used templates, but a more traditional C api
12     // uses more explicit typing which is ok for this purpose.
13 root 1.6 enum data_type
14     {
15 root 1.2 DT_END, // no further arguments
16     DT_INT,
17 root 1.5 DT_INT64,
18 root 1.2 DT_DOUBLE,
19     DT_STRING, // 0-terminated string
20     DT_DATA, // string + length
21     DT_OBJECT,
22     DT_MAP,
23     DT_PLAYER,
24     DT_ARCH,
25     DT_PARTY,
26     DT_REGION,
27 root 1.5
28     // following are only for cfapi-compatibility
29     DT_INT_PTR,
30     DT_INT64_PTR,
31     DT_DOUBLE_PTR,
32 root 1.2 };
33    
34 root 1.6 enum event_klass
35     {
36 root 1.3 KLASS_GLOBAL,
37     KLASS_PLAYER,
38     KLASS_OBJECT,
39     KLASS_MAP,
40     };
41    
42 root 1.6 enum event_type
43     {
44 root 1.5 # define def(klass,name) EV_ ## klass ## _ ## name,
45 root 1.2 # include "eventinc.h"
46     # undef def
47 root 1.4 NUM_EVENT_TYPES
48 root 1.2 };
49    
50 root 1.3 #define ARG_INT(v) DT_INT , static_cast<int> (v)
51 root 1.5 #define ARG_INT64(v) DT_INT64 , static_cast<sint64> (v)
52 root 1.3 #define ARG_DOUBLE(v) DT_DOUBLE, static_cast<double> (v)
53 root 1.5 #define ARG_STRING(v) DT_STRING, static_cast<const char *> (v)
54     #define ARG_DATA(s,l) DT_DATA , static_cast<const void *> (s), (l)
55 root 1.6 #define ARG_OBJECT(o) DT_OBJECT, (void *)static_cast<struct object *> (o)
56     #define ARG_MAP(o) DT_MAP , (void *)static_cast<struct mapstruct *> (o)
57     #define ARG_PLAYER(o) DT_PLAYER, (void *)static_cast<struct pl *> (o)
58     #define ARG_ARCH(o) DT_ARCH , (void *)static_cast<struct archetype *> (o)
59     #define ARG_PARTY(o) DT_PARTY , (void *)static_cast<struct party *> (o)
60     #define ARG_REGION(o) DT_REGION, (void *)static_cast<struct region *> (o)
61 root 1.3
62     // the ", ## __VA_ARGS" is, unfortunately, a gnu-cpp extension
63 root 1.2
64 root 1.4 // all these return true when the normal event processing should be skipped (if any)
65 root 1.5 #define INVOKE(klass, event, ...) cfperl_invoke (EV_ ## klass ## _ ## event, ## __VA_ARGS__, DT_END)
66 root 1.1
67 root 1.3 #define INVOKE_GLOBAL(event, ...) INVOKE (GLOBAL, event, ## __VA_ARGS__)
68     #define INVOKE_OBJECT(event, op, ...) INVOKE (OBJECT, event, ARG_OBJECT (op), ## __VA_ARGS__)
69     #define INVOKE_PLAYER(event, pl, ...) INVOKE (PLAYER, event, ARG_PLAYER (pl), ## __VA_ARGS__)
70     #define INVOKE_MAP(event, map, ...) INVOKE (MAP , event, ARG_MAP (map) , ## __VA_ARGS__)
71    
72 root 1.6 struct extendable_base
73     {
74     void *self, *cb; // CF+ perl self and callback
75     const char *attach; // generic extension attachment information
76    
77     void clear (); // called when free'ing objects
78     void optimise (); // possibly save some memory by destroying unneeded data
79     void reattach (data_type type, void *self); // called after swapin
80     void instantiate (data_type type, void *self); // called on first instantiation
81     void clone (data_type type, void *self, void *dest);
82     };
83    
84     // objects extendable from perl (or any other extension) should include or
85     // derive using the curiously recurring template pattern, to avoid
86     // virtual method calls etc.
87     template<class subclass>
88     struct extendable : extendable_base
89     {
90     void clear ()
91     {
92     if (self || cb)
93     extendable_base::clear ();
94     }
95     void instantiate ()
96     {
97     if (attach)
98     extendable_base::instantiate (
99     static_cast<subclass *>(this)->get_dt (),
100     static_cast<subclass *>(this)
101     );
102     }
103     void reattach ()
104     {
105     if (attach)
106     extendable_base::reattach (
107     static_cast<subclass *>(this)->get_dt (),
108     static_cast<subclass *>(this)
109     );
110     }
111     void clone (subclass *destination)
112     {
113     if (self || cb)
114     extendable_base::clone (
115     static_cast<subclass *>(this)->get_dt (),
116     static_cast<subclass *>(this),
117     destination
118     );
119     }
120     };
121    
122 root 1.4 bool cfperl_invoke (event_type event, ...);
123 root 1.6 void cfperl_free_ob (struct object *op);
124 root 1.3
125     void cfperl_init ();
126 root 1.1 void cfperl_main ();
127 root 1.2
128     #endif
129 root 1.3