Click here to Skip to main content
15,860,861 members
Articles / Programming Languages / C++

Wave: a Standard conformant C++ preprocessor library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (58 votes)
10 Jan 200413 min read 392.1K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    Copyright (c) 2001-2004 Hartmut Kaiser
    http://spirit.sourceforge.net/

    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)

    See Copyright.txt for full acknowledgements.
=============================================================================*/

#if !defined(ITERATION_CONTEXT_HPP_9556CD16_F11E_4ADC_AC8B_FB9A174BE664_INCLUDED)
#define ITERATION_CONTEXT_HPP_9556CD16_F11E_4ADC_AC8B_FB9A174BE664_INCLUDED

#include <cstdlib>
#include <stack>

#include "wave/cpp_exceptions.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace util {

#if !defined(WAVE_MAX_INCLUDE_LEVEL_DEPTH)
#define WAVE_MAX_INCLUDE_LEVEL_DEPTH 1024
#endif // !defined(WAVE_MAX_INCLUDE_LEVEL_DEPTH)

///////////////////////////////////////////////////////////////////////////////
template <typename IterationContextT>
class iteration_context_stack 
{
    typedef std::stack<IterationContextT> base_t;
    
public:
    typedef typename base_t::size_type size_type;
    
    iteration_context_stack()
    :   max_include_nesting_depth(WAVE_MAX_INCLUDE_LEVEL_DEPTH)
    {}
    
    void set_max_include_nesting_depth(size_type new_depth)
        {  max_include_nesting_depth = new_depth; }
    size_type get_max_include_nesting_depth() const
        { return max_include_nesting_depth; }

    typename base_t::size_type size() const { return iter_ctx.size(); }
    typename base_t::value_type &top() { return iter_ctx.top(); }
    void pop() { iter_ctx.pop(); }
    
    template <typename PositionT>
    void push(PositionT const &pos, typename base_t::value_type const &val)
    { 
        if (iter_ctx.size() == max_include_nesting_depth) {
        char buffer[22];    // 21 bytes holds all NUL-terminated unsigned 64-bit numbers

            using namespace std;    // for some systems ltoa is in namespace std
            sprintf(buffer, "%d", max_include_nesting_depth);
            CPP_THROW(preprocess_exception, include_nesting_too_deep, buffer,
                pos);
        }
        iter_ctx.push(val); 
    }
        
private:
    size_type max_include_nesting_depth;
    base_t iter_ctx;
};

///////////////////////////////////////////////////////////////////////////////
}   // namespace util
}   // namespace wave

#endif // !defined(ITERATION_CONTEXT_HPP_9556CD16_F11E_4ADC_AC8B_FB9A174BE664_INCLUDED)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

Comments and Discussions