Loading...
Searching...
No Matches
account.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 "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#if DOCUMENT_MODEL
77 mutable void * data;
78#endif
79
80 account_t(account_t * _parent = NULL,
81 const string& _name = "",
82 const optional<string>& _note = none)
83 : supports_flags<>(), scope_t(), parent(_parent),
84 name(_name), note(_note),
85 depth(static_cast<unsigned short>(parent ? parent->depth + 1 : 0))
86#if DOCUMENT_MODEL
87 , data(NULL)
88#endif
89 {
90 TRACE_CTOR(account_t, "account_t *, const string&, const string&");
91 }
92 account_t(const account_t& other)
93 : supports_flags<>(other.flags()), scope_t(),
94 parent(other.parent),
95 name(other.name),
96 note(other.note),
97 depth(other.depth),
98 accounts(other.accounts)
99#if DOCUMENT_MODEL
100 , data(NULL)
101#endif
102 {
103 TRACE_CTOR(account_t, "copy");
104 }
105 virtual ~account_t();
106
107 virtual string description() {
108 return string(_("account ")) + fullname();
109 }
110
111 operator string() const {
112 return fullname();
113 }
114 string fullname() const;
115 string partial_name(bool flat = false) const;
116
117 void add_account(account_t * acct) {
118 accounts.insert(accounts_map::value_type(acct->name, acct));
119 }
121 accounts_map::size_type n = accounts.erase(acct->name);
122 return n > 0;
123 }
124
125 account_t * find_account(const string& name, bool auto_create = true);
126 account_t * find_account_re(const string& regexp);
127
128 typedef transform_iterator<function<account_t *(accounts_map::value_type&)>,
129 accounts_map::iterator>
131
133 return make_transform_iterator
134 (accounts.begin(), boost::bind(&accounts_map::value_type::second, _1));
135 }
137 return make_transform_iterator
138 (accounts.end(), boost::bind(&accounts_map::value_type::second, _1));
139 }
140
141 void add_post(post_t * post);
142 void add_deferred_post(const string& uuid, post_t * post);
144 bool remove_post(post_t * post);
145
146 posts_list::iterator posts_begin() {
147 return posts.begin();
148 }
149 posts_list::iterator posts_end() {
150 return posts.end();
151 }
152
154 const string& name);
155
156 bool valid() const;
157
158 friend class journal_t;
159
160 struct xdata_t : public supports_flags<>
161 {
162#define ACCOUNT_EXT_SORT_CALC 0x01
163#define ACCOUNT_EXT_HAS_NON_VIRTUALS 0x02
164#define ACCOUNT_EXT_HAS_UNB_VIRTUALS 0x04
165#define ACCOUNT_EXT_AUTO_VIRTUALIZE 0x08
166#define ACCOUNT_EXT_VISITED 0x10
167#define ACCOUNT_EXT_MATCHING 0x20
168#define ACCOUNT_EXT_TO_DISPLAY 0x40
169#define ACCOUNT_EXT_DISPLAYED 0x80
170
172 {
177
178 std::size_t posts_count;
184
189
193
194 std::set<path> filenames;
195 std::set<string> accounts_referenced;
196 std::set<string> payees_referenced;
197
198 optional<posts_list::const_iterator> last_post;
199 optional<posts_list::const_iterator> last_reported_post;
200
215 // A copy copies nothing
233
235
236 void update(post_t& post, bool gather_all = false);
237 };
238
242
243 std::list<sort_value_t> sort_values;
244
245 xdata_t() : supports_flags<>()
246 {
248 }
249 xdata_t(const xdata_t& other)
250 : supports_flags<>(other.flags()),
254 {
256 }
257 ~xdata_t() throw() {
259 }
260 };
261
262 // This variable holds optional "extended data" which is usually produced
263 // only during reporting, and only for the posting set being reported.
264 // It's a memory-saving measure to delay allocation until the last possible
265 // moment.
266 mutable optional<xdata_t> xdata_;
267
268 bool has_xdata() const {
269 return static_cast<bool>(xdata_);
270 }
273 if (! xdata_)
274 xdata_ = xdata_t();
275 return *xdata_;
276 }
277 const xdata_t& xdata() const {
278 assert(xdata_);
279 return *xdata_;
280 }
281
282 value_t amount(const optional<bool> real_only = false, const optional<expr_t&>& expr = none) const;
283 value_t total(const optional<expr_t&>& expr = none) const;
284
285 const xdata_t::details_t& self_details(bool gather_all = true) const;
286 const xdata_t::details_t& family_details(bool gather_all = true) const;
287
288 bool has_xflags(xdata_t::flags_t flags) const {
289 return xdata_ && xdata_->has_flags(flags);
290 }
292 std::size_t children_with_flags(xdata_t::flags_t flags) const;
293};
294
295std::ostream& operator<<(std::ostream& out, const account_t& account);
296
297void put_account(property_tree::ptree& pt, const account_t& acct,
298 function<bool(const account_t&)> pred);
299
300//simple struct added to allow std::map to compare accounts in the accounts report
302 bool operator() (const account_t& lhs, const account_t& rhs) const {
303 return (lhs.fullname().compare(rhs.fullname()) < 0);
304 }
305};
306
307} // 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:268
string _fullname
Definition account.h:75
posts_list::iterator posts_end()
Definition account.h:149
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:136
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:80
bool remove_post(post_t *post)
account_t * find_account_re(const string &regexp)
transform_iterator< function< account_t *(accounts_map::value_type &)>, accounts_map::iterator accounts_map_seconds_iterator)
Definition account.h:130
bool has_xflags(xdata_t::flags_t flags) const
Definition account.h:288
unsigned short depth
Definition account.h:69
accounts_map_seconds_iterator accounts_begin()
Definition account.h:132
bool children_with_xdata() const
virtual string description()
Definition account.h:107
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:277
virtual ~account_t()
posts_list posts
Definition account.h:71
void add_post(post_t *post)
optional< xdata_t > xdata_
Definition account.h:266
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:92
void add_account(account_t *acct)
Definition account.h:117
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:120
value_t amount(const optional< bool > real_only=false, const optional< expr_t & > &expr=none) const
account_t * parent
Definition account.h:66
xdata_t & xdata()
Definition account.h:272
posts_list::iterator posts_begin()
Definition account.h:146
string fullname() const
xdata_t(const xdata_t &other)
Definition account.h:249
std::list< sort_value_t > sort_values
Definition account.h:243
void update(post_t &post, bool gather_all=false)
std::set< string > accounts_referenced
Definition account.h:195
optional< posts_list::const_iterator > last_reported_post
Definition account.h:199
optional< posts_list::const_iterator > last_post
Definition account.h:198
std::set< string > payees_referenced
Definition account.h:196
details_t & operator+=(const details_t &other)
bool operator()(const account_t &lhs, const account_t &rhs) const
Definition account.h:302
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:83