Line data Source code
1 : /**********************************************************************
2 : * Account.hpp
3 : * *
4 : * This program is free software; you can redistribute it and/or *
5 : * modify it under the terms of the GNU General Public License as *
6 : * published by the Free Software Foundation; either version 2 of *
7 : * the License, or (at your option) any later version. *
8 : * *
9 : * This program is distributed in the hope that it will be useful, *
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 : * GNU General Public License for more details. *
13 : * *
14 : * You should have received a copy of the GNU General Public License *
15 : * along with this program; if not, contact: *
16 : * *
17 : * Free Software Foundation Voice: +1-617-542-5942 *
18 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 : * Boston, MA 02110-1301, USA gnu@gnu.org *
20 : * *
21 : *********************************************************************/
22 :
23 : /** @addtogroup Engine
24 : @{ */
25 : /** @addtogroup Account
26 :
27 : @{ */
28 : /** @file Account.hpp
29 : * @brief Account public routines (C++ api)
30 : */
31 :
32 : #ifndef GNC_ACCOUNT_HPP
33 : #define GNC_ACCOUNT_HPP
34 :
35 : #include <vector>
36 : #include <functional>
37 : #include <algorithm>
38 : #include <optional>
39 :
40 : #include <Account.h>
41 : #include <SplitP.hpp>
42 : #include <TransactionP.hpp>
43 :
44 : using SplitsVec = std::vector<Split*>;
45 : using AccountVec = std::vector<Account*>;
46 :
47 : const SplitsVec& xaccAccountGetSplits (const Account*);
48 :
49 : void gnc_account_foreach_descendant (const Account *, std::function<void(Account*)> func);
50 :
51 :
52 : static inline SplitsVec::const_iterator
53 1260 : splits_start (const SplitsVec& splits, std::optional<time64> start_date)
54 : {
55 1260 : if (!start_date) return splits.begin();
56 0 : return std::lower_bound (splits.begin(), splits.end(), *start_date,
57 0 : [](auto s, time64 t){ return s->parent->date_posted < t; });
58 : }
59 :
60 : static inline SplitsVec::const_iterator
61 1260 : splits_end (const SplitsVec& splits, std::optional<time64> end_date)
62 : {
63 1260 : if (!end_date) return splits.end();
64 0 : return std::upper_bound (splits.begin(), splits.end(), *end_date,
65 0 : [](time64 t, auto s){ return t < s->parent->date_posted; });
66 : }
67 :
68 : template <typename Fn>
69 1260 : void gnc_account_foreach_split_between_dates (const Account* account,
70 : std::optional<time64> start_date,
71 : std::optional<time64> end_date,
72 : bool include_descendants, Fn&& fn)
73 : {
74 1260 : g_return_if_fail (GNC_IS_ACCOUNT (account));
75 3780 : auto scan_account = [&](const Account* acc)
76 : {
77 1260 : const auto& splits = xaccAccountGetSplits (acc);
78 1260 : std::for_each (splits_start (splits, start_date), splits_end (splits, end_date),
79 : fn);
80 : };
81 :
82 1260 : scan_account (account);
83 1260 : if (include_descendants)
84 0 : gnc_account_foreach_descendant (account, scan_account);
85 : }
86 :
87 : void gnc_account_foreach_split (const Account*, std::function<void(Split*)>);
88 :
89 : void gnc_account_foreach_split_until_date (const Account *acc, time64 end_date,
90 : std::function<void(Split*)> f);
91 :
92 : /** scans account split list (in forward or reverse order) until
93 : * predicate split->bool returns true. Maybe return the split.
94 : *
95 : * @param acc The account to which the split should be added.
96 : *
97 : * @param predicate A split->bool predicate.
98 : *
99 : * @param reverse To scan in reverse order
100 : *
101 : * @result Split* or nullptr if not found */
102 : Split* gnc_account_find_split (const Account*, std::function<bool(const Split*)>, bool);
103 :
104 : std::vector<const Account*> gnc_account_get_all_parents (const Account *account);
105 :
106 : #endif /* GNC_COMMODITY_HPP */
107 : /** @} */
108 : /** @} */
|