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