Suppose you are interested by one component of the McStas library, but you would like to customize it a little. There are different ways to extend an existing component.
If you only want to add something on top of the component existing behavior, the simplest is to work from the instrument definition TRACE section, using the EXTEND modifier (see section 4.4.2). You do not need to write a new component definition, but only add a piece of code to execute.
There is a heritage mechanism to create children of existing components. These are exact duplicates of the parent component, but one may override/extend original definitions of any section.
The syntax for a full component child is
1DEFINE COMPONENT child_name INHERIT parent_name 2
This single line will copy all parts of the parent into the child, except for the documentation header.
As for normal component definitions, you may add other parameters, DECLARE, TRACE, ... sections. Each of them will replace or extend (be concatenated to, with the INHERIT/EXTEND keywords, see example below) the corresponding parent definition. In practice, you could inherit a component and only rewrite some of it, as in the following example:
1DEFINE COMPONENT child_name INHERIT parent_name 2 3 SETTING PARAMETERS (newpar1, newpar2) 4 INITIALISE 5 INHERIT parent_name 6 EXTEND 7 %{ 8// C code to be concatenated to the parent_name INITIALIZE 9 %} 10 SAVE 11 %{ 12// C code to replace the parent_name SAVE 13 %} 14END 15
where two additional parameters have been defined, and should be handled in the extension of the original INITIALIZE section. Note that each per-section INHERIT parent_name (optionally followed by EXTEND %{...%}) is specified inside that section’s own block, immediately after the section keyword, rather than as a separate top-level COPY clause.
A special care should be taken in mixing bits from different sources, specially concerning variables. This may result in difficulties to compile components.
On the other hand, if you do not want to derive a component as a whole from a parent (i.e. the top-level DEFINE COMPONENT line does not itself use INHERIT), you may still pull in specific sections from any component using the same per-section INHERIT syntax:
1DEFINE COMPONENT name (...) 2SETTING PARAMETERS (...) 3DECLARE 4INHERIT parent1 5INITIALISE 6INHERIT parent2 7EXTEND 8%{ 9// C code to be concatenated to the parent2 INITIALIZE 10%} 11TRACE 12INHERIT parent3 13END 14
This mechanism may lighten the component code, but as above, mixing bits from different sources requires special care regarding variable naming.