25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Method Generators<br />

Class MyApp.MyClass Extends %RegisteredObject<br />

{<br />

Method MyMethod() [ CodeMode = objectgenerator ]<br />

{<br />

Do %code.WriteLine(" Write """ _ %class.Name _ """")<br />

Do %code.WriteLine(" Quit")<br />

Quit $$$OK<br />

}<br />

}<br />

When the class MyApp.MyClass is compiled, it ends up with a MyMethod method with the<br />

following implementation:<br />

Write "MyApp.MyClass"<br />

Quit<br />

Note:<br />

The value of CodeMode in the previous example is “objectgenerator” , since this<br />

method generator uses the preferred, object-based, method generator mechanism.<br />

Prior to version 5 of <strong>Caché</strong>, there was a different preferred mechanism, in which the<br />

value of CodeMode was “generator” . While the older mechanism is preserved for<br />

compatibility, new applications should use “objectgenerator” .<br />

18.2 How Method Generators Work<br />

The operation of a method generator is straightforward. When you compile a class definition,<br />

the Class Compiler does the following:<br />

1. It resolves inheritance for the class (builds a list of all inherited members).<br />

2. It makes a list of all methods specified as method generators (by looking at the CodeMode<br />

keyword of each method).<br />

3. It gathers the code from all method generators, copies it into one or more temporary<br />

routines, and compiles them (this makes it possible to execute the method generator code).<br />

4. It creates a set of transient objects that represent the definition of the class being compiled.<br />

These objects are made available to the method generator code.<br />

5. It executes the code for every method generator.<br />

If present, the compiler will arrange the order in which it invokes the method generators<br />

by looking at the value of the GenerateAfter keyword for each of the methods. This<br />

keyword gives you some control in cases where there may be compiler timing dependencies<br />

among methods.<br />

156 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!