Move sourcecode into 'senf/' directory
[senf.git] / senf / Packets / ListParser.ct
diff --git a/senf/Packets/ListParser.ct b/senf/Packets/ListParser.ct
new file mode 100644 (file)
index 0000000..19fccdb
--- /dev/null
@@ -0,0 +1,188 @@
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.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 ListParser non-inline template implementation  */
+
+#include "ListParser.ih"
+
+// Custom includes
+#include "../Utils/senfassert.hh"
+
+#define prefix_
+///////////////////////////////ct.p////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::ListParser<ElementParser,ListPolicy>
+
+template <class ListPolicy>
+prefix_ void senf::ListParser<ListPolicy>::init()
+    const
+{
+    ListPolicy::init(i(),state());
+    container c (*this);
+    typename container::iterator i (c.begin());
+    typename container::iterator const e (c.end());
+    for(; i!=e; ++i)
+        i->init();
+}
+
+template <class ListPolicy>
+prefix_ typename senf::ListParser<ListPolicy>::value_type
+senf::ListParser<ListPolicy>::back()
+    const
+{
+    SENF_ASSERT( ! empty() );
+    container c(*this);
+    typename container::iterator i (c.begin()), j;
+    typename container::iterator const e (c.end());
+    for (j=i; i!=e; j=i, ++i) ;
+    return *j;
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::ListParser_Container<ListPolicy>
+
+template <class ListPolicy>
+prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
+senf::ListParser_Container<ListPolicy>::back()
+    const
+{
+    SENF_ASSERT( ! empty() );
+    iterator i (begin()), j;
+    iterator const e (end());
+    for (j=i; i!=e; j=i, ++i) ;
+    return *j;
+}
+
+template <class ListPolicy>
+prefix_ void senf::ListParser_Container<ListPolicy>::shift(iterator pos, size_type n)
+{
+    ListPolicy::update(*this);
+    safe_data_iterator sp (data(),pos.raw());
+    for (; n>0; --n) {
+        data().insert(sp,senf::init_bytes<value_type>::value,0);
+        value_type(sp,state()).init();
+        ListPolicy::insert(*this,sp);
+    }
+}
+
+template <class ListPolicy>
+template <class Value>
+prefix_ void senf::ListParser_Container<ListPolicy>::insert(iterator pos,
+                                                            size_type n,
+                                                            Value const & t)
+{
+    ListPolicy::update(*this);
+    safe_data_iterator sp (data(),pos.raw());
+    for (; n>0; --n) {
+        data().insert(sp,senf::init_bytes<value_type>::value,0);
+        value_type(sp,state()).init();
+        value_type(sp,state()) << t;
+        ListPolicy::insert(*this,sp);
+    }
+}
+
+#ifndef DOXYGEN
+template <class ListPolicy>
+template <class ForwardIterator>
+prefix_ void senf::ListParser_Container<ListPolicy>::
+insert(iterator pos, ForwardIterator f, ForwardIterator l,
+       typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type *)
+{
+    ListPolicy::update(*this);
+    safe_data_iterator sp (data(),pos.raw());
+    for (; f!=l; ++f) {
+        data().insert(sp,senf::init_bytes<value_type>::value,0);
+        value_type(sp,state()).init();
+        value_type(sp,state()) << *f;
+        ListPolicy::insert(*this,sp);
+        sp += senf::bytes(value_type(sp,state()));
+    }
+}
+#else
+template <class ListPolicy>
+template <class ForwardIterator>
+prefix_ void senf::ListParser_Container<ListPolicy>::
+insert(iterator pos, ForwardIterator f, ForwardIterator l)
+{}
+#endif
+
+template <class ListPolicy>
+prefix_ void senf::ListParser_Container<ListPolicy>::erase(iterator pos,
+                                                           size_type n)
+{
+    ListPolicy::update(*this);
+    safe_data_iterator sp (data(),pos.raw());
+    for (; n>0; --n) {
+        ListPolicy::erase(*this,sp);
+        data().erase(sp,boost::next(sp,senf::bytes(value_type(sp,state()))));
+    }
+}
+
+template <class ListPolicy>
+prefix_ void senf::ListParser_Container<ListPolicy>::clear()
+{
+    size_type sz (bytes());
+    if (sz > ListPolicy::init_bytes)
+        data().erase(boost::next(i(),ListPolicy::init_bytes),boost::next(i(),sz));
+    else
+        data().insert(boost::next(i(),sz), ListPolicy::init_bytes-sz, 0u);
+    std::fill(i(),boost::next(i(),ListPolicy::init_bytes), 0u);
+    ListPolicy::init(i(),state());
+}
+
+template <class ListPolicy>
+prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n)
+{
+    size_type sz (size());
+    if (sz>n)
+        erase(boost::next(begin(),n),end());
+    else
+        push_back_space(n-sz);
+}
+
+template <class ListPolicy>
+template <class Value>
+prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n,
+                                                            Value value)
+{
+    size_type sz (size());
+    if (sz>n)
+        erase(boost::next(begin(),n),end());
+    else
+        push_back(value,n-sz);
+}
+
+///////////////////////////////ct.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End: