Loading...
Searching...
No Matches
account.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2025, 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
35
42#pragma once
43
44#include "scope.h"
45
46namespace ledger {
47
48using namespace boost::placeholders;
49
50class account_t;
51class xact_t;
52class post_t;
53
54typedef std::list<post_t *> posts_list;
55typedef std::map<string, account_t *> accounts_map;
56typedef std::map<string, posts_list> deferred_posts_map_t;
57
58class account_t : public flags::supports_flags<>, public scope_t
59{
60#define ACCOUNT_NORMAL 0x00 // no flags at all, a basic account
61#define ACCOUNT_KNOWN 0x01
62#define ACCOUNT_TEMP 0x02 // account is a temporary object
63#define ACCOUNT_GENERATED 0x04 // account never actually existed
64
65public:
67 string name;
68 optional<string> note;
69 unsigned short depth;
72 optional<deferred_posts_map_t> deferred_posts;
73 optional<expr_t> value_expr;
74
75 mutable string _fullname;
76
77 account_t(account_t * _parent = NULL,
78 const string& _name = "",
79 const optional<string>& _note = none)
80 : supports_flags<>(), scope_t(), parent(_parent),
81 name(_name), note(_note),
82 depth(static_cast<unsigned short>(parent ? parent->depth + 1 : 0))
83 {
84 TRACE_CTOR(account_t, "account_t *, const string&, const string&");
85 }
86 account_t(const account_t& other)
87 : supports_flags<>(other.flags()), scope_t(),
88 parent(other.parent),
89 name(other.name),
90 note(other.note),
91 depth(other.depth),
92 accounts(other.accounts)
93 {
94 TRACE_CTOR(account_t, "copy");
95 }
96 virtual ~account_t();
97
98 virtual string description() {
99 return string(_("account ")) + fullname();
100 }
101
102 operator string() const {
103 return fullname();
104 }
105 string fullname() const;
106 string partial_name(bool flat = false) const;
107
108 void add_account(account_t * acct) {
109 accounts.insert(accounts_map::value_type(acct->name, acct));
110 }
112 accounts_map::size_type n = accounts.erase(acct->name);
113 return n > 0;
114 }
115
116 account_t * find_account(const string& name, bool auto_create = true);
117 account_t * find_account_re(const string& regexp);
118
119 typedef transform_iterator<function<account_t *(accounts_map::value_type&)>,
120 accounts_map::iterator>
122
124 return make_transform_iterator
125 (accounts.begin(), boost::bind(&accounts_map::value_type::second, _1));
126 }
128 return make_transform_iterator
129 (accounts.end(), boost::bind(&accounts_map::value_type::second, _1));
130 }
131
132 void add_post(post_t * post);
133 void add_deferred_post(const string& uuid, post_t * post);
135 bool remove_post(post_t * post);
136
137 posts_list::iterator posts_begin() {
138 return posts.begin();
139 }
140 posts_list::iterator posts_end() {
141 return posts.end();
142 }
143
145 const string& name);
146
147 bool valid() const;
148
149 friend class journal_t;
150
151 struct xdata_t : public supports_flags<>
152 {
153#define ACCOUNT_EXT_SORT_CALC 0x01
154#define ACCOUNT_EXT_HAS_NON_VIRTUALS 0x02
155#define ACCOUNT_EXT_HAS_UNB_VIRTUALS 0x04
156#define ACCOUNT_EXT_AUTO_VIRTUALIZE 0x08
157#define ACCOUNT_EXT_VISITED 0x10
158#define ACCOUNT_EXT_MATCHING 0x20
159#define ACCOUNT_EXT_TO_DISPLAY 0x40
160#define ACCOUNT_EXT_DISPLAYED 0x80
161
163 {
168
169 std::size_t posts_count;
175
180
184
185 std::set<path> filenames;
186 std::set<string> accounts_referenced;
187 std::set<string> payees_referenced;
188
189 optional<posts_list::const_iterator> last_post;
190 optional<posts_list::const_iterator> last_reported_post;
191
206 // A copy copies nothing
224
226
227 void update(post_t& post, bool gather_all = false);
228 };
229
233
234 std::list<sort_value_t> sort_values;
235
240 xdata_t(const xdata_t& other)
241 : supports_flags<>(other.flags()),
245 {
247 }
248 ~xdata_t() throw() {
250 }
251 };
252
253 // This variable holds optional "extended data" which is usually produced
254 // only during reporting, and only for the posting set being reported.
255 // It's a memory-saving measure to delay allocation until the last possible
256 // moment.
257 mutable optional<xdata_t> xdata_;
258
259 bool has_xdata() const {
260 return static_cast<bool>(xdata_);
261 }
264 if (! xdata_)
265 xdata_ = xdata_t();
266 return *xdata_;
267 }
268 const xdata_t& xdata() const {
269 assert(xdata_);
270 return *xdata_;
271 }
272
273 value_t amount(const optional<bool> real_only = false, const optional<expr_t&>& expr = none) const;
274 value_t total(const optional<expr_t&>& expr = none) const;
275
276 const xdata_t::details_t& self_details(bool gather_all = true) const;
277 const xdata_t::details_t& family_details(bool gather_all = true) const;
278
279 bool has_xflags(xdata_t::flags_t flags) const {
280 return xdata_ && xdata_->has_flags(flags);
281 }
283 std::size_t children_with_flags(xdata_t::flags_t flags) const;
284};
285
286std::ostream& operator<<(std::ostream& out, const account_t& account);
287
288void put_account(property_tree::ptree& pt, const account_t& acct,
289 function<bool(const account_t&)> pred);
290
291//simple struct added to allow std::map to compare accounts in the accounts report
293 bool operator() (const account_t& lhs, const account_t& rhs) const {
294 return (lhs.fullname().compare(rhs.fullname()) < 0);
295 }
296};
297
298} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define assert(x)
Definition utils.h:92
void put_account(property_tree::ptree &pt, const account_t &acct, function< bool(const account_t &)> pred)
boost::gregorian::date date_t
Definition times.h:60
std::map< string, posts_list > deferred_posts_map_t
Definition account.h:56
std::string string
Definition utils.h:59
std::ostream & operator<<(std::ostream &out, const account_t &account)
std::map< string, account_t * > accounts_map
Definition account.h:55
std::list< post_t * > posts_list
Definition account.h:54
boost::posix_time::ptime datetime_t
Definition times.h:53
bool has_xdata() const
Definition account.h:259
string _fullname
Definition account.h:75
posts_list::iterator posts_end()
Definition account.h:140
void add_deferred_post(const string &uuid, post_t *post)
optional< expr_t > value_expr
Definition account.h:73
accounts_map_seconds_iterator accounts_end()
Definition account.h:127
const xdata_t::details_t & self_details(bool gather_all=true) const
account_t(account_t *_parent=NULL, const string &_name="", const optional< string > &_note=none)
Definition account.h:77
bool remove_post(post_t *post)
account_t * find_account_re(const string &regexp)
friend class journal_t
Definition account.h:149
bool has_xflags(xdata_t::flags_t flags) const
Definition account.h:279
unsigned short depth
Definition account.h:69
accounts_map_seconds_iterator accounts_begin()
Definition account.h:123
bool children_with_xdata() const
virtual string description()
Definition account.h:98
value_t total(const optional< expr_t & > &expr=none) const
optional< string > note
Definition account.h:68
std::size_t children_with_flags(xdata_t::flags_t flags) const
account_t * find_account(const string &name, bool auto_create=true)
const xdata_t & xdata() const
Definition account.h:268
virtual ~account_t()
posts_list posts
Definition account.h:71
void add_post(post_t *post)
optional< xdata_t > xdata_
Definition account.h:257
optional< deferred_posts_map_t > deferred_posts
Definition account.h:72
bool valid() const
void apply_deferred_posts()
account_t(const account_t &other)
Definition account.h:86
void add_account(account_t *acct)
Definition account.h:108
virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, const string &name)
string partial_name(bool flat=false) const
const xdata_t::details_t & family_details(bool gather_all=true) const
accounts_map accounts
Definition account.h:70
bool remove_account(account_t *acct)
Definition account.h:111
value_t amount(const optional< bool > real_only=false, const optional< expr_t & > &expr=none) const
transform_iterator< function< account_t *(accounts_map::value_type &)>, accounts_map::iterator > accounts_map_seconds_iterator
Definition account.h:121
account_t * parent
Definition account.h:66
xdata_t & xdata()
Definition account.h:263
posts_list::iterator posts_begin()
Definition account.h:137
string fullname() const
xdata_t(const xdata_t &other)
Definition account.h:240
std::list< sort_value_t > sort_values
Definition account.h:234
void update(post_t &post, bool gather_all=false)
std::set< string > accounts_referenced
Definition account.h:186
optional< posts_list::const_iterator > last_reported_post
Definition account.h:190
optional< posts_list::const_iterator > last_post
Definition account.h:189
std::set< string > payees_referenced
Definition account.h:187
details_t & operator+=(const details_t &other)
bool operator()(const account_t &lhs, const account_t &rhs) const
Definition account.h:293
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
flags_t flags() const
Definition flags.h:74
Dynamic type representing various numeric types.
Definition value.h:86