doc/named-param.dox
author alpar
Fri, 08 Apr 2005 06:46:12 +0000
changeset 1323 3aaadfb7de3d
parent 955 0a066f80e05f
child 1438 826bdac3525a
permissions -rw-r--r--
The case when the objective function contains a const component is handled
correctly.
alpar@953
     1
/*!
alpar@954
     2
alpar@953
     3
\page named-param Named Parameters
alpar@953
     4
alpar@955
     5
\section named-func-param Named "Function" Parameters
alpar@955
     6
hegyi@1141
     7
C++ makes it possible to use default parameter values when calling a function. In such
hegyi@1141
     8
a case we do not have to give value for parameters, the program will use the default ones.
hegyi@1141
     9
Unfortunately sometimes this is not enough. If we do not want to give values for all the parameters, only
hegyi@1141
    10
for some of them we come across problems, because an arbitrary set of parameters cannot be omitted. On the other hand parameters have a fixed order in the head of the function.
hegyi@1141
    11
C++ can apply the default values only in the back of the order, if we do not give other value for them.
hegyi@1141
    12
So we can not give the function for example the value of the first, and the third parameter, expecting that the program will aplly the default value for the second parameter.
hegyi@1141
    13
However sometimes we would like to use some functinos exactly in this way. With a crafty trick and with some little inconvenience this is possible. We have implemented this little trick as an example below.
hegyi@1141
    14
hegyi@1141
    15
\code
hegyi@1141
    16
class named_fn 
hegyi@1141
    17
{
hegyi@1141
    18
  int _id;
hegyi@1141
    19
  double _val;
hegyi@1141
    20
  int _dim;
hegyi@1141
    21
  
hegyi@1141
    22
  public:
hegyi@1141
    23
  named_fn() : _id(0), _val(1), _dim(2) {}
hegyi@1141
    24
  named_fn& id(int p)     { _id  = p ; return *this; }
hegyi@1141
    25
  named_fn& val(double p) { _val = p ; return *this; }
hegyi@1141
    26
  named_fn& dim(int p)    { _dim = p ; return *this; }
hegyi@1141
    27
hegyi@1141
    28
  run() {
hegyi@1141
    29
    printf("Here is the function itself.");
hegyi@1141
    30
  }
hegyi@1141
    31
};
hegyi@1141
    32
\endcode
hegyi@1141
    33
hegyi@1141
    34
hegyi@1141
    35
The usage is the following.
hegyi@1141
    36
hegyi@1141
    37
We have to define a class, let's call it named_fn.
hegyi@1141
    38
Let us assume that we would like to use a parameter, called X. In the named_fn class we have to define an _X attribute, and an X function. The function expects a parameter with the type of _X, and sets the value of _X. After setting the value the function returns the class itself. The class also have to have a function, called for example run(), we have to implement here the original function itself. The constructor of the class have to give all the attributes like _X the default values of them.
hegyi@1141
    39
hegyi@1141
    40
If we instantiate this class, the default values will be set for the attributes (originally the parameters), initially. If we call the X function, we get a class with the modified parameter value of X. Therefore we can modify any parameter-value, independent from the order. To run the algorithm we have to call the run() function at the end of the row.
hegyi@1141
    41
hegyi@1141
    42
Example: named_fn().id(3).val(2).run();
alpar@955
    43
alpar@955
    44
\section traits-classes Traits Classes
alpar@955
    45
hegyi@1141
    46
The procedure above can also be applied when defining classes. In this case the type of the attributes can be changed.
hegyi@1141
    47
Initially we have to define a class with the default attribute types. This is the se called Traits Class. Later on
hegyi@1141
    48
the types of these attributes can be changed, as described below. In our software \ref DijkstraDefaultTraits is an example of how a traits class look like.
hegyi@1141
    49
alpar@955
    50
\section named-templ-param Named Class Template Parameters
alpar@954
    51
hegyi@1141
    52
If we would like to change the type of an attribute in a class that was instantiated by using a traits class as a template parameter, and the class contains named parameters, we do not have to reinstantiate the class with new traits class. Instead of that adaptor classes can be used like in the following cases.
hegyi@1141
    53
alpar@954
    54
\code
alpar@954
    55
Dijkstra<>::SetPredNodeMap<NullMap<Node,Node> >
alpar@954
    56
\endcode
hegyi@1141
    57
alpar@954
    58
It can also be used in conjunction with other named template
alpar@954
    59
parameters in arbitrary order.
hegyi@1141
    60
alpar@954
    61
\code
alpar@954
    62
Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Edge> >
alpar@954
    63
\endcode
alpar@954
    64
hegyi@1141
    65
The result will be an instantiated Dijkstra class, in which the DistMap and the PredMap is modified.
hegyi@1141
    66
alpar@955
    67
\section named-templ-func-param Named "Function" Template Parameters
alpar@955
    68
hegyi@1141
    69
If the class has so called wizard functions, the new class with the modified tpye of attributes can be returned
hegyi@1141
    70
by the appropriate wizard function. The usage of these wizard functions is the following:
alpar@953
    71
alpar@953
    72
*/