Debian Codegraph @

wdoc/ace_display_struct.html

raw
1 <HTML>
2
3 <HEAD>
4 <TITLE> ACEDB source code documentation: programming ace displays </TITLE>
5
6 <LINK rel=stylesheet type="text/css"
7 href="./wdoc.css"
8 title="Style sheet for wdoc developer documents">
9
10 </HEAD>
11
12 <BODY>
13
14 <H1>Application programmers' guide to ace displays</H1>
15
16 <P>This is a prelimininary discussion document. New displays will be
17 written according to this spec and old ones will gradually be
18 converted.</P>
19
20 <P>We're discussing the window handling, the data structures, their names and their memory handling.</P>
21
22 <P>The primary ideas about an ace-display are :</P>
23
24 <OL>
25 <LI>The window display the data of a specific object.
26 <LI>All data calculated about the object is held in a datastructure.
27 <LI>This data structure is attached to the window using graphAssociators.
28 <LI>The memory of the datastructure has to be re-claimed if the window dies.
29 <LI>The graph has to be cleared if the data-structure is destroyed.<BR>
30 This may occur, if the handle upon which the datastructure was allocated is destroyed.
31 </OL>
32
33 This is a model of a fictitious display for another Acedb Map :
34
35 <PRE>
36 +--------------------------+
37 |O| AMAP:dj9812 |X|
38 +--------------------------+ +-----------------------------------+
39 | = | | struct AceMapStruct |
40 | =_ _ gene |------> | { |
41 | = - = unc-1 | | &lt;standard struct-members&gt; |
42 | dj9812 =_ = | | &lt;specific struct-members&gt; |
43 | _- _ | | } |
44 | | - | +-----------------------------------+
45 | - = |
46 +--------------------------+
47 </PRE>
48
49 <P>The way the window and the data-structure is created and destroyed
50 needs standardization, some of that can hopefully be provided by a set
51 of wrapper functions in the display-Package.</P>
52
53 <H2>The layout of the data-structure and naming conventions :</H2>
54
55 <P>Let's assume that the name for this display is "<B>AceMap</B>", another display
56 along the lines of FMAP or GMAP.</P>
57
58 <H3>Magic identifiers and associators</H3>
59
60 <P>Every graph has an associator attached : an associator lets you register a void*
61 upon a unique identifier. This unique identifier is another pointer-type, and we will use
62 the address of a static string-constant. The unique address of that string is guaranted by the
63 compiler, which during link-time will fit the constant string within the address-space of the program.
64 <BR>
65 When the window is created and it's data-structure filled with appropriate information, the
66 graphAssociator for the active graph will be registered to return the address of the data-structure
67 if it is presented with the string's magic address on that window again.
68 </P>
69
70 The magic associator string for our example display would be declared like this :
71 <PRE>
72 static magic_t GRAPH2AceMap_ASSOC = "GRAPH2AceMap";
73 </PRE>
74
75 <P>It is because C has very poor type-checking and no generic
76 data-types, that the graphAss-package uses <CODE>void*</CODE> as the
77 address to the window's data-structure.
78 <BR>
79 The pointer returned by graphAssFind (upon the active graph) has to be
80 type-cast from <CODE>void*</CODE> to the type of
81 <CODE>AceMapStruct</CODE>. Pointer type-casts are always a
82 dangerous thing to do, because the dereferencing of structures is just
83 a very crude memory look-up in C.
84 </P>
85
86 <P>Therefore, one of the <B>&lt;standard struct-members&gt;</B> is a
87 magic identifier, which is used to verify that the <CODE>void*</CODE>
88 pointer is what we expect. After that we know that the type-cast was
89 safe and we can derefence the <CODE>void*</CODE> pointer based on the
90 struct definition of <CODE>AceMapStruct</CODE>.</P>
91
92 <P>The magic identifier is again a unique pointer, generated by the
93 compiler as the address to a static string constant. The magic id is
94 declared in the following way :</P>
95
96 <PRE>
97 static magic_t AceMap_MAGIC = "AceMap";
98 </PRE>
99
100 One of the standard ace-display struct members is <CODE>-&gt;magic</CODE>
101 which is assigned to <CODE>&amp;AceMap_MAGIC</CODE> when the data-structure
102 is created and filled with data.</P>
103
104 <H3>Memory handling</H3>
105
106 <P>Above we mentioned two goals about the memory handling of
107 ace-displays. The graph should be destroyable by the usual
108 <CODE>graphDestroy()</CODE> menu-action or a window-managers "CLOSE"
109 signal and upon dying the memory of the data-strcuture needs to be
110 reclaimed cleanly. On the other hand we want to be able to create an
111 AceMapStruct and destroy it, which should clear up the graph.<P>
112
113 <P>The function creating the graph needs to create the data-structure and graphAssociate it with itself.
114 The graph's destructor function needs to disassociate the data-structure, with the dying graph
115 and clear its memory.</P>
116
117 <P>The function creating the data-structure needs to allocate the
118 memory of the struct upon a given handle, register the finalisation
119 function, assign it's magic identifier, assign the graph to be the
120 one we've just opened and fill in all the data.</P>
121
122 <P>The window is created by the standard <CODE>DisplayFunc</CODE>,
123 whose type is defined in <CODE>acedb.h</CODE> (<I>should move to
124 display.h</I>) as</P>
125
126 <PRE>
127 typedef BOOL (*DisplayFunc)(KEY key, KEY from, BOOL isOldDisplay) ;
128 </PRE>
129
130 <P>A window that has a display-function of this type can be registered
131 and configured using the wspec/displays.wrm configuration file.</P>
132
133 The structure of the AceMap display module so far is :
134
135 <PRE>
136
137 /*******************************************/
138
139 static magic_t GRAPH2AceMap_ASSOC = "GRAPH2AceMap";
140 static magic_t AceMap_MAGIC = "AceMap";
141
142 typedef struct AceMapStruct
143 {
144 /* standard display-struct members */
145 magic_t *magic; /* == &amp;AceMap_MAGIC */
146 STORE_HANDLE handle;
147 Graph graph;
148 KEY originKey;
149
150 <I>
151 /* AceMap display struct members */
152 Array exampleArray;
153 char *exampleString;
154 </I>
155 } *AceMap;
156
157 /*******************************************/
158
159 static AceMap currentAceMap (char *caller)
160 /* return AceMap structure associated with the active graph */
161 {
162 AceMap amap;
163
164 if (!graphAssFind (&amp;GRAPH2AceMap_ASSOC,&amp;amap))
165 messcrash("%s() could not find AceMap on graph", caller);
166 if (!amap)
167 messcrash("%s() received NULL AceMap pointer", caller);
168 if (amap-&gt;magic != &amp;AceMap_MAGIC)
169 messcrash("%s() received non-magic AceMap pointer", caller);
170
171 return amap;
172 } /* currentAceMap */
173
174 /*******************************************/
175
176 static void aceMapFinalise (void *vp)
177 /* Finalisation function for AceMap type */
178 {
179 AceMap amap = (AceMap)vp;
180 Graph old = graphActive ();
181
182 if (graphActivate (amap-&gt;graph))
183 {
184 graphDestroy (); /* let the graph-destructor do the rest */
185 graphActivate (old);
186 }
187 else
188 handleDestroy (amap-&gt;handle);
189
190 return;
191 } /* aceMapFinalise */
192
193 static AceMap aceMapCreate (KEY key, KEY from)
194 /* Constructor function for AceMap type */
195 {
196 AceMap amap = NULL;
197
198 if (aceMapPossibleFor(key))
199 {
200 amap = (AceMap)messalloc(sizeof(struct AceMapStruct));
201 blockSetFinalise (amap, aceMapFinalise);
202 amap-&gt;magic = &amp;AceMap_MAGIC;
203 amap-&gt;handle = handleCreate();
204 amap-&gt;graph = 0;
205 amap-&gt;originKey = key;
206
207 <I>
208 /* now the AceMap specifics */
209 amap-&gt;exampleArray = arrayHandleCreate(10, amap-&gt;handle);
210 amap-&gt;exampleString = strnew ("example", amap-&gt;handle);
211 </I>
212 }
213
214 return amap;
215 } /* aceMapCreate */
216
217
218 static void aceMapDestroy (void)
219 /* graph-destructor for AceMap display */
220 {
221 AceMap amap = currentAceMap ("aceMapDestroy");
222
223 amap-&gt;graph = 0; /* graph is dying already */
224 messfree (amap);
225 } /* aceMapDestroy */
226
227
228 static void aceMapRefresh (void)
229 {
230 AceMap amap = currentAceMap("aceMapRefresh");
231
232 aceMapDraw (amap, 0);
233
234 return;
235 } /* aceMapRefresh */
236
237
238 static void aceMapDraw (AceMap amap, KEY from)
239 {
240 /* display object amap->originKey, but center on object 'from' */
241
242 return;
243 } /* aceMapRefresh */
244
245
246 BOOL aceMapDisplay (KEY key, KEY from, BOOL isOldGraph)
247 /* graph-constructor for AceMap display */
248 {
249 AceMap amap;
250
251 if (isOldGraph &amp;&amp;
252 /* there is a window of type "AceMap already" */
253 graphAssFind(&amp;GRAPH2AceMap_ASSOC, &amp;amap) &amp;&amp;
254 amap -&gt;magic == &amp;AceMap_MAGIC &amp;&amp;
255 amap-&gt;originKey == key)
256 {
257 /* That existing graph is already showing object we're asking for
258 just redraw for new position based on the 'from'-object */
259 aceMapDraw (amap, from);
260 return TRUE;
261 }
262
263 amap = aceMapCreate (key, from);
264
265 if (!amap)
266 {
267 /* can't create an AceMap, no data or similar problem
268 * so we show the object in the text display instead */
269 display (key, 0, TREE);
270 return FALSE;
271 }
272
273
274 /* open new display of type "AceMap" */
275 if (!displayCreate ("AceMap"))
276 return FALSE;
277
278 graphRetitle (name(key));
279 graphRegister (RESIZE, aceMapRefresh) ;
280 graphRegister (DESTROY, aceMapDestroy) ;
281
282 graphAssociate (&amp;GRAPH2AceMap_ASSOC, amap);
283 amap-&gt;graph = graphActive();
284
285 aceMapDraw (amap, from);
286
287 return TRUE;
288 } /* aceMapDisplay */
289
290 /*******************************************/
291
292 </PRE>
293
294 </BODY>
295 </HTML>