| 1 |
% Copyright 2019 by Till Tantau |
| 2 |
% Copyright 2019 by Saso Zivanovic |
| 3 |
% |
| 4 |
% This file may be distributed and/or modified |
| 5 |
% |
| 6 |
% 1. under the LaTeX Project Public License and/or |
| 7 |
% 2. under the GNU Public License. |
| 8 |
% |
| 9 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 10 |
% |
| 11 |
% Based on pgfmoduleoo.code.tex v1.8 by Till Tantau. This version adds inheritance. |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
% Support of object-oriented programming in TeX, with inheritance. |
| 16 |
|
| 17 |
|
| 18 |
% The oo support works as follows: |
| 19 |
% |
| 20 |
% The main supported concepts are classes, objects, methods, |
| 21 |
% attributes and signal/slots. A class defines a set of methods, which are, |
| 22 |
% in the end, just normal TeX macros. Once a class has been created, |
| 23 |
% it can be instantiated by calling the \pgfoonew command, resulting |
| 24 |
% in a new object. Objects are local to their group. Given an object, |
| 25 |
% you can send it a message, resulting in the method code of the |
| 26 |
% object's method to be executed. While an object exists, it has a set |
| 27 |
% of attributes whose values can change over time. Attributes values |
| 28 |
% are not local to TeX groups, rather their life-cycle is the same as |
| 29 |
% the object's (which, however, is local to the group in which the |
| 30 |
% object was declared). |
| 31 |
% |
| 32 |
% The implementation is as follows: There is an ID counter that is |
| 33 |
% increased each time an object is created. This counter is local to |
| 34 |
% the group, which means that when a group ends the counter will |
| 35 |
% revert to the previous value, allowing objects to the reused in |
| 36 |
% subsequent groups. |
| 37 |
% |
| 38 |
% A method is just a normal \TeX macro, but to call it you write |
| 39 |
% \objecthandle.methodname(parameters). The \objecthandle is a macro |
| 40 |
% that is created when you say \pgfoonew. The special object |
| 41 |
% \pgfoothis is the current object. |
| 42 |
% |
| 43 |
% Attributes are stored globally in internal TeX macros whose name is |
| 44 |
% composed of the object number and the attribute name. |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
% Declares a class |
| 50 |
% |
| 51 |
% #1 = class name |
| 52 |
% #2 = methods |
| 53 |
% |
| 54 |
% Description: |
| 55 |
% |
| 56 |
% This command declares a class for future use. Inside #2, the macro |
| 57 |
% \method can be used to declare a method. The \method macro takes a |
| 58 |
% method name, parameters (methods are normal TeX macros, after all) |
| 59 |
% and body. |
| 60 |
|
| 61 |
% base classes are put in parenthesis before the new class name, but |
| 62 |
% are optional. |
| 63 |
\def\pgfooclass{% |
| 64 |
\pgfutil@ifnextchar ({% |
| 65 |
\pgfooclass@ |
| 66 |
}{% |
| 67 |
\pgfooclass@() |
| 68 |
}% |
| 69 |
}% |
| 70 |
\long\def\pgfooclass@(#1)#2#3{% |
| 71 |
\def\pgfoo@classname{#2}% |
| 72 |
\expandafter\ifx\csname pgfooY\pgfoo@classname.@pgfooinit\endcsname\relax\else |
| 73 |
\pgferror{class #2 is already defined}% |
| 74 |
\fi |
| 75 |
\pgfoo@ciii{#2}{#1}% |
| 76 |
\expandafter\let\csname pgfooY#2@pgfoo@mro\endcsname\pgfoo@mro |
| 77 |
\let\pgfoo@origmethod=\method% |
| 78 |
\let\pgfoo@origattribute=\attribute% |
| 79 |
\let\method=\pgfoo@declaremethod% |
| 80 |
\let\attribute=\pgfoo@declareattribute% |
| 81 |
\let\pgfoo@attributes=\pgfutil@empty% |
| 82 |
\let\pgfoo@methods=\pgfutil@empty% |
| 83 |
#3% |
| 84 |
% inherit |
| 85 |
\def\pgfoo@temp@baseclasses{#1}% |
| 86 |
\pgfoo@inherit@methods |
| 87 |
\pgfoo@inherit@attributes |
| 88 |
% Always present (and never inherited) methods: |
| 89 |
\expandafter\let\csname pgfooY\[email protected] handle\endcsname\pgfoo@obj% |
| 90 |
\expandafter\let\csname pgfooY\[email protected] id\endcsname\pgfoo@id% |
| 91 |
% for compatibility with previous versions of pgfoo, define method |
| 92 |
% with the same name as the class as a synonym for init |
| 93 |
\expandafter\let\expandafter\pgfoo@init\csname pgfooY\[email protected]\endcsname |
| 94 |
\expandafter\let\csname pgfooY\pgfoo@classname.\pgfoo@classname\endcsname\pgfoo@init |
| 95 |
% Cleanup |
| 96 |
\let\method=\pgfoo@origmethod% |
| 97 |
\let\attribute=\pgfoo@origattribute% |
| 98 |
}% |
| 99 |
|
| 100 |
\def\pgfoo@emptyinit(){}% |
| 101 |
\def\pgfoo@escapeif#1#2\fi{\fi#1}% a little helper |
| 102 |
|
| 103 |
|
| 104 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 |
% Compute MRO using C3 algorithm |
| 106 |
% The sequences are enclosed in brackets; elements of sequences and |
| 107 |
% the big list are terminated by a comma and a dot, respectively. (Why |
| 108 |
% comma *and* a dot: TeX's parset---brackets don't form groups! Soooo, |
| 109 |
% why not braces? Because I don't know how do delete excess "{},"s...) |
| 110 |
|
| 111 |
% #1 = current class, #2 = list of base classes |
| 112 |
\def\pgfoo@ciii#1#2{% |
| 113 |
% \pgfoo@seq will hold a list of sequences. |
| 114 |
% (i) the first sequence has just one element: the current class |
| 115 |
\def\pgfoo@seq{[#1,].}% |
| 116 |
% (ii) every subclass has a sequence (in the order as they were |
| 117 |
% given): the class' sequence is its mro. |
| 118 |
\pgfutil@for\pgfoo@baseclass:={#2}\do{% |
| 119 |
\edef\pgfoo@baseclass@mro{\csname pgfooY\pgfoo@baseclass @pgfoo@mro\endcsname}% |
| 120 |
\expandafter\ifx\expandafter\relax\pgfoo@baseclass@mro\relax\else |
| 121 |
\edef\pgfoo@seq{\pgfoo@seq[\pgfoo@baseclass@mro,].}% |
| 122 |
\fi |
| 123 |
}% |
| 124 |
% The final sequence is a sequence of base classes, in the order as |
| 125 |
% they were given. |
| 126 |
\ifx\relax#2\relax\else |
| 127 |
\edef\pgfoo@seq{\pgfoo@seq[#2,].}% |
| 128 |
\fi |
| 129 |
\edef\pgfoo@temp{()(\pgfoo@seq)}% |
| 130 |
\expandafter\pgfoo@ciii@merge\pgfoo@temp |
| 131 |
}% |
| 132 |
|
| 133 |
% #1 = MRO so far |
| 134 |
% #2 = remaining sequences |
| 135 |
% no empty sequences [] should arrive here: they should be removed |
| 136 |
% before calling this macro |
| 137 |
\def\pgfoo@ciii@merge(#1)(#2){% |
| 138 |
\ifx\relax#2\relax |
| 139 |
\pgfoo@ciii@done(#1)% |
| 140 |
\else\pgfoo@escapeif{% split #2 |
| 141 |
\pgfoo@ciii@merge@A(#1)(#2)% |
| 142 |
}\fi% |
| 143 |
}% |
| 144 |
% a splitter: to be only called from \pgfoo@ciii@merge |
| 145 |
\def\pgfoo@ciii@merge@A(#1)(#2.#3){% |
| 146 |
\ifx\relax#3\relax % if #3 is empty, we can take the shortcut |
| 147 |
\pgfoo@ciii@mergeA@done(#1)(#2)% |
| 148 |
\else\pgfoo@escapeif{% |
| 149 |
\pgfoo@ciii@merge@checkHead(#1)()(#2)()(#3)% |
| 150 |
}\fi |
| 151 |
}% |
| 152 |
|
| 153 |
\def\pgfoo@ciii@mergeA@done(#1)([#2]){% |
| 154 |
\pgfoo@ciii@done(#1#2)% |
| 155 |
}% |
| 156 |
|
| 157 |
% #1 = MRO so far |
| 158 |
% #2 = sequences with bad heads (dot after each sequence) |
| 159 |
% [#3,#4] = the sequence whose head we're testing right |
| 160 |
% now---obviously, it should not be empty! (#3 has no comma, |
| 161 |
% #4 has a comma after each class name) |
| 162 |
% #5 = #3 does not occur in the tail of these sequences (dot after |
| 163 |
% each sequence) |
| 164 |
% #6 = the remaining sequences (dot after each sequence) |
| 165 |
\def\pgfoo@ciii@merge@checkHead(#1)(#2)([#3,#4])(#5)(#6){% |
| 166 |
\pgfutil@in@{[#3,}{#2}% |
| 167 |
\ifpgfutil@in@\pgfoo@escapeif{% #3 is already blacklisted |
| 168 |
\pgfoo@ciii@clean\pgfoo@ciii@merge@checkHead@P{(#1)(#2[#3,#4].)(#5#6)}% |
| 169 |
}\else\pgfoo@escapeif{% |
| 170 |
\ifx\relax#6\relax\pgfoo@escapeif{% no more sequences to test the head against! |
| 171 |
% so, #3 is a good head, and we can recurse |
| 172 |
\pgfoo@ciii@goodHeadFound(#1)(#3)(#2[#4].#5)% |
| 173 |
}\else\pgfoo@escapeif{% split #6 and get to work |
| 174 |
\pgfoo@ciii@merge@checkHead@A(#1)(#2)([#3,#4])(#5)(#6)% |
| 175 |
}\fi |
| 176 |
}\fi |
| 177 |
}% |
| 178 |
% splitter: to be only called from \pgfoo@ciii@merge@checkHead |
| 179 |
% we will check if #3 occurs in the tail of #6 |
| 180 |
\def\pgfoo@ciii@merge@checkHead@A(#1)(#2)([#3,#4])(#5)(#6.#7){% |
| 181 |
\pgfoo@ciii@merge@checkHead@B(#1)(#2)([#3,#4])(#5)(#6)(#7)% |
| 182 |
}% |
| 183 |
% splitter: to be only called from \pgfoo@ciii@merge@checkHead@A |
| 184 |
% &worker: we will check if #3 occurs in #7. If it does, #3 is a bad head. |
| 185 |
\def\pgfoo@ciii@merge@checkHead@B(#1)(#2)([#3,#4])(#5)([#6,#7])(#8){% |
| 186 |
% \def\pgfoo@ciii@tempA{#3}\def\pgfoo@ciii@tempB{#5}% |
| 187 |
% \ifx\pgfoo@ciii@tempA\pgfoo@ciii@tempB |
| 188 |
% \pgferror{An attempt to derive from the same class twice}% |
| 189 |
% \else\pgfoo@escapeif{% |
| 190 |
\pgfutil@in@{,#3,}{,#7}% |
| 191 |
\ifpgfutil@in@\pgfoo@escapeif{% bad head! get next head and restart |
| 192 |
% but: need to clean up first! |
| 193 |
\pgfoo@ciii@clean\pgfoo@ciii@merge@checkHead@P{(#1)(#2[#3,#4].)(#5[#6,#7].#8)}% |
| 194 |
}\else\pgfoo@escapeif{% so far, so good |
| 195 |
\ifx\relax#8\relax\pgfoo@escapeif{% if #8 is empty, we won: |
| 196 |
% #3 is a good head! we will move it to MRO |
| 197 |
% but clean-up first! |
| 198 |
\pgfoo@ciii@goodHeadFound(#1)(#3)(#2[#4].#5[#6,#7].)% |
| 199 |
}\else\pgfoo@escapeif{% proceed to the first seq in #8 |
| 200 |
\pgfoo@ciii@clean\pgfoo@ciii@merge@checkHead{(#1)(#2)([#3,#4])(#5[#6,#7].)(#8)}% |
| 201 |
}\fi |
| 202 |
}\fi |
| 203 |
% }\fi |
| 204 |
}% |
| 205 |
|
| 206 |
% #1 = MRO |
| 207 |
% #2 = good head |
| 208 |
% #3 = the remaining sequences (in need of removing the good head from |
| 209 |
% them, and then also some general cleanup) |
| 210 |
\def\pgfoo@ciii@goodHeadFound(#1)(#2)(#3){% |
| 211 |
\pgfutil@in@{,#2,}{#3}% |
| 212 |
\ifpgfutil@in@\pgfoo@escapeif{% |
| 213 |
\def\pgfoo@ciii@removeGoodHead(##1)(##2)(##3,#2,##4){% |
| 214 |
\pgfoo@ciii@goodHeadFound(##1)(##2)(##3,##4)}% |
| 215 |
\pgfoo@ciii@removeGoodHead(#1)(#2)(#3)% |
| 216 |
}\else\pgfoo@escapeif{% |
| 217 |
\pgfutil@in@{[#2,}{#3}% |
| 218 |
\ifpgfutil@in@\pgfoo@escapeif{% |
| 219 |
\def\pgfoo@ciii@removeGoodHead(##1)(##2)(##3[#2,##4){% |
| 220 |
\pgfoo@ciii@goodHeadFound(##1)(##2)(##3[##4)}% |
| 221 |
\pgfoo@ciii@removeGoodHead(#1)(#2)(#3)% |
| 222 |
}\else\pgfoo@escapeif{% |
| 223 |
\pgfoo@ciii@clean\pgfoo@ciii@merge{(#1#2,)(#3)}% |
| 224 |
}\fi |
| 225 |
}\fi |
| 226 |
}% |
| 227 |
|
| 228 |
% mediates between the bad head exit of \pgfoo@ciii@merge@checkHead@B |
| 229 |
% and \pgfoo@ciii@merge@checkHead; we need the mediator because #3 |
| 230 |
% below might have been affected by cleaning. |
| 231 |
\def\pgfoo@ciii@merge@checkHead@P(#1)(#2)(#3){% |
| 232 |
\if\relax#3\relax % no more good head candidates! |
| 233 |
\pgferror{Bad MRO: There is no good (monotonic etc.) |
| 234 |
Method Resolution Order for your class hierarchy}% |
| 235 |
\else\pgfoo@escapeif{% |
| 236 |
\pgfoo@ciii@merge@checkHead@Q(#1)(#2)(#3)% |
| 237 |
}\fi |
| 238 |
}% |
| 239 |
% splitter for \pgfoo@ciii@merge@checkHead@P |
| 240 |
% #3 = the sequence containing the newest good head candidate |
| 241 |
\def\pgfoo@ciii@merge@checkHead@Q(#1)(#2)(#3.#4){% |
| 242 |
\pgfoo@ciii@merge@checkHead(#1)(#2)(#3)()(#4)% |
| 243 |
}% |
| 244 |
|
| 245 |
% cyclically removes all occurrences of "[]," and "[]." (i.e., empty |
| 246 |
% sequences) in #2. When done, calls #1 with whatever remains. |
| 247 |
\def\pgfoo@ciii@clean#1#2{% clean commas |
| 248 |
\pgfutil@in@{[].}{#2}% clean dots |
| 249 |
\ifpgfutil@in@\pgfoo@escapeif{% |
| 250 |
\pgfoo@ciii@clean@dot#1#2\pgfoo@ciii@clean@END |
| 251 |
}\else\pgfoo@escapeif{% |
| 252 |
#1#2% |
| 253 |
}\fi |
| 254 |
}% |
| 255 |
\def\pgfoo@ciii@clean@dot#1#2[].#3\pgfoo@ciii@clean@END{% |
| 256 |
\pgfoo@ciii@clean#1{#2#3}% |
| 257 |
}% |
| 258 |
|
| 259 |
\def\pgfoo@ciii@done(#1,){% |
| 260 |
\def\pgfoo@mro{#1}% |
| 261 |
}% |
| 262 |
|
| 263 |
% end of C3 |
| 264 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 265 |
|
| 266 |
|
| 267 |
% Declare a method |
| 268 |
% |
| 269 |
% Description: |
| 270 |
% |
| 271 |
% Defines a method. To use/invoke this method for an object \object, |
| 272 |
% you write \object.methodname(parameters). This will cause the method |
| 273 |
% body to be invoked with the argument "(parameters)". |
| 274 |
% |
| 275 |
% To define the method \method should be directly followed by the |
| 276 |
% method name and, then, by (, followed by a parameter pattern, |
| 277 |
% followed by ), followed by the message body. Spaces are allowed only |
| 278 |
% after "\method" and after the closing ). |
| 279 |
% |
| 280 |
% Example: |
| 281 |
% |
| 282 |
% \pgfooclass{MyPlot}{ |
| 283 |
% |
| 284 |
% \attribute x=0; |
| 285 |
% \attribute y=0; |
| 286 |
% |
| 287 |
% \method MyPlot() { |
| 288 |
% } |
| 289 |
% |
| 290 |
% \method getX(#1) { |
| 291 |
% \pgfooget{x}{#1} |
| 292 |
% } |
| 293 |
% |
| 294 |
% \method setPoint(#1,#2) { |
| 295 |
% \pgfooset{x}{#1} |
| 296 |
% \pgfooset{y}{#2} |
| 297 |
% } |
| 298 |
% } |
| 299 |
|
| 300 |
% define the method macro and append the method to the method collection |
| 301 |
\long\def\pgfoo@declaremethod#1(#2)#3{% |
| 302 |
\def\pgfoo@method{#1}% |
| 303 |
\ifx\pgfoo@classname\pgfoo@method |
| 304 |
\def\pgfoo@method{init}% |
| 305 |
\fi |
| 306 |
\expandafter\long\expandafter\def\csname pgfooY\pgfoo@classname.\pgfoo@method\endcsname(#2){#3}% |
| 307 |
\edef\pgfoo@methods{\pgfoo@methods,\pgfoo@method}% |
| 308 |
}% |
| 309 |
|
| 310 |
\def\pgfoo@inherit@methods{% |
| 311 |
% for non-derived classes: |
| 312 |
\expandafter\ifx\expandafter\relax\pgfoo@temp@baseclasses\relax |
| 313 |
% if init is not defined, define an empty one |
| 314 |
\expandafter\ifx\csname pgfooY\[email protected]\endcsname\relax |
| 315 |
\expandafter\let\csname pgfooY\[email protected]\endcsname\pgfoo@emptyinit% |
| 316 |
\edef\pgfoo@methods{,init\pgfoo@methods}% |
| 317 |
\fi |
| 318 |
\fi |
| 319 |
\if\relax\pgfoo@methods\relax\else |
| 320 |
% gobble the initial comma |
| 321 |
\edef\pgfoo@methods{\expandafter\pgfutil@gobble\pgfoo@methods}% |
| 322 |
\fi |
| 323 |
% remember the list of collected methods |
| 324 |
\expandafter\let\csname pgfooY\pgfoo@classname @pgfoo@methods\endcsname\pgfoo@methods |
| 325 |
% get MRO |
| 326 |
\expandafter\let\expandafter\pgfoo@mro\csname pgfooY\pgfoo@classname @pgfoo@mro\endcsname |
| 327 |
% loop throough base classes in MRO |
| 328 |
\pgfutil@for\pgfoo@baseclass:=\pgfoo@mro\do{% |
| 329 |
\ifx\pgfoo@baseclass\pgfoo@classname % skip self |
| 330 |
\else\pgfoo@escapeif{% |
| 331 |
% get methods defined in this base class |
| 332 |
\expandafter\let\expandafter\pgfoo@methods\csname pgfooY\pgfoo@baseclass @pgfoo@methods\endcsname |
| 333 |
\pgfutil@for\pgfoo@method:=\pgfoo@methods\do{% for each method |
| 334 |
% check if it is already defined in our class |
| 335 |
\expandafter\ifx\csname pgfooY\pgfoo@classname.\pgfoo@method\endcsname\relax |
| 336 |
% if not, link the method name from our class to base class |
| 337 |
\edef\pgfoo@temp{\noexpand\let |
| 338 |
\expandafter\noexpand\csname pgfooY\pgfoo@classname.\pgfoo@method\endcsname |
| 339 |
\expandafter\noexpand\csname pgfooY\pgfoo@baseclass.\pgfoo@method\endcsname}% |
| 340 |
\pgfoo@temp |
| 341 |
\fi |
| 342 |
}% |
| 343 |
}\fi |
| 344 |
}% |
| 345 |
}% |
| 346 |
|
| 347 |
\def\pgfoosuper(#1,#2).#3({% |
| 348 |
#2.get id(\pgfoo@temp@id)% |
| 349 |
\edef\pgfoo@classname{\csname pgfooX\pgfoo@temp@id @class\endcsname}% |
| 350 |
\def\pgfoo@super@A##1,#1,##2.##3({% |
| 351 |
\pgfoo@super@B##2.##3(% |
| 352 |
}% |
| 353 |
\expandafter\let\expandafter\pgfoo@mro\csname pgfooY\pgfoo@classname @pgfoo@mro\endcsname |
| 354 |
\expandafter\pgfoo@super@A\expandafter,\pgfoo@mro,.#3(% |
| 355 |
}% |
| 356 |
\def\pgfoo@super@B#1,#2.#3({% |
| 357 |
\expandafter\ifx\csname pgfooY#1.#3\endcsname\relax |
| 358 |
\pgfoo@escapeif{\pgfoo@super@B#2.#3(}% |
| 359 |
\else |
| 360 |
\pgfoo@escapeif{\expandafter\pgfoo@caller\pgfoo@temp@id.#1.#3(}% |
| 361 |
\fi |
| 362 |
}% |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
% Declare an attribute |
| 367 |
% |
| 368 |
% #1 = attribute name |
| 369 |
% #2 = optional default value |
| 370 |
% |
| 371 |
% Description: |
| 372 |
% |
| 373 |
% Declares the attribute #1 for the current class. If the attribute |
| 374 |
% name is followed by =, the text following the equal sign is the |
| 375 |
% default value. |
| 376 |
|
| 377 |
\def\pgfoo@declareattribute#1;{% |
| 378 |
\pgfutil@in@{ =}{#1}% |
| 379 |
\ifpgfutil@in@% |
| 380 |
\pgfoo@declareunpackspace#1;% |
| 381 |
\else% |
| 382 |
\pgfutil@in@={#1}% |
| 383 |
\ifpgfutil@in@% |
| 384 |
\pgfoo@declareunpack#1;% |
| 385 |
\else% |
| 386 |
\pgfoo@declareattribute@\let{#1}\pgfutil@empty |
| 387 |
\fi% |
| 388 |
\fi% |
| 389 |
}% |
| 390 |
\def\pgfoo@declareunpack#1=#2;{\pgfoo@declareattribute@\def{#1}{{#2}}}% |
| 391 |
\def\pgfoo@declareunpackspace#1 =#2;{\pgfoo@declareattribute@\def{#1}{{#2}}}% |
| 392 |
|
| 393 |
% put the initial value in the class's namespace |
| 394 |
% put the attr name in a list |
| 395 |
\def\pgfoo@declareattribute@#1#2#3{% |
| 396 |
\expandafter#1\csname pgfooY\pgfoo@classname @#2\endcsname#3% |
| 397 |
\expandafter\def\expandafter\pgfoo@attributes\expandafter{\pgfoo@attributes,#2}% |
| 398 |
}% |
| 399 |
|
| 400 |
% for each base class: |
| 401 |
% for each attribute declared in that class: |
| 402 |
% add the attr on a list if it's not there yet |
| 403 |
% convert the list of (attr,class which it comes from) into an |
| 404 |
% efficient sequence of triples \op{attrname}\pointer-to-initial-value |
| 405 |
% (\op is then set differently in init and garbage collection) |
| 406 |
\def\pgfoo@inherit@attributes{% |
| 407 |
% store the list of attributes declared in this class for use in |
| 408 |
% derived classes |
| 409 |
\if\relax\pgfoo@attributes\relax\else |
| 410 |
\edef\pgfoo@attributes{\expandafter\pgfutil@gobble\pgfoo@attributes}% gobble comma |
| 411 |
\fi |
| 412 |
\expandafter\let\csname pgfooY\pgfoo@classname.@pgfoo@attributes\endcsname\pgfoo@attributes% |
| 413 |
% get MRO |
| 414 |
\expandafter\let\expandafter\pgfoo@mro\csname pgfooY\pgfoo@classname @pgfoo@mro\endcsname |
| 415 |
\def\pgfoo@allattributes{}% here we will store the attrs declared in base classes |
| 416 |
\pgfutil@for\pgfoo@baseclass:=\pgfoo@mro\do{% for each base class in MRO order |
| 417 |
% the attributes declared in this base class |
| 418 |
\expandafter\let\expandafter\pgfoo@attributes\csname pgfooY\pgfoo@baseclass.@pgfoo@attributes\endcsname |
| 419 |
\pgfutil@for\pgfoo@attribute:=\pgfoo@attributes\do{% for each attribute |
| 420 |
% check if we have already found it in some previous base class |
| 421 |
\edef\pgfoo@temp{{,\pgfoo@attribute=}{\pgfoo@allattributes,}}% |
| 422 |
\expandafter\pgfutil@in@\pgfoo@temp |
| 423 |
\ifpgfutil@in@\else % don't overwrite |
| 424 |
% append to the list |
| 425 |
\edef\pgfoo@allattributes{\pgfoo@allattributes,\pgfoo@attribute=\pgfoo@baseclass}% |
| 426 |
\fi |
| 427 |
}% |
| 428 |
}% |
| 429 |
\if\relax\pgfoo@allattributes\relax\else |
| 430 |
\edef\pgfoo@allattributes{\expandafter\pgfutil@gobble\pgfoo@allattributes}% gobble comma |
| 431 |
\fi |
| 432 |
% this macro will hold a list of attributes from all base classes |
| 433 |
% we run this macro at init and gc |
| 434 |
\def\pgfoo@process@attributes{}% |
| 435 |
\pgfutil@for\pgfoo@attributeclass:=\pgfoo@allattributes\do{% for each (attribute, class) pair |
| 436 |
% append to the attribute processor |
| 437 |
\expandafter\pgfoo@inherit@attributes@appendattribute\pgfoo@attributeclass.% |
| 438 |
}% |
| 439 |
\expandafter\let\csname pgfooY\pgfoo@classname .@pgfoo@process@attributes\endcsname\pgfoo@process@attributes |
| 440 |
}% |
| 441 |
|
| 442 |
|
| 443 |
% #1=attr, #2=class which the attr comes from |
| 444 |
\def\pgfoo@inherit@attributes@appendattribute#1=#2.{% |
| 445 |
\def\pgfoo@tempA{\pgfoo@attribute@op{#1}}% |
| 446 |
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfoo@tempB\expandafter\expandafter\expandafter{\expandafter\pgfoo@tempA\csname pgfooY#2@#1\endcsname}% |
| 447 |
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfoo@process@attributes\expandafter\expandafter\expandafter{\expandafter\pgfoo@process@attributes\pgfoo@tempB}% |
| 448 |
% above is just this: |
| 449 |
% \def\pgfoo@process@attributes{\pgfoo@process@attributes\pgfoo@attribute@op{#1}% |
| 450 |
% \csname pgfooY#2@#1\endcsname} |
| 451 |
% with \pgfoo@process@attributes and \csname expanded exactly once: |
| 452 |
}% |
| 453 |
|
| 454 |
% \pgfoo@attribute@op for garbage collection |
| 455 |
\def\pgfoo@gc@attribute#1#2{\pgfoolet{#1}\relax}% |
| 456 |
|
| 457 |
|
| 458 |
\newcount\pgfoo@objectcount |
| 459 |
\newcount\pgfoothis@count |
| 460 |
|
| 461 |
% The attribute value method |
| 462 |
% |
| 463 |
% #1 = attr |
| 464 |
% |
| 465 |
% This method inserts the current value of the given attribute for the |
| 466 |
% current object. |
| 467 |
|
| 468 |
\def\pgfoovalueof#1{% |
| 469 |
\csname pgfooX\the\pgfoothis@count @#1\endcsname% |
| 470 |
}% |
| 471 |
|
| 472 |
|
| 473 |
% The attribute get method |
| 474 |
% |
| 475 |
% #1 = attr |
| 476 |
% #2 = macro |
| 477 |
% |
| 478 |
% This method makes the macro equal to the current value of the |
| 479 |
% attribute for the current object. |
| 480 |
|
| 481 |
\def\pgfooget#1#2{% |
| 482 |
\expandafter\let\expandafter#2\csname pgfooX\the\pgfoothis@count @#1\endcsname% |
| 483 |
}% |
| 484 |
|
| 485 |
|
| 486 |
% The attribute set method |
| 487 |
% |
| 488 |
% #1 = attr |
| 489 |
% #2 = value |
| 490 |
% |
| 491 |
% This method sets the given attribute for the current object to the |
| 492 |
% given value. |
| 493 |
|
| 494 |
\long\def\pgfooset#1#2{% |
| 495 |
\expandafter\gdef\csname pgfooX\the\pgfoothis@count @#1\endcsname{#2}% |
| 496 |
}% |
| 497 |
|
| 498 |
|
| 499 |
% The attribute set method (expanded version) |
| 500 |
% |
| 501 |
% #1 = attr |
| 502 |
% #2 = value |
| 503 |
% |
| 504 |
% This method sets the given attribute for the current object to the |
| 505 |
% expanded version of the given value. |
| 506 |
|
| 507 |
\long\def\pgfooeset#1#2{% |
| 508 |
\expandafter\xdef\csname pgfooX\the\pgfoothis@count @#1\endcsname{#2}% |
| 509 |
}% |
| 510 |
|
| 511 |
|
| 512 |
% The attribute let method |
| 513 |
% |
| 514 |
% #1 = attr |
| 515 |
% #2 = value |
| 516 |
% |
| 517 |
% This method sets the given attribute for the current object to the |
| 518 |
% given value using \let. |
| 519 |
|
| 520 |
\def\pgfoolet#1#2{% |
| 521 |
\expandafter\global\expandafter\let\csname pgfooX\the\pgfoothis@count @#1\endcsname#2% |
| 522 |
}% |
| 523 |
|
| 524 |
|
| 525 |
% Add something to an attribute at the end |
| 526 |
% |
| 527 |
% #1 = attr |
| 528 |
% #2 = code |
| 529 |
% |
| 530 |
% This method adds the give code to the attr at the end. |
| 531 |
|
| 532 |
\def\pgfooappend#1#2{% |
| 533 |
\expandafter\expandafter\expandafter\def% |
| 534 |
\expandafter\expandafter\expandafter\pgf@oo@temp\expandafter\expandafter\expandafter{\csname pgfooX\the\pgfoothis@count @#1\endcsname#2}% |
| 535 |
\expandafter\global\expandafter\let\csname pgfooX\the\pgfoothis@count @#1\endcsname\pgf@oo@temp% |
| 536 |
}% |
| 537 |
|
| 538 |
|
| 539 |
% Add something to an attribute at the beginning |
| 540 |
% |
| 541 |
% #1 = attr |
| 542 |
% #2 = code |
| 543 |
% |
| 544 |
% This method adds the give code to the attr at the beginning. |
| 545 |
|
| 546 |
\def\pgfooprefix#1#2{% |
| 547 |
\pgfooget{#1}\pgf@oo@temp% |
| 548 |
\def\pgf@oo@@temp{#2}% |
| 549 |
\expandafter\expandafter\expandafter\def% |
| 550 |
\expandafter\expandafter\expandafter\pgf@oo@temp% |
| 551 |
\expandafter\expandafter\expandafter{\expandafter\pgf@oo@@temp\pgf@oo@temp}% |
| 552 |
\pgfoolet{#1}\pgf@oo@temp% |
| 553 |
}% |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
% Help macro for scanning arguments |
| 558 |
\newtoks\pgfoo@toks |
| 559 |
|
| 560 |
\def\pgfoo@collect@args{% |
| 561 |
\pgfutil@ifnextchar\bgroup\pgfoo@collect@args@group\pgfoo@collect@args@nogroup% |
| 562 |
}% |
| 563 |
\def\pgfoo@collect@args@nogroup#1){% |
| 564 |
\pgfoo@toks{#1}% |
| 565 |
\pgfoo@continue% |
| 566 |
}% |
| 567 |
\def\pgfoo@collect@args@group#1{% |
| 568 |
\pgfutil@ifnextchar){\pgfoo@toks{{#1}}\expandafter\pgfoo@continue\pgfutil@gobble}{\pgfoo@collect@args@nogroup{#1}}% |
| 569 |
}% |
| 570 |
|
| 571 |
|
| 572 |
% Instantiate an object |
| 573 |
% |
| 574 |
% Possible syntax: |
| 575 |
% |
| 576 |
% 1) \pgfoonew new <class name>(<constructor parameters) |
| 577 |
% 2) \pgfoonew \<objectname>=new <class name>(<constructor parameters) |
| 578 |
% 3) \pgfoonew{attribute}=new <class name>(<constructor parameters) |
| 579 |
% |
| 580 |
% Description: |
| 581 |
% |
| 582 |
% Creates an object. After the object has been created, the method |
| 583 |
% called <class name> (the constructor) is invoked. If the |
| 584 |
% \<objectname>= part is present, the macro is assigned to the newly |
| 585 |
% created object. |
| 586 |
|
| 587 |
\def\pgfoonew{% |
| 588 |
\pgfutil@ifnextchar n{% |
| 589 |
\pgfoo@new\pgfoo@dummy=% |
| 590 |
}{% |
| 591 |
\pgfutil@ifnextchar\bgroup{% |
| 592 |
\pgfoo@new@attribute% |
| 593 |
}{% |
| 594 |
\pgfoo@new% |
| 595 |
}% |
| 596 |
}% |
| 597 |
}% |
| 598 |
\def\pgfoo@new#1={% |
| 599 |
\def\pgfutil@reserved@a{#1}% |
| 600 |
\let\pgfutil@next\pgfoo@@new |
| 601 |
\futurelet\pgfutil@let@token\pgfutil@ignorespaces}% |
| 602 |
\def\pgfoo@@new new #1({% |
| 603 |
\expandafter\ifx\csname pgfooY#1.get id\endcsname\relax% |
| 604 |
\pgferror{Unknown class '#1'}% |
| 605 |
\else% |
| 606 |
\expandafter\pgfoo@new@create\pgfutil@reserved@a{#1}% |
| 607 |
{% |
| 608 |
\pgfoothis@count\pgfoo@objectcount% |
| 609 |
\let\pgfoo@attribute@op\pgfoolet |
| 610 |
\csname pgfooY#1.@pgfoo@process@attributes\endcsname% |
| 611 |
}% |
| 612 |
\let\pgfoo@continue=\pgf@oo@new@cont% |
| 613 |
\expandafter\pgfoo@collect@args% |
| 614 |
\fi% |
| 615 |
}% |
| 616 |
|
| 617 |
\def\pgf@oo@new@cont{% |
| 618 |
\expandafter\pgfoolastobj\expandafter.\expandafter i\expandafter n\expandafter i\expandafter t\expandafter(\the\pgfoo@toks)% |
| 619 |
\aftergroup\pgfoogc% cleanup after group |
| 620 |
}% |
| 621 |
|
| 622 |
\def\pgfoo@new@attribute#1={% |
| 623 |
\def\pgfutil@reserved@a{#1}% |
| 624 |
\let\pgfutil@next\pgfoo@@new@attribute |
| 625 |
\futurelet\pgfutil@let@token\pgfutil@ignorespaces}% |
| 626 |
\def\pgfoo@@new@attribute new #1({% |
| 627 |
\expandafter\ifx\csname pgfooY#1.get id\endcsname\relax% |
| 628 |
\pgferror{Unknown class '#1'}% |
| 629 |
\else% |
| 630 |
\pgfoo@new@create\pgfoo@temp{#1}% |
| 631 |
{% |
| 632 |
\pgfoothis@count\pgfoo@objectcount% |
| 633 |
\let\pgfoo@attribute@op\pgfoolet% |
| 634 |
\csname pgfooY#1.@pgfoo@process@attributes\endcsname% |
| 635 |
}% |
| 636 |
\expandafter\pgfoolet\expandafter{\pgfutil@reserved@a}\pgfoo@temp% |
| 637 |
\let\pgfoo@continue=\pgf@oo@new@cont% |
| 638 |
\expandafter\pgfoo@collect@args% |
| 639 |
\fi% |
| 640 |
}% |
| 641 |
|
| 642 |
\def\pgfoo@new@create#1#2{% |
| 643 |
\advance\pgfoo@objectcount by 1\relax% |
| 644 |
\edef\pgfoolastobj{\noexpand\pgfoo@caller{\the\pgfoo@objectcount}}% |
| 645 |
\expandafter\gdef\csname pgfooX\the\pgfoo@objectcount @class\endcsname{#2}% |
| 646 |
\let#1\pgfoolastobj% |
| 647 |
}% |
| 648 |
|
| 649 |
\def\pgfoo@caller#1.#2({% |
| 650 |
\expandafter\let\expandafter\pgfoo@caller@temp\csname pgfooY\csname pgfooX#1@class\endcsname.#2\endcsname% |
| 651 |
\ifx\pgfoo@caller@temp\relax% |
| 652 |
% assume that #2 is of form "superclass.method" |
| 653 |
\expandafter\let\expandafter\pgfoo@caller@temp\csname pgfooY#2\endcsname% |
| 654 |
\ifx\pgfoo@caller@temp\relax% |
| 655 |
\pgferror{Object #1 has no method '#2'}% |
| 656 |
\fi% |
| 657 |
\fi% |
| 658 |
\def\pgf@marshal{% |
| 659 |
\pgfoothis@count=#1\relax% |
| 660 |
}% |
| 661 |
\let\pgfoo@continue\pgfoo@caller@cont% |
| 662 |
\pgfoo@collect@args% |
| 663 |
}% |
| 664 |
\def\pgfoo@caller@cont{% |
| 665 |
\edef\pgf@marshal{% |
| 666 |
\pgf@marshal% |
| 667 |
\noexpand\pgfoo@caller@temp(\the\pgfoo@toks)% |
| 668 |
}% |
| 669 |
\expandafter\pgf@marshal\expandafter\pgfoothis@count\the\pgfoothis@count\relax% |
| 670 |
}% |
| 671 |
\let\pgfoo@orig@caller=\pgfoo@caller |
| 672 |
|
| 673 |
% The special "this" object |
| 674 |
|
| 675 |
\def\pgfoothis.#1({% |
| 676 |
\expandafter\let\expandafter\pgfoo@caller@temp\csname pgfooY\csname pgfooX\the\pgfoothis@count @class\endcsname.#1\endcsname% |
| 677 |
\ifx\pgfoo@caller@temp\relax% |
| 678 |
% assume that #1 is of form "superclass.method" |
| 679 |
\expandafter\let\expandafter\pgfoo@caller@temp\csname pgfooY#1\endcsname% |
| 680 |
\fi% |
| 681 |
\let\pgfoo@continue\pgfoothis@cont% |
| 682 |
\pgfoo@collect@args% |
| 683 |
}% |
| 684 |
\def\pgfoothis@cont{% |
| 685 |
\expandafter\pgfoo@caller@temp\expandafter(\the\pgfoo@toks)% |
| 686 |
}% |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
% Get the object id |
| 691 |
% |
| 692 |
% #1 = macro to store the id |
| 693 |
% |
| 694 |
% Description: |
| 695 |
% |
| 696 |
% This special method allows you to access the object id. You can then |
| 697 |
% use \pgfoocall to call a method using this id. This is mainly useful |
| 698 |
% when you wish to store the id for a longer time. |
| 699 |
|
| 700 |
\def\pgfoo@id(#1){% |
| 701 |
\edef#1{\the\pgfoothis@count}% |
| 702 |
}% |
| 703 |
|
| 704 |
% Yields the object with the given id |
| 705 |
% |
| 706 |
% #1 = id |
| 707 |
% |
| 708 |
% Description: |
| 709 |
% |
| 710 |
% Given an object id, \pgfooobj{<id>} will yield the object having |
| 711 |
% this id. |
| 712 |
|
| 713 |
\def\pgfooobj#1{% |
| 714 |
\pgfoo@caller{#1}% |
| 715 |
}% |
| 716 |
|
| 717 |
|
| 718 |
% Get the object |
| 719 |
% |
| 720 |
% #1 = macro to store the object |
| 721 |
% |
| 722 |
% Description: |
| 723 |
% |
| 724 |
% This special method allows you to get a new handle to a given |
| 725 |
% object. If \obj is an object, you could normally just say |
| 726 |
% \let#1=\obj. However, if \obj happens to be \pgfoothis, then you may |
| 727 |
% wish to get a handle to the object itself, not to the special macro |
| 728 |
% \pgfoothis. In this case you can say \obj.get handle(#1). |
| 729 |
|
| 730 |
\def\pgfoo@obj(#1){% |
| 731 |
\edef#1{\noexpand\pgfoo@caller{\the\pgfoothis@count}}% |
| 732 |
}% |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
% The garbage collector |
| 737 |
% |
| 738 |
% Description: |
| 739 |
% |
| 740 |
% This method frees space occupied by unused objects. Garbage are |
| 741 |
% objects that have been destroyed because of the end of the scope in |
| 742 |
% which they were created. In this case, however, the memory used by |
| 743 |
% this object is not immediately reused because the attributes of the |
| 744 |
% object are actually stored in global variables. When the garbage |
| 745 |
% collector is called, it will set all these global variables to |
| 746 |
% \relax, thereby ensuring that no memory is needed for them. |
| 747 |
|
| 748 |
\def\pgfoogc{% |
| 749 |
{% |
| 750 |
% We do this in a group... |
| 751 |
\pgfoothis@count\pgfoo@objectcount% this is temporary... |
| 752 |
\let\pgfoo@next=\pgfoo@dogc% |
| 753 |
\pgfoo@next% |
| 754 |
}% |
| 755 |
}% |
| 756 |
\def\pgfoo@dogc{% |
| 757 |
\advance\pgfoothis@count by 1\relax% |
| 758 |
\expandafter\ifx\csname pgfooX\the\pgfoothis@count @class\endcsname\relax% |
| 759 |
\let\pgfoo@next=\relax% |
| 760 |
\else% |
| 761 |
% Cleanup this object: |
| 762 |
% The following is the fast version of \pgfooobj{\the\pgfoo@objectcount}.@pgfoogc: |
| 763 |
\let\pgfoo@attribute@op\pgfoo@gc@attribute |
| 764 |
\csname pgfooY\csname pgfooX\the\pgfoothis@count @class\endcsname.@pgfoo@process@attributes\endcsname% |
| 765 |
\expandafter\global\expandafter\let\csname pgfooX\the\pgfoothis@count @class\endcsname\relax% |
| 766 |
\fi% |
| 767 |
\pgfoo@next% |
| 768 |
}% |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
% |
| 773 |
% |
| 774 |
% Object class |
| 775 |
% defines method "copy" |
| 776 |
% |
| 777 |
% |
| 778 |
|
| 779 |
\pgfooclass{object}{% |
| 780 |
\method copy(#1) {% |
| 781 |
% create a new object |
| 782 |
\edef\pgfoo@temp@classname{\csname pgfooX\the\pgfoothis@count @class\endcsname}% |
| 783 |
\expandafter\pgfoo@new@create\expandafter#1\expandafter{\pgfoo@temp@classname}% |
| 784 |
\def\pgfoo@copy@attributes##1##2{% |
| 785 |
\pgfooget{##1}\pgfoo@attrvalue |
| 786 |
\expandafter\let\csname pgfooX\the\pgfoo@objectcount @##1\endcsname\pgfoo@attrvalue |
| 787 |
}% |
| 788 |
\let\pgfoo@attribute@op\pgfoo@copy@attributes |
| 789 |
\csname pgfooY\pgfoo@temp@classname .@pgfoo@process@attributes\endcsname |
| 790 |
\aftergroup\pgfoogc% cleanup after group |
| 791 |
}% |
| 792 |
}% |
| 793 |
|
| 794 |
|
| 795 |
% |
| 796 |
% |
| 797 |
% Signal class |
| 798 |
% |
| 799 |
% |
| 800 |
|
| 801 |
\pgfooclass{signal} |
| 802 |
{% |
| 803 |
% |
| 804 |
% This class implements signals. |
| 805 |
% |
| 806 |
% After you have created a signal object, you can call |
| 807 |
% connect to connect a slot. Then, whenever the emit method is |
| 808 |
% called, all connected methods get called. |
| 809 |
|
| 810 |
% Attribute |
| 811 |
\attribute emitter;% |
| 812 |
% Collects the objects that should be called. |
| 813 |
|
| 814 |
% Constructor |
| 815 |
\method signal() {}% |
| 816 |
|
| 817 |
% Connect a slot (method) #2 of object #1 |
| 818 |
\method connect(#1,#2) {% |
| 819 |
{% |
| 820 |
#1.get id(\pgf@tempid)% |
| 821 |
% Save in emitter |
| 822 |
\pgfooget{emitter}\pgf@temp% |
| 823 |
\let\pgfoo@signal@call=\relax% avoid expansion |
| 824 |
\edef\pgf@temp{\pgf@temp\pgfoo@signal@call{\pgf@tempid}{#2}}% |
| 825 |
\pgfoolet{emitter}\pgf@temp% |
| 826 |
}% |
| 827 |
}% |
| 828 |
|
| 829 |
\def\pgfoo@signal@call#1#2{% |
| 830 |
\def\pgf@temp{\pgfooobj{#1}.#2}% |
| 831 |
\expandafter\pgf@temp\expandafter(\pgfoo@signal@args)% |
| 832 |
}% |
| 833 |
|
| 834 |
% Emit a signal |
| 835 |
\method emit(#1) {% |
| 836 |
\def\pgfoo@signal@args{#1}% |
| 837 |
\pgfoovalueof{emitter} |
| 838 |
}% |
| 839 |
}% |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
\endinput |