Loading...
Searching...
No Matches
annotate.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
46#pragma once
47
48#include "expr.h"
49
50namespace ledger {
51
53 public equality_comparable<annotation_t>
54{
55#define ANNOTATION_PRICE_CALCULATED 0x01
56#define ANNOTATION_PRICE_FIXATED 0x02
57#define ANNOTATION_PRICE_NOT_PER_UNIT 0x04
58#define ANNOTATION_DATE_CALCULATED 0x08
59#define ANNOTATION_TAG_CALCULATED 0x10
60#define ANNOTATION_VALUE_EXPR_CALCULATED 0x20
61
62 optional<amount_t> price;
63 optional<date_t> date;
64 optional<string> tag;
65 optional<expr_t> value_expr;
66
67 explicit annotation_t(const optional<amount_t>& _price = none,
68 const optional<date_t>& _date = none,
69 const optional<string>& _tag = none,
70 const optional<expr_t>& _value_expr = none)
71 : supports_flags<>(), price(_price), date(_date), tag(_tag),
72 value_expr(_value_expr) {
74 "optional<amount_t> + date_t + string + expr_t");
75 }
77 : supports_flags<>(other.flags()),
78 price(other.price), date(other.date), tag(other.tag),
80 {
81 TRACE_CTOR(annotation_t, "copy");
82 }
86
87 operator bool() const {
88 return price || date || tag || value_expr;
89 }
90
91 bool operator<(const annotation_t& rhs) const;
92 bool operator==(const annotation_t& rhs) const {
93 return (price == rhs.price &&
94 date == rhs.date &&
95 tag == rhs.tag &&
96 (value_expr && rhs.value_expr ?
97 value_expr->text() == rhs.value_expr->text() :
98 value_expr == rhs.value_expr));
99 }
100
101 void parse(std::istream& in);
102 void print(std::ostream& out, bool keep_base = false,
103 bool no_computed_annotations = false) const;
104
105 bool valid() const {
106 assert(*this);
107 return true;
108 }
109};
110
111void put_annotation(property_tree::ptree& pt, const annotation_t& details);
112
114{
119
120 explicit keep_details_t(bool _keep_price = false,
121 bool _keep_date = false,
122 bool _keep_tag = false,
123 bool _only_actuals = false)
124 : keep_price(_keep_price),
125 keep_date(_keep_date),
126 keep_tag(_keep_tag),
127 only_actuals(_only_actuals)
128 {
129 TRACE_CTOR(keep_details_t, "bool, bool, bool, bool");
130 }
132 : keep_price(other.keep_price), keep_date(other.keep_date),
134 TRACE_CTOR(keep_details_t, "copy");
135 }
139
140 bool keep_all() const {
141 return keep_price && keep_date && keep_tag && ! only_actuals;
142 }
143 bool keep_all(const commodity_t& comm) const;
144
145 bool keep_any() const {
146 return keep_price || keep_date || keep_tag;
147 }
148 bool keep_any(const commodity_t& comm) const;
149};
150
151inline std::ostream& operator<<(std::ostream& out,
152 const annotation_t& details) {
153 details.print(out);
154 return out;
155}
156
158 : public commodity_t,
159 public equality_comparable<annotated_commodity_t,
160 equality_comparable2<annotated_commodity_t, commodity_t,
161 noncopyable> >
162{
163protected:
164 friend class commodity_pool_t;
165
167
169 const annotation_t& _details)
170 : commodity_t(_ptr->parent_, _ptr->base), ptr(_ptr), details(_details) {
171 annotated = true;
173 TRACE_CTOR(annotated_commodity_t, "commodity_t *, annotation_t");
174 }
175
176public:
178
182
183 virtual bool operator==(const commodity_t& comm) const;
184 virtual bool operator==(const annotated_commodity_t& comm) const {
185 return *this == static_cast<const commodity_t&>(comm);
186 }
187
189 return *ptr;
190 }
191 virtual const commodity_t& referent() const {
192 return *ptr;
193 }
194
195 virtual optional<expr_t> value_expr() const {
197 return details.value_expr;
199 }
200
201 optional<price_point_t>
202 virtual find_price(const commodity_t * commodity = NULL,
203 const datetime_t& moment = datetime_t(),
204 const datetime_t& oldest = datetime_t()) const;
205
206 virtual commodity_t& strip_annotations(const keep_details_t& what_to_keep);
207
208 virtual void print(std::ostream& out, bool elide_quotes = false,
209 bool print_annotations = false) const {
210 if (print_annotations) {
211 std::ostringstream buf;
212 commodity_t::print(buf, elide_quotes);
214 out << buf.str();
215 } else {
216 commodity_t::print(out, elide_quotes);
217 }
218 }
219
220 virtual void write_annotations(std::ostream& out,
221 bool no_computed_annotations = false) const;
222};
223
228inline const annotated_commodity_t&
231}
232
233} // 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
gregorian::date date
Definition utils.h:64
annotated_commodity_t & as_annotated_commodity(commodity_t &commodity)
Definition annotate.h:225
void put_annotation(property_tree::ptree &pt, const annotation_t &details)
std::ostream & operator<<(std::ostream &out, const account_t &account)
T & downcast(U &object)
Definition utils.h:468
boost::posix_time::ptime datetime_t
Definition times.h:53
optional< amount_t > price
Definition annotate.h:62
optional< date_t > date
Definition annotate.h:63
bool operator==(const annotation_t &rhs) const
Definition annotate.h:92
void parse(std::istream &in)
optional< expr_t > value_expr
Definition annotate.h:65
void print(std::ostream &out, bool keep_base=false, bool no_computed_annotations=false) const
bool operator<(const annotation_t &rhs) const
annotation_t(const optional< amount_t > &_price=none, const optional< date_t > &_date=none, const optional< string > &_tag=none, const optional< expr_t > &_value_expr=none)
Definition annotate.h:67
annotation_t(const annotation_t &other)
Definition annotate.h:76
bool valid() const
Definition annotate.h:105
optional< string > tag
Definition annotate.h:64
bool keep_any() const
Definition annotate.h:145
bool keep_all(const commodity_t &comm) const
keep_details_t(bool _keep_price=false, bool _keep_date=false, bool _keep_tag=false, bool _only_actuals=false)
Definition annotate.h:120
keep_details_t(const keep_details_t &other)
Definition annotate.h:131
bool keep_all() const
Definition annotate.h:140
bool keep_any(const commodity_t &comm) const
virtual optional< price_point_t > find_price(const commodity_t *commodity=NULL, const datetime_t &moment=datetime_t(), const datetime_t &oldest=datetime_t()) const
virtual void print(std::ostream &out, bool elide_quotes=false, bool print_annotations=false) const
Definition annotate.h:208
virtual bool operator==(const annotated_commodity_t &comm) const
Definition annotate.h:184
virtual void write_annotations(std::ostream &out, bool no_computed_annotations=false) const
annotated_commodity_t(commodity_t *_ptr, const annotation_t &_details)
Definition annotate.h:168
virtual commodity_t & referent()
Definition annotate.h:188
virtual bool operator==(const commodity_t &comm) const
virtual commodity_t & strip_annotations(const keep_details_t &what_to_keep)
virtual const commodity_t & referent() const
Definition annotate.h:191
virtual optional< expr_t > value_expr() const
Definition annotate.h:195
shared_ptr< base_t > base
Definition commodity.h:130
virtual void print(std::ostream &out, bool elide_quotes=false, bool print_annotations=false) const
virtual optional< expr_t > value_expr() const
Definition commodity.h:233
optional< string > qualified_symbol
Definition commodity.h:133
commodity_pool_t * parent_
Definition commodity.h:132
flags_t flags() const
Definition flags.h:74