Click here to Skip to main content
15,891,136 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 398.6K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    Copyright (c) 2001 Daniel C. Nuffer
    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.
=============================================================================*/

#include <cstdlib>
#include <cstring>

#include <boost/spirit/core/assert.hpp>
#include "wave/idllexer/re2clex/aq.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace idllexer {
namespace re2clex {

int aq_grow(aq_queue q)
{
    std::size_t new_size = q->max_size << 1;
    aq_stdelement* new_queue = (aq_stdelement*)realloc(q->queue,
            new_size * sizeof(aq_stdelement));

    BOOST_SPIRIT_ASSERT(q);
    BOOST_SPIRIT_ASSERT(q->max_size < 100000);
    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);

#define ASSERT_SIZE BOOST_SPIRIT_ASSERT( \
    ((q->tail + q->max_size + 1) - q->head) % q->max_size == \
    q->size % q->max_size)

    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    if (!new_queue)
    {
        BOOST_SPIRIT_ASSERT(0);
        return 0;
    }

    q->queue = new_queue;
    if (q->tail <= q->head) /* tail has wrapped around */
    {
        /* move the tail from the beginning to the end */
        memcpy(q->queue + q->max_size, q->queue,
                (q->tail + 1) * sizeof(aq_stdelement));
        q->tail += q->max_size;
    }
    q->max_size = new_size;

    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    return 1;
}

int aq_enqueue(aq_queue q, aq_stdelement e)
{
    BOOST_SPIRIT_ASSERT(q);
    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);


    if (AQ_FULL(q))
        if (!aq_grow(q))
            return 0;

    ++q->tail;
    if (q->tail == q->max_size)
        q->tail = 0;

    q->queue[q->tail] = e;
    ++q->size;

    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    return 1;
}

int aq_enqueue_front(aq_queue q, aq_stdelement e)
{
    BOOST_SPIRIT_ASSERT(q);

    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);


    if (AQ_FULL(q))
        if (!aq_grow(q))
            return 0;

    if (q->head == 0)
        q->head = q->max_size - 1;
    else
        --q->head;

    q->queue[q->head] = e;
    ++q->size;

    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    return 1;
}

int aq_serve(aq_queue q, aq_stdelement *e)
{

    BOOST_SPIRIT_ASSERT(q);
    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);


    if (AQ_EMPTY(q))
        return 0;

    *e = q->queue[q->head];
    return aq_pop(q);
}

int aq_pop(aq_queue q)
{

    BOOST_SPIRIT_ASSERT(q);
    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);


    if (AQ_EMPTY(q))
        return 0;

    ++q->head;
    if (q->head == q->max_size)
        q->head = 0;
    --q->size;

    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    return 1;
}

aq_queue aq_create(void)
{
    aq_queue q;

    q = (aq_queue)malloc(sizeof(aq_queuetype));
    if (!q)
    {
        return 0;
    }

    q->max_size = 8; /* initial size */
    q->queue = (aq_stdelement*)malloc(
            sizeof(aq_stdelement) * q->max_size);
    if (!q->queue)
    {
        free(q);
        return 0;
    }

    q->head = 0;
    q->tail = q->max_size - 1;
    q->size = 0;


    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    return q;
}

void aq_terminate(aq_queue q)
{

    BOOST_SPIRIT_ASSERT(q);
    BOOST_SPIRIT_ASSERT(q->size <= q->max_size);
    ASSERT_SIZE;
    BOOST_SPIRIT_ASSERT(q->head <= q->max_size);
    BOOST_SPIRIT_ASSERT(q->tail <= q->max_size);

    free(q->queue);
    free(q);
}

///////////////////////////////////////////////////////////////////////////////
}   // namespace re2clex
}   // namespace idllexer
}   // namespace wave

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