extension.qbk 19.2 KB
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
[/==============================================================================
    Copyright (C) 2001-2011 Joel de Guzman
    Copyright (C) 2006 Dan Marsden

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
===============================================================================/]
[section Extension]

[section:ext_full The Full Extension Mechanism]

The Fusion library is designed to be extensible, new sequences types can easily
be added. In fact, the library support for `std::pair`, `boost::array` and __mpl__
sequences is entirely provided using the extension mechanism.

The process for adding a new sequence type to Fusion is:

# Enable the __tag_dispatching__ mechanism used by Fusion for your sequence type
# Design an iterator type for the sequence
# Provide specialized behaviour for the intrinsic operations of the new Fusion sequence

[heading Our example]

In order to illustrate enabling a new sequence type for use with Fusion, we
are going to use the type:

    namespace example
    {
        struct example_struct
        {
            std::string name;
            int age;
            example_struct(
                const std::string& n,
                int a)
                : name(n), age(a)
            {}
        };
    }

We are going to pretend that this type has been provided by a 3rd party
library, and therefore cannot be modified. We shall work through all the
necessary steps to enable `example_struct` to serve as an __associative_sequence__
as described in the __quick_start__ guide.

[heading Enabling Tag Dispatching]

The Fusion extensibility mechanism uses __tag_dispatching__ to call the
correct code for a given sequence type. In order to exploit the tag
dispatching mechanism we must first declare a new tag type for the
mechanism to use. For example:

    namespace example {
        struct example_sequence_tag; // Only definition needed
    }

Next we need to enable the `traits::tag_of` metafunction to return our newly chosen
tag type for operations involving our sequence. This is done by specializing
`traits::tag_of` for our sequence type.

    #include <boost/fusion/support/tag_of_fwd.hpp>
    #include <boost/fusion/include/tag_of_fwd.hpp>

    namespace boost { namespace fusion { namespace traits {
        template<>
        struct tag_of<example_struct>
        {
            typedef example::example_sequence_tag type;
        };
    }}}

`traits::tag_of` also has a second template argument,
that can be used in conjunction with `boost::enable_if` to provide tag
support for groups of related types. This feature is not necessary
for our sequence, but for an example see the code in:

    #include <boost/fusion/adapted/array/tag_of.hpp>
    #include <boost/fusion/include/tag_of.hpp>

[heading Designing a suitable iterator]

We need an iterator to describe positions, and provide access to
the data within our sequence. As it is straightforward to do,
we are going to provide a random access iterator in our example.

We will use a simple design, in which the 2 members of
`example_struct` are given numbered indices, 0 for `name` and
1 for `age` respectively.

    template<typename Struct, int Pos>
    struct example_struct_iterator
        : boost::fusion::iterator_base<example_struct_iterator<Struct, Pos> >
    {
        BOOST_STATIC_ASSERT(Pos >=0 && Pos < 3);
        typedef Struct struct_type;
        typedef boost::mpl::int_<Pos> index;
        typedef boost::fusion::random_access_traversal_tag category;

        example_struct_iterator(Struct& str)
            : struct_(str) {}

        Struct& struct_;
    };

A quick summary of the details of our iterator:

# The iterator is parameterized by the type it is iterating over, and the index of the current element.
# The typedefs `struct_type` and `index` provide convenient access to information we will need later in
  the implementation.
# The typedef `category` allows the `traits::__category_of__` metafunction to establish
  the traversal category of the iterator.
# The constructor stores a reference to the `example_struct` being iterated over.

We also need to enable __tag_dispatching__ for our iterator type, with another specialization of
`traits::tag_of`.

In isolation, the iterator implementation is pretty dry. Things should become clearer as we
add features to our implementation.

[heading A first couple of instructive features]

To start with, we will get the __result_of_value_of__ metafunction working. To
do this, we provide a specialization of the `boost::fusion::extension::value_of_impl` template for
our iterator's tag type.

    template<>
    struct value_of_impl<example::example_struct_iterator_tag>
    {
        template<typename Iterator>
        struct apply;

        template<typename Struct>
        struct apply<example::example_struct_iterator<Struct, 0> >
        {
            typedef std::string type;
        };

        template<typename Struct>
        struct apply<example::example_struct_iterator<Struct, 1> >
        {
            typedef int type;
        };
    };

The implementation itself is pretty simple, it just uses 2 partial specializations to
provide the type of the 2 different members of `example_struct`, based on the index of the iterator.

To understand how `value_of_impl` is used by the library we will look at the implementation of __result_of_value_of__:

    template <typename Iterator>
    struct value_of
        : extension::value_of_impl<typename detail::tag_of<Iterator>::type>::
            template apply<Iterator>
    {};

So __result_of_value_of__ uses __tag_dispatching__ to select an __mpl_metafunction_class__
to provide its functionality. You will notice this pattern throughout the
implementation of Fusion.

Ok, lets enable dereferencing of our iterator. In this case we must provide a suitable
specialization of `deref_impl`.

    template<>
    struct deref_impl<example::example_struct_iterator_tag>
    {
        template<typename Iterator>
        struct apply;

        template<typename Struct>
        struct apply<example::example_struct_iterator<Struct, 0> >
        {
            typedef typename mpl::if_<
                is_const<Struct>, std::string const&, std::string&>::type type;

            static type
            call(example::example_struct_iterator<Struct, 0> const& it)
            {
                return it.struct_.name;
            }
        };

        template<typename Struct>
        struct apply<example::example_struct_iterator<Struct, 1> >
        {
            typedef typename mpl::if_<
                is_const<Struct>, int const&, int&>::type type;

            static type
            call(example::example_struct_iterator<Struct, 1> const& it)
            {
                    return it.struct_.age;
                }
            };
        };
    }

The use of `deref_impl` is very similar to that of `value_of_impl`, but it also
provides some runtime functionality this time via the `call` static member function.
To see how `deref_impl` is used, lets have a look at the implementation of __deref__:

    namespace result_of
    {
        template <typename Iterator>
        struct __deref__
            : extension::deref_impl<typename detail::tag_of<Iterator>::type>::
                template apply<Iterator>
        {};
    }

    template <typename Iterator>
    typename result_of::deref<Iterator>::type
    __deref__(Iterator const& i)
    {
        typedef result_of::deref<Iterator> deref_meta;
        return deref_meta::call(i);
    }

So again __result_of_deref__ uses __tag_dispatching__ in exactly the
same way as the __result_of_value_of__ implementation. The runtime functionality used
by __deref__ is provided by the `call` static function of the selected
__mpl_metafunction_class__.

The actual implementation of `deref_impl` is slightly more complex than that of  `value_of_impl`.
We also need to implement the `call` function, which returns a reference
to the appropriate member of the underlying sequence. We also require a little
bit of metaprogramming to return `const` references if the underlying sequence
is const.

[note Although there is a fair amount of left to do to produce a fully fledged
Fusion sequence, __result_of_value_of__ and __deref__ illustrate all the significant concepts
required. The remainder of the process is very repetitive, simply requiring
implementation of a suitable `xxxx_impl` for each feature `xxxx`.
]

[heading Implementing the remaining iterator functionality]

Ok, now we have seen the way __result_of_value_of__ and __deref__ work, everything else will work
 in pretty much the same way. Lets start with forward iteration,
by providing a `next_impl`:

    template<>
    struct next_impl<example::example_struct_iterator_tag>
    {
        template<typename Iterator>
        struct apply
        {
            typedef typename Iterator::struct_type struct_type;
            typedef typename Iterator::index index;
            typedef example::example_struct_iterator<struct_type, index::value + 1> type;

            static type
            call(Iterator const& i)
            {
                 return type(i.struct_);
            }
        };
    };

This should be very familiar from our `deref_impl` implementation, we will be
using this approach again and again now. Our design is simply to increment
the `index` counter to move on to the next element. The various other iterator
manipulations we need to perform will all just involve simple calculations
with the `index` variables.

We also need to provide a suitable `equal_to_impl` so that iterators can be
correctly compared. A __bidirectional_iterator__ will also need an implementation of `prior_impl`. For a
__random_access_iterator__ `distance_impl` and `advance_impl` also need to be provided
in order to satisfy the necessary complexity guarantees. As our iterator is
a __random_access_iterator__ we will have to implement all of these functions.

Full implementations of `prior_impl`, `advance_impl`, `distance_impl` and `equal_to_impl` are
provided in the example code.

[heading Implementing the intrinsic functions of the sequence]

In order that Fusion can correctly identify our sequence as a Fusion sequence, we
need to enable `is_sequence` for our sequence type. As usual we just create
an `impl` type specialized for our sequence tag:

     template<>
     struct is_sequence_impl<example::example_sequence_tag>
     {
         template<typename T>
         struct apply : mpl::true_ {};
     };

We've some similar formalities to complete, providing `category_of_impl` so Fusion
can correctly identify our sequence type, and `is_view_impl` so Fusion can correctly
identify our sequence as not being a __view__ type. Implementations are
provide in the example code.

Now we've completed some formalities, on to more interesting features. Lets get
__begin__ working so that we can get an iterator to start accessing the data in
our sequence.

    template<>
    struct begin_impl<example::example_sequence_tag>
    {
        template<typename Sequence>
        struct apply
        {
            typedef example::example_struct_iterator<Sequence, 0> type;

            static type
            call(Sequence& seq)
            {
                return type(seq);
            }
        };
    };

The implementation uses the same ideas we have applied throughout, in this case
we are just creating one of the iterators we developed earlier, pointing to the
first element in the sequence. The implementation of __end__ is very similar, and
is provided in the example code.

For our __random_access_sequence__ we will also need to implement `size_impl`,
`value_at_impl` and `at_impl`.

[heading Enabling our type as an associative sequence]

In order for `example_struct` to serve as an associative forward sequence,
we need to adapt the traversal category of our sequence and our iterator
accordingly and enable 3 intrinsic sequence lookup features, __at_key__,
__result_of_value_at_key__ and __has_key__. We also need to enable 3 iterator lookup
features, __result_of_key_of__, __result_of_value_of_data__ and __deref_data__.

To implement `at_key_impl` we need to associate the `fields::name` and `fields::age`
types described in the __quick_start__ guide with the appropriate members of `example_struct`.
Our implementation is as follows:

    template<>
    struct at_key_impl<example::example_sequence_tag>
    {
        template<typename Sequence, typename Key>
        struct apply;

        template<typename Sequence>
        struct apply<Sequence, fields::name>
        {
            typedef typename mpl::if_<
                is_const<Sequence>,
                std::string const&,
                std::string&>::type type;

            static type
            call(Sequence& seq)
            {
                return seq.name;
            };
        };

        template<typename Sequence>
        struct apply<Sequence, fields::age>
        {
            typedef typename mpl::if_<
                is_const<Sequence>,
                int const&,
                int&>::type type;

            static type
            call(Sequence& seq)
            {
                return seq.age;
            };
        };
    };

Its all very similar to the implementations we've seen previously,
such as `deref_impl` and `value_of_impl`. Instead of identifying
the members by index or position, we are now selecting them using
the types `fields::name` and `fields::age`. The implementations of
the other functions are equally straightforward, and are provided in
the example code.

[heading Summary]

We've now worked through the entire process for adding a new random
access sequence and we've also enabled our type to serve as an associative
sequence. The implementation was slightly long-winded, but followed
a simple repeating pattern.

The support for `std::pair`, __mpl__ sequences, and `boost::array` all
use the same approach, and provide additional examples of the approach
for a variety of types.

[endsect]

[section Sequence Facade]

[heading Description]
The __sequence_facade__ template provides an intrusive mechanism for
producing a conforming Fusion sequence.

[heading Synopsis]
    template<typename Derived, typename TravesalTag, typename IsView = mpl::false_>
    struct sequence_facade;

[heading Usage]
The user of __sequence_facade__ derives his sequence type from a specialization of __sequence_facade__ and passes the derived sequence type as the first template parameter. The second template parameter should be the traversal category of the sequence being implemented. The 3rd parameter should be set to `mpl::true_` if the sequence is a view.

The user must implement the key expressions required by their sequence type.

[table Parameters
[[Name][Description]]
[[`sequence`, `Seq`][A type derived from __sequence_facade__]]
[[`N`][An __mpl_integral_constant__]]
]

[table Key Expressions
[[Expression][Result]]
[[`sequence::template begin<Seq>::type`][The type of an iterator to the beginning of a sequence of type `Seq`]]
[[`sequence::template begin<Seq>::call(seq)`][An iterator to the beginning of sequence `seq`]]
[[`sequence::template end<Seq>::type`][The type of an iterator to the end of a sequence of type `Seq`]]
[[`sequence::template end<Seq>::call(seq)`][An iterator to the end of sequence `seq`]]
[[`sequence::template size<Seq>::type`][The size of a sequence of type `Seq` as an __mpl_integral_constant__]]
[[`sequence::template size<Seq>::call(seq)`][The size of sequence `seq`]]
[[`sequence::template empty<Seq>::type`][Returns `mpl::true_` if `Seq` has zero elements, `mpl::false_` otherwise.]]
[[`sequence::template empty<Seq>::call`][Returns a type convertible to `bool` that evaluates to true if the sequence is empty, else, evaluates to false. ]]
[[`sequence::template at<Seq, N>::type`][The type of element `N` in a sequence of type `Seq`]]
[[`sequence::template at<Seq, N>::call(seq)`][Element `N` in sequence `seq`]]
[[`sequence::template value_at<Sequence, N>::type`][The type of the `N`th element in a sequence of type `Seq`]]
]

[heading Include]

    #include <boost/fusion/sequence/sequence_facade.hpp>
    #include <boost/fusion/include/sequence_facade.hpp>

[heading Example]
A full working example using __sequence_facade__ is provided in triple.cpp in the extension examples.

[endsect]

[section Iterator Facade]

[heading Description]
The __iterator_facade__ template provides an intrusive mechanism for
producing a conforming Fusion iterator.

[heading Synopsis]

    template<typename Derived, typename TravesalTag>
    struct iterator_facade;

[heading Usage]
The user of iterator_facade derives his iterator type from a specialization of iterator_facade and passes the derived iterator type as the first template parameter. The second template parameter should be the traversal category of the iterator being implemented.

The user must implement the key expressions required by their iterator type.

[table Parameters
[[Name][Description]]
[[`iterator`, `It`, `It1`, `It2`][A type derived from __iterator_facade__]]
[[`N`][An __mpl_integral_constant__]]
]

[table Key Expressions
[[Expression][Result][Default]]
[[`iterator::template value_of<It>::type`][The element stored at iterator position `It`][None]]
[[`iterator::template deref<It>::type`][The type returned when dereferencing an iterator of type `It`][None]]
[[`iterator::template deref<It>::call(it)`][Dereferences iterator `it`][None]]
[[`iterator::template next<It>::type`][The type of the next element from `It`][None]]
[[`iterator::template next<It>::call(it)`][The next iterator after `it`][None]]
[[`iterator::template prior<It>::type`][The type of the next element from `It`][None]]
[[`iterator::template prior<It>::call(it)`][The next iterator after `it`][None]]
[[`iterator::template advance<It, N>::type`][The type of an iterator advanced `N` elements from `It`][Implemented in terms of `next` and `prior`]]
[[`iterator::template advance<It, N>::call(it)`][An iterator advanced `N` elements from `it`][Implemented in terms of `next` and `prior`]]
[[`iterator::template distance<It1, It2>::type`][The distance between iterators of type `It1` and `It2` as an __mpl_integral_constant__][None]]
[[`iterator::template distance<It1, It2>::call(it1, it2)`][The distance between iterator `it1` and `it2`][None]]
[[`iterator::template equal_to<It1, It2>::type`][Returns `mpl::true_` if `It1` is equal to `It2`, `mpl::false_` otherwise.][`boost::same_type<It1, It2>::type`]]
[[`iterator::template equal_to<It1, It2>::call(it1, it2)`][Returns a type convertible to `bool` that evaluates to `true` if `It1` is equal to `It2`, `false` otherwise.][`boost::same_type<It1, It2>::type()`]]
[[`iterator::template key_of<It>::type`][The key type associated with the element from `It`][None]]
[[`iterator::template value_of_data<It>::type`][The type of the data property associated with the element from `It`][None]]
[[`iterator::template deref_data<It>::type`][The type that will be returned by dereferencing the data property of the element from `It`][None]]
[[`iterator::template deref_data<It>::call(it)`][Deferences the data property associated with the element referenced by `it`][None]]
]

[heading Header]

    #include <boost/fusion/iterator/iterator_facade.hpp>
    #include <boost/fusion/include/iterator_facade.hpp>

[heading Example]
A full working example using __iterator_facade__ is provided in triple.cpp in the extension examples.

[endsect]

[endsect]