view gen/utils.h @ 1351:8d501abecd24

Initial (but disabled) fix for ticket #294 , the actual part that fixes the bug is in a #if 0 block as I'm afraid it will cause regressions. I'm most likely not going to be around tonight, and maybe not tomorrow as well, so I'm pushing it in case someone wants to run some serious testing/investigate the problem noted in llvmhelpers.cpp : realignOffset .
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Thu, 14 May 2009 17:20:17 +0200
parents ec1d9dc1d32a
children
line wrap: on
line source

#ifndef __LDC_GEN_UTILS_H__
#define __LDC_GEN_UTILS_H__

#include "root.h"

/// Very simple templated iterator for DMD ArrayS.
template<class C>
struct ArrayIter
{
    Array* array;
    size_t index;

    ArrayIter(Array& arr, size_t idx = 0)
    :   array(&arr), index(idx)
    { }
    ArrayIter(Array* arr, size_t idx = 0)
    :   array(arr), index(idx)
    { assert(arr && "null array"); }

    ArrayIter<C>& operator=(const Array& arr)
    {
        array = &arr;
        index = 0;
        return *this;
    }
    ArrayIter<C>& operator=(const Array* arr)
    {
        assert(arr && "null array");
        array = arr;
        index = 0;
        return *this;
    }

    bool done()
    {
        return index >= array->dim;
    }
    bool more()
    {
        return index < array->dim;
    }

    C* get() {
        return static_cast<C*>(array->data[index]);
    }
    C* operator->() {
        return get();
    }
    C* operator*() {
        return get();
    }

    void next()
    {
        ++index;
    }

    bool operator==(const ArrayIter<C>& other) {
        return &array->data[index] == &other.array->data[other.index];
    }
};

// some aliases
typedef ArrayIter<Dsymbol> DsymbolIter;
typedef ArrayIter<FuncDeclaration> FuncDeclarationIter;
typedef ArrayIter<VarDeclaration> VarDeclarationIter;

#endif