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/cpp_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 wave {
namespace iteration_context_policies {

    struct load_file_to_string {
    
        template <typename IterContextT>
        class inner {
        
        public:
            // expose the begin and end iterators for the
            // included file
            template <typename PositionT>
            static 
            void init_iterators(IterContextT &iter_ctx, 
                PositionT const &act_pos)
            {
                typedef typename IterContextT::iterator_t iterator_t;
                
                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(instream.rdbuf()),
                    std::istreambuf_iterator());

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

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

}   // namespace iteration_context_policies
} // namespace wave

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_t 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 abritrary iterator type, representing the actual input stream.

Member Functions

init_iterators

    template <typename PositionT>
    static void init_iterators(
        IterContextT iter_ctx, 
        PositionT const &act_pos);

The init_iterators function is called, when an #include 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.


Last updated: Monday, January 5, 2004 14:57