Loading...
Searching...
No Matches
item.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
49{
51 std::istream::pos_type beg_pos;
52 std::size_t beg_line;
53 std::istream::pos_type end_pos;
54 std::size_t end_line;
55 std::size_t sequence;
56
58 : beg_pos(0), beg_line(0), end_pos(0), end_line(0), sequence(0) {
60 }
61 position_t(const position_t& pos) {
62 *this = pos;
63 TRACE_CTOR(position_t, "copy");
64 }
68
70 if (this != &pos) {
71 pathname = pos.pathname;
72 beg_pos = pos.beg_pos;
73 beg_line = pos.beg_line;
74 end_pos = pos.end_pos;
75 end_line = pos.end_line;
76 sequence = pos.sequence;
77 }
78 return *this;
79 }
80};
81
82class item_t : public flags::supports_flags<uint_least16_t>, public scope_t
83{
84public:
85#define ITEM_NORMAL 0x00 // no flags at all, a basic posting
86#define ITEM_GENERATED 0x01 // posting was not found in a journal
87#define ITEM_TEMP 0x02 // posting is a managed temporary
88#define ITEM_NOTE_ON_NEXT_LINE 0x04 // did we see a note on the next line?
89#define ITEM_INFERRED 0x08 // bucketed item
90
92
93 typedef std::pair<optional<value_t>, bool> tag_data_t;
94 typedef std::map<string, tag_data_t,
95 std::function<bool(string, string)> > string_map;
96
103
105 : supports_flags<uint_least16_t>(_flags), _state(UNCLEARED), note(_note)
106 {
107 TRACE_CTOR(item_t, "flags_t, const string&");
108 }
109 item_t(const item_t& item) : supports_flags<uint_least16_t>(), scope_t()
110 {
112 TRACE_CTOR(item_t, "copy");
113 }
114 virtual ~item_t() {
116 }
117
118 virtual void copy_details(const item_t& item)
119 {
120 set_flags(item.flags());
121 set_state(item.state());
122
123 _date = item._date;
124 _date_aux = item._date_aux;
125 note = item.note;
126 pos = item.pos;
127 metadata = item.metadata;
128 }
129
130 virtual bool operator==(const item_t& xact) {
131 return this == &xact;
132 }
133 virtual bool operator!=(const item_t& xact) {
134 return ! (*this == xact);
135 }
136
137 string id() const {
138 if (optional<value_t> ref = get_tag(_("UUID"))) {
139 return ref->to_string();
140 } else {
141 std::ostringstream buf;
142 buf << seq();
143 return buf.str();
144 }
145 }
146 std::size_t seq() const {
147 return pos ? pos->sequence : 0L;
148 }
149
150 virtual bool has_tag(const string& tag,
151 bool inherit = true) const;
152 virtual bool has_tag(const mask_t& tag_mask,
154 bool inherit = true) const;
155
156 virtual optional<value_t> get_tag(const string& tag,
157 bool inherit = true) const;
160 bool inherit = true) const;
161
162 virtual string_map::iterator
163 set_tag(const string& tag,
164 const optional<value_t>& value = none,
165 const bool overwrite_existing = true);
166
167 virtual void parse_tags(const char * p,
168 scope_t& scope,
169 bool overwrite_existing = true);
170 virtual void append_note(const char * p,
171 scope_t& scope,
172 bool overwrite_existing = true);
173
174 static bool use_aux_date;
175
176 virtual bool has_date() const {
177 return static_cast<bool>(_date);
178 }
179
180 virtual date_t date() const {
181 assert(_date);
182 if (use_aux_date)
184 return *aux;
185 return *_date;
186 }
187 virtual date_t primary_date() const {
188 assert(_date);
189 return *_date;
190 }
191 virtual optional<date_t> aux_date() const {
192 return _date_aux;
193 }
194
198 virtual state_t state() const {
199 return _state;
200 }
201
202 virtual void define(const symbol_t::kind_t, const string&,
205 const string& name);
206
207 bool valid() const;
208};
209
211void print_item(std::ostream& out, const item_t& item,
212 const string& prefix = "");
213string item_context(const item_t& item, const string& desc);
214void put_metadata(property_tree::ptree& pt, const item_t::string_map& metadata);
215
216} // 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
#define ITEM_NORMAL
Definition item.h:85
string item_context(const item_t &item, const string &desc)
boost::gregorian::date date_t
Definition times.h:60
void print_item(std::ostream &out, const item_t &item, const string &prefix="")
value_t get_comment(item_t &item)
boost::filesystem::path path
Definition utils.h:68
std::string string
Definition utils.h:59
void put_metadata(property_tree::ptree &pt, const item_t::string_map &metadata)
T & downcast(U &object)
Definition utils.h:468
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
position_t & operator=(const position_t &pos)
Definition item.h:69
position_t(const position_t &pos)
Definition item.h:61
std::size_t beg_line
Definition item.h:52
std::istream::pos_type beg_pos
Definition item.h:51
std::size_t sequence
Definition item.h:55
std::size_t end_line
Definition item.h:54
std::istream::pos_type end_pos
Definition item.h:53
virtual bool operator==(const item_t &xact)
Definition item.h:130
virtual string_map::iterator set_tag(const string &tag, const optional< value_t > &value=none, const bool overwrite_existing=true)
optional< string_map > metadata
Definition item.h:102
virtual void copy_details(const item_t &item)
Definition item.h:118
virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, const string &name)
virtual void append_note(const char *p, scope_t &scope, bool overwrite_existing=true)
void set_state(state_t new_state)
Definition item.h:195
virtual date_t primary_date() const
Definition item.h:187
item_t(const item_t &item)
Definition item.h:109
virtual void parse_tags(const char *p, scope_t &scope, bool overwrite_existing=true)
virtual optional< date_t > aux_date() const
Definition item.h:191
optional< date_t > _date_aux
Definition item.h:99
string id() const
Definition item.h:137
virtual optional< value_t > get_tag(const mask_t &tag_mask, const optional< mask_t > &value_mask=none, bool inherit=true) const
optional< date_t > _date
Definition item.h:98
virtual state_t state() const
Definition item.h:198
item_t(flags_t _flags=0x00, const optional< string > &_note=none)
Definition item.h:104
std::pair< optional< value_t >, bool > tag_data_t
Definition item.h:93
static bool use_aux_date
Definition item.h:174
virtual optional< value_t > get_tag(const string &tag, bool inherit=true) const
virtual void define(const symbol_t::kind_t, const string &, expr_t::ptr_op_t)
virtual bool has_date() const
Definition item.h:176
bool valid() const
std::map< string, tag_data_t, std::function< bool(string, string)> > string_map
Definition item.h:95
state_t _state
Definition item.h:97
virtual bool has_tag(const string &tag, bool inherit=true) const
virtual bool operator!=(const item_t &xact)
Definition item.h:133
optional< position_t > pos
Definition item.h:101
virtual ~item_t()
Definition item.h:114
virtual bool has_tag(const mask_t &tag_mask, const optional< mask_t > &value_mask=none, bool inherit=true) const
optional< string > note
Definition item.h:100
virtual date_t date() const
Definition item.h:180
std::size_t seq() const
Definition item.h:146
Dynamic type representing various numeric types.
Definition value.h:83