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