// $Id$
//
// Copyright (C) 2006
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

/** \file
    \brief ParseArray inline template implementation */

#include "ParseArray.ih"

// Custom includes

#define prefix_ inline
///////////////////////////////cti.p///////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// senf::Parse_Array<elements,ElementParser>

template <unsigned elements, class ElementParser>
prefix_ senf::Parse_Array<elements,ElementParser>::Parse_Array(data_iterator i, state_type s)
    : PacketParserBase(i,s,fixed_bytes)
{}

// Container interface

template <unsigned elements, class ElementParser>
prefix_ typename senf::Parse_Array<elements,ElementParser>::size_type
senf::Parse_Array<elements,ElementParser>::size()
{
    return elements;
}

template <unsigned elements, class ElementParser>
prefix_ typename senf::Parse_Array<elements,ElementParser>::iterator
senf::Parse_Array<elements,ElementParser>::begin()
    const
{
    return iterator(i(),state());
}

template <unsigned elements, class ElementParser>
prefix_ typename senf::Parse_Array<elements,ElementParser>::iterator
senf::Parse_Array<elements,ElementParser>::end()
    const
{
    return iterator(boost::next(i(),fixed_bytes),state());
}

template <unsigned elements, class ElementParser>
prefix_ typename senf::Parse_Array<elements,ElementParser>::value_type
senf::Parse_Array<elements,ElementParser>::operator[](difference_type i)
    const
{
    BOOST_ASSERT( i>=0 && i < difference_type(elements) );
    return begin()[i];
}

///////////////////////////////////////////////////////////////////////////
// senf::detail::Parse_Array_iterator<ElementParser>

template <class ElementParser>
prefix_ senf::detail::Parse_Array_iterator<ElementParser>::Parse_Array_iterator()
    : i_()
{}

template <class ElementParser>
prefix_  senf::detail::Parse_Array_iterator<ElementParser>::
Parse_Array_iterator(PacketParserBase::data_iterator const & i, PacketParserBase::state_type s)
    : i_(i), s_(s)
{}

template <class ElementParser>
prefix_ ElementParser 
senf::detail::Parse_Array_iterator<ElementParser>::operator[](int i)
    const
{
    return (*this+i).dereference();
}

template <class ElementParser>
prefix_ senf::PacketParserBase::data_iterator
senf::detail::Parse_Array_iterator<ElementParser>::raw()
    const
{
    return i_;
}

// iterator_facade interface

template <class ElementParser>
prefix_ ElementParser 
senf::detail::Parse_Array_iterator<ElementParser>::dereference()
    const
{
    return ElementParser(i_,s_);
}

template <class ElementParser>
prefix_ bool
senf::detail::Parse_Array_iterator<ElementParser>::
equal(Parse_Array_iterator const & other)
    const
{
    return i_==other.i_;
}

template <class ElementParser>
prefix_ int senf::detail::Parse_Array_iterator<ElementParser>::
distance_to(Parse_Array_iterator const & other)
    const
{
    return (other.i_-i_)/ElementParser::fixed_bytes;
}

template <class ElementParser>
prefix_ void senf::detail::Parse_Array_iterator<ElementParser>::increment()
{
    i_ += ElementParser::fixed_bytes;
}

template <class ElementParser>
prefix_ void senf::detail::Parse_Array_iterator<ElementParser>::decrement()
{
    i_ -= ElementParser::fixed_bytes;
}

template <class ElementParser>
prefix_ void senf::detail::Parse_Array_iterator<ElementParser>::advance(int n)
{
    i_ += n*ElementParser::fixed_bytes;
}

///////////////////////////////cti.e///////////////////////////////////////
#undef prefix_


// Local Variables:
// mode: c++
// fill-column: 100
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
// compile-command: "scons -u test"
// comment-column: 40
// End: