This file is indexed.

/usr/share/doc/libcppnetlib-doc/html/network/_sources/techniques/tag_metafunctions.txt is in libcppnetlib-doc 0.11.0-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
Tag metafunctions
=================

Sometimes you want to vary a function or a type's behavior based on a
static parameter. In the :mod:`cpp-netlib` there are a number of
things you might want to change based on some such parameter -- like
what the underlying string type should be and how large a buffer
should be, among other things. The primary way to define this in a
header-only manner is to use tag-based metafunctions.

The skeleton of the approach is based on a similar technique for
defining type traits. In the :mod:`cpp-netlib` however the type traits
are defined on opaque tag types which serve to associate results to a
family of metafunctions.

Template Specialization
-----------------------

To illustrate this point, let's define a tag ``default_`` which we use
to denote the default implementation of a certain type ``foo``. For
instance we decide that the default string type we will use for
``default_`` tagged ``foo`` specializations will be an
``std::string``.

In the :mod:`cpp-netlib` this is done by defining a ``string``
metafunction type that is specialized on the tag ``default_`` whose
nested ``type`` result is the type ``std::string``. In code this would
translate to:

.. code-block:: c++

    template <class Tag>
    struct string {
        typedef void type;
    };

    struct default_;

    template <>
    struct string<default_> {
        typedef std::string type;
    };

Template Metaprogramming
------------------------

Starting with version 0.7, the tag dispatch mechanism changed slightly to use
Boost.MPL_. The idea is still the same, although we can get a little smarter
than just using template specializations. Instead of just defining an opaque
type ``default_``, we use the Boost.MPL equivalent of a vector to define which
root types of properties this ``default_`` tag supports. The idea is to make the
opaque type ``default_`` inherit property tags which the library supports
internally as definite extension points.

.. _Boost.MPL: http://www.boost.org/libs/mpl/index.html

Our definition of the ``default_`` tag will then look something like the
following:

.. code-block:: c++

    typedef mpl::vector<default_string> default_tags;

    template <class Tag>
    struct components;

    typedef mpl::inherit_linearly<
        default_tags,
        mpl::inherit<mpl::placeholders::_1, mpl::placeholders::_2>
        >::type default_;

    template <class Tag>
    struct components<default_> {
        typedef default_tags type;
    };

In the above listing, ``default_string`` is what we call a "root" tag which is
meant to be combined with other "root" tags to form composite tags. In this case
our composite tag is the tag ``default_``. There are a number of these "root"
tags that :mod:`cpp-netlib` provides. These are in the namespace
``boost::network::tags`` and are defined in ``boost/network/tags.hpp``.

Using this technique we change slightly our definition of the ``string``
metafunction class into this:

.. code-block:: c++

    template <class Tag>
    struct unsupported_tag;

    template <class Tag>
    struct string :
        mpl::if_<
            is_base_of<
                tags::default_string,
                Tag
            >,
            std::string,
            unsupported_tag<Tag>
        >
    {};

Notice that we don't have the typedef for ``type`` in the body of ``string``
anymore, but we do inherit from ``mpl::if_``. Since ``mpl::if_`` is a template
metafunction itself, it contains a definition of the resulting ``type`` which
``string`` inherits.

You can see the real definition of the ``string`` metafunction in
``boost/network/traits/string.hpp``.

Using Tags
----------

Once we have the defined tag, we can then use this in the definition of our
types. In the definition of the type ``foo`` we use this type function
``string`` and pass the tag type parameter to determine what to use as
the string type in the context of the type ``foo``. In code this would
translate into:

.. code-block:: c++

    template <class Tag>
    struct foo {
        typedef typename string<Tag>::type string_type;

        // .. use string_type where you need a string.
    };

Using this approach we can support different types of strings for
different tags on the type ``foo``. In case we want to use a different
type of string for the tag ``default_`` we only change the
composition of the ``string_tags`` MPL vector. For example, in :mod:`cpp-netlib`
there is a root tag ``default_wstring`` which causes the ``string`` metafunction 
to define ``std::wstring`` as the resulting type.

The approach also allows for the control of the structure and features
of types like ``foo`` based on the specialization of the tag. Whole
type function families can be defined on tags where they are supported
and ignored in cases where they are not.

To illustrate let's define a new tag ``swappable``. Given the above
definition of ``foo``, we want to make the ``swappable``-tagged
``foo`` define a ``swap`` function that extends the original
``default_``-tagged ``foo``. In code this would look like:

.. code-block:: c++

    struct swappable;

    template <>
    struct foo<swappable> : foo<default_> {
        void swap(foo<swappable> & other) {
            // ...
        }
    };

We also for example want to enable an ADL-reachable ``swap`` function:

.. code-block:: c++

    struct swappable;

    inline
    void swap(foo<swappable> & left, foo<swappable> & right) {
        left.swap(right);
    }

Overall what the tag-based definition approach allows is for static
definition of extension points that ensures type-safety and
invariants. This keeps the whole extension mechanism static and yet
flexible.