Blame view

libmu_cpp/iterator.cc 2.44 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 2004, 2006, 2007, 2009, 2010 Free Software Foundation,
   Inc.
4 5 6 7

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
8
   version 3 of the License, or (at your option) any later version.
9 10 11 12 13 14

   This library 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
   Lesser General Public License for more details.

15
   You should have received a copy of the GNU Lesser General
16 17
   Public License along with this library.  If not, see 
   <http://www.gnu.org/licenses/>. */
18 19 20 21 22 23 24 25 26 27 28

#include <mailutils/cpp/iterator.h>

using namespace mailutils;

//
// Iterator
//

Iterator :: Iterator (const List& lst)
{
29
  int status = mu_list_get_iterator (lst.mu_list, &mu_iter);
30 31 32 33 34 35
  if (status)
    throw Exception ("Iterator::Iterator", status);

  this->pList = (List*) &lst;
}

36
Iterator :: Iterator (const mu_iterator_t iter)
37 38 39 40 41 42 43 44 45 46
{
  if (iter == 0)
    throw Exception ("Iterator::Iterator", EINVAL);

  this->mu_iter = iter;
  this->pList = 0;
}

Iterator :: ~Iterator ()
{
47
  mu_iterator_destroy (&mu_iter);
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61
bool
Iterator :: operator == (const Iterator& iter)
{
  return mu_iter == iter.mu_iter;
}

bool
Iterator :: operator != (const Iterator& iter)
{
  return mu_iter != iter.mu_iter;
}

62
void
63
Iterator :: first ()
64
{
65
  mu_iterator_first (mu_iter);
66 67 68
}

void
69
Iterator :: next ()
70
{
71
  mu_iterator_next (mu_iter);
72 73 74 75 76
}

Iterator&
Iterator :: operator ++ (int)
{
77
  mu_iterator_next (mu_iter);
78 79 80 81
  return *this;
}

void
82
Iterator :: current (void** pitem)
83
{
84
  int status = mu_iterator_current (mu_iter, pitem);
85
  if (status)
86
    throw Exception ("Iterator::current", status);
87 88 89
}

void*
90
Iterator :: current ()
91 92 93
{
  void* pitem;

94
  int status = mu_iterator_current (mu_iter, &pitem);
95
  if (status)
96
    throw Exception ("Iterator::current", status);
97 98 99 100 101

  return pitem;
}

bool
102
Iterator :: is_done ()
103
{
104
  return (bool) mu_iterator_is_done (mu_iter);
105 106 107
}

List&
108
Iterator :: get_list ()
109 110
{
  if (!pList)
111
    throw Exception ("Iterator::get_list", ENOTSUP);
112 113 114 115
  return *pList;
}

void
116
Iterator :: dup (Iterator*& piter, const Iterator& orig)
117
{
118
  mu_iterator_t iter;
119

120
  int status = mu_iterator_dup (&iter, orig.mu_iter);
121
  if (status)
122
    throw Exception ("Iterator::dup", status);
123 124 125 126

  piter->mu_iter = iter;
}