Loading...
Searching...
No Matches
iterators.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
42#pragma once
43
44#include "xact.h"
45#include "post.h"
46#include "account.h"
47#include "temps.h"
48
49namespace ledger {
50
51class journal_t;
52class report_t;
53
54template <typename Derived, typename Value, typename CategoryOrTraversal>
56 : public boost::iterator_facade<Derived, Value, CategoryOrTraversal>
57{
58 typedef Value node_base;
59
60public:
70
71 explicit iterator_facade_base(node_base p) : m_node(p) {}
72
73 void increment();
74
75private:
77
78 bool equal(iterator_facade_base const& other) const {
79 return this->m_node == other.m_node;
80 }
81
82 node_base& dereference() const { return const_cast<node_base&>(m_node); }
83
84protected:
85 node_base m_node;
86};
87
89 : public iterator_facade_base<xact_posts_iterator, post_t *,
90 boost::forward_traversal_tag>
91{
92 posts_list::iterator posts_i;
93 posts_list::iterator posts_end;
94
95 bool posts_uninitialized;
96
97public:
98 xact_posts_iterator() : posts_uninitialized(true) {
100 }
102 : posts_uninitialized(true) {
103 reset(xact);
105 }
109 posts_i(i.posts_i), posts_end(i.posts_end),
110 posts_uninitialized(i.posts_uninitialized) {
112 }
116
117 void reset(xact_t& xact) {
118 posts_i = xact.posts.begin();
119 posts_end = xact.posts.end();
120
121 posts_uninitialized = false;
122
123 increment();
124 }
125
126 void increment() {
127 if (posts_uninitialized || posts_i == posts_end)
128 m_node = NULL;
129 else
130 m_node = *posts_i++;
131 }
132};
133
135 : public iterator_facade_base<xacts_iterator, xact_t *,
136 boost::forward_traversal_tag>
137{
138public:
139 xacts_list::iterator xacts_i;
140 xacts_list::iterator xacts_end;
141
143
148 reset(journal);
149 TRACE_CTOR(xacts_iterator, "journal_t&");
150 }
151 xacts_iterator(xacts_list::iterator beg,
152 xacts_list::iterator end) : xacts_uninitialized(false) {
153 reset(beg, end);
154 TRACE_CTOR(xacts_iterator, "xacts_list::iterator, xacts_list::iterator");
155 }
166
167 void reset(journal_t& journal);
168
169 void reset(xacts_list::iterator beg, xacts_list::iterator end) {
170 xacts_i = beg;
171 xacts_end = end;
172 increment();
173 }
174
175 void increment();
176};
177
179 : public iterator_facade_base<journal_posts_iterator, post_t *,
180 boost::forward_traversal_tag>
181{
182 xacts_iterator xacts;
184
185public:
190 reset(journal);
191 TRACE_CTOR(journal_posts_iterator, "journal_t&");
192 }
202
203 void reset(journal_t& journal);
204
205 void increment();
206};
207
242
244 : public iterator_facade_base<basic_accounts_iterator, account_t *,
245 boost::forward_traversal_tag>
246{
247 std::list<accounts_map::const_iterator> accounts_i;
248 std::list<accounts_map::const_iterator> accounts_end;
249
250public:
255 push_back(account);
256 increment();
257 TRACE_CTOR(basic_accounts_iterator, "account_t&");
258 }
262 accounts_i(i.accounts_i), accounts_end(i.accounts_end) {
264 }
268
269 void increment();
270
271private:
272 void push_back(account_t& account) {
273 accounts_i.push_back(account.accounts.begin());
274 accounts_end.push_back(account.accounts.end());
275 }
276};
277
279 : public iterator_facade_base<sorted_accounts_iterator, account_t *,
280 boost::forward_traversal_tag>
281{
282 expr_t sort_cmp;
283 report_t& report;
284 bool flatten_all;
285
286 typedef std::deque<account_t *> accounts_deque_t;
287
288 std::list<accounts_deque_t> accounts_list;
289 std::list<accounts_deque_t::const_iterator> sorted_accounts_i;
290 std::list<accounts_deque_t::const_iterator> sorted_accounts_end;
291
292public:
294 const expr_t& _sort_cmp,
296 bool _flatten_all)
297 : sort_cmp(_sort_cmp), report(_report),
298 flatten_all(_flatten_all) {
299 push_back(account);
300 increment();
302 "account_t&, expr_t, report_t&, bool");
303 }
307 sort_cmp(i.sort_cmp), report(i.report),
308 flatten_all(i.flatten_all),
309 accounts_list(i.accounts_list),
310 sorted_accounts_i(i.sorted_accounts_i),
311 sorted_accounts_end(i.sorted_accounts_end) {
313 }
317
318 void increment();
319
320private:
321 void push_back(account_t& account);
322 void push_all(account_t& account, accounts_deque_t& deque);
323 void sort_accounts(account_t& account, accounts_deque_t& deque);
324};
325
326} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
std::list< xact_t * > xacts_list
Definition journal.h:60
T & downcast(U &object)
Definition utils.h:468
accounts_map accounts
Definition account.h:70
iterator_facade_base(node_base p)
Definition iterators.h:71
iterator_facade_base(const iterator_facade_base &i)
Definition iterators.h:64
friend class boost::iterator_core_access
Definition iterators.h:76
xact_posts_iterator(xact_t &xact)
Definition iterators.h:101
xact_posts_iterator(const xact_posts_iterator &i)
Definition iterators.h:106
void reset(xact_t &xact)
Definition iterators.h:117
void reset(journal_t &journal)
xacts_iterator(xacts_list::iterator beg, xacts_list::iterator end)
Definition iterators.h:151
xacts_iterator(const xacts_iterator &i)
Definition iterators.h:156
xacts_iterator(journal_t &journal)
Definition iterators.h:147
xacts_list::iterator xacts_i
Definition iterators.h:139
xacts_list::iterator xacts_end
Definition iterators.h:140
void reset(xacts_list::iterator beg, xacts_list::iterator end)
Definition iterators.h:169
journal_posts_iterator(const journal_posts_iterator &i)
Definition iterators.h:193
void reset(journal_t &journal)
journal_posts_iterator(journal_t &journal)
Definition iterators.h:189
posts_commodities_iterator(journal_t &journal)
Definition iterators.h:223
posts_commodities_iterator(const posts_commodities_iterator &i)
Definition iterators.h:227
journal_posts_iterator journal_posts
Definition iterators.h:213
void reset(journal_t &journal)
basic_accounts_iterator(const basic_accounts_iterator &i)
Definition iterators.h:259
basic_accounts_iterator(account_t &account)
Definition iterators.h:254
sorted_accounts_iterator(account_t &account, const expr_t &_sort_cmp, report_t &_report, bool _flatten_all)
Definition iterators.h:293
sorted_accounts_iterator(const sorted_accounts_iterator &i)
Definition iterators.h:304
posts_list posts
Definition xact.h:59