The Input Policy

#  | The Input Policy |

> Introduction
> Header 'wave/cpp_iteration_context.hpp' synopsis
> Template parameters
> Member functions

## Introduction

The input policy type may be specified as a template parameter to the `wave::context` object and is used for customizing the way, how an included file is to be represented by a pair of iterators pointing to the beginning and the end of the resulting input sequence. If this template parameter is not given while instantiating the context object, it defaults to the `iteration_context_policies::load_file_to_string` type.

## Header wave/iteration_context.hpp synopsis

The following code listing does not show the required interface only, but for brevity reasons the whole implementation of an input policy, which loads the given file into a string variable and exposes the begin() and end() iterators of this string to the `Wave` library.

    namespace boost {
    namespace wave {
    namespace iteration_context_policies {

        struct load_file_to_string {

            template <typename IterContext>
            class inner {

            public:
                // expose the begin and end iterators for the
                // included file
                template <typename Position>
                static
                void init_iterators(IterContext&iter_ctx,
                    Position const &act_pos)
                {
                    typedef typename IterContext::iterator_type iterator_type;

                    std::ifstream instream(iter_ctx.filename.c_str());
                    if (!instream.is_open()) {
                        CPP_THROW(preprocess_exception, bad_include_file,
                            iter_ctx.filename, act_pos);
                    }

                    iter_ctx.instring = std::string(
                        std::istreambuf_iterator<char>(instream.rdbuf()),
                        std::istreambuf_iterator<char>());

                    iter_ctx.first = iterator_type(iter_ctx.instring.begin(),
                        iter_ctx.instring.end(),
                        PositionT(iter_ctx.filename));
                    iter_ctx.last = iterator_type();
                }

            private:
                std::string instring;
            };
        };

    }   // namespace iteration_context_policies
    }   // namespace wave
    }   // namespace boost

As you can see, an `input_policy` for the `wave::context` object should implement one function only, the init_iterators function. The policy shown is implemented with the help of an embedded class to avoid the need for template template parameters, which aren't implemented by all systems today. This embedded class should have the name `inner`.

### Template Parameters

The `inner` class is instantiated with one template parameter, the iteration context type, from which the policy is a part of. The iterator type `iterator_type` which is used to access the underlying input stream has to be derived through a typedef as shown. The iterator pair to initialize (which is accessible as `iter_ctx.first` and `iter_ctx.last`) has to initialized from an arbitrary iterator type, representing the actual input stream.

### Member Functions

init_iterators

        template <typename Position>
        static void init_iterators(
            IterContext iter_ctx,
            Position const &act_pos);

directive was found in the input token stream. The main rationale for this function is to initialize the pair of iterators `iter_ctx.first` and `iter_ctx.last`, which are to be used to access the input stream corresponding to the include file to be inserted from inside the preprocessing engine.

Copyright © 2003-2011 Hartmut Kaiser
Distributed under 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)

Last updated: Sunday, October 12, 2008 20:14