summaryrefslogtreecommitdiff
path: root/src/annotate.h
blob: 1e62e2fae0df43d17de8b24abbbe9cf193597e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2003-2012, John Wiegley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @addtogroup math
 */

/**
 * @file   annotate.h
 * @author John Wiegley
 *
 * @ingroup math
 *
 * @brief  Types for annotating commodities
 *
 * Long.
 */
#ifndef _ANNOTATE_H
#define _ANNOTATE_H

#include "expr.h"

namespace ledger {

struct annotation_t : public supports_flags<>,
                      public equality_comparable<annotation_t>
{
#define ANNOTATION_PRICE_CALCULATED      0x01
#define ANNOTATION_PRICE_FIXATED         0x02
#define ANNOTATION_PRICE_NOT_PER_UNIT    0x04
#define ANNOTATION_DATE_CALCULATED       0x08
#define ANNOTATION_TAG_CALCULATED        0x10
#define ANNOTATION_VALUE_EXPR_CALCULATED 0x20

  optional<amount_t> price;
  optional<date_t>   date;
  optional<string>   tag;
  optional<expr_t>   value_expr;

  explicit 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)
    : supports_flags<>(), price(_price), date(_date), tag(_tag),
      value_expr(_value_expr) {
    TRACE_CTOR(annotation_t,
               "optional<amount_t> + date_t + string + expr_t");
  }
  annotation_t(const annotation_t& other)
    : supports_flags<>(other.flags()),
      price(other.price), date(other.date), tag(other.tag),
      value_expr(other.value_expr)
  {
    TRACE_CTOR(annotation_t, "copy");
  }
  ~annotation_t() {
    TRACE_DTOR(annotation_t);
  }

  operator bool() const {
    return price || date || tag || value_expr;
  }

  bool operator<(const annotation_t& rhs) const;
  bool operator==(const annotation_t& rhs) const {
    return (price == rhs.price &&
            date  == rhs.date  &&
            tag   == rhs.tag   &&
            (value_expr && rhs.value_expr ?
             value_expr->text() == rhs.value_expr->text() :
             value_expr == rhs.value_expr));
  }

  void parse(std::istream& in);
  void print(std::ostream& out, bool keep_base = false,
             bool no_computed_annotations = false) const;

  bool valid() const {
    assert(*this);
    return true;
  }

#if defined(HAVE_BOOST_SERIALIZATION)
private:
  /** Serialization. */

  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive& ar, const unsigned int /* version */) {
    ar & boost::serialization::base_object<supports_flags<> >(*this);
    ar & price;
    ar & date;
    ar & tag;
  }
#endif // HAVE_BOOST_SERIALIZATION
};

void put_annotation(property_tree::ptree& pt, const annotation_t& details);

struct keep_details_t
{
  bool keep_price;
  bool keep_date;
  bool keep_tag;
  bool only_actuals;

  explicit keep_details_t(bool _keep_price   = false,
                          bool _keep_date    = false,
                          bool _keep_tag     = false,
                          bool _only_actuals = false)
    : keep_price(_keep_price),
      keep_date(_keep_date),
      keep_tag(_keep_tag),
      only_actuals(_only_actuals)
  {
    TRACE_CTOR(keep_details_t, "bool, bool, bool, bool");
  }
  keep_details_t(const keep_details_t& other)
    : keep_price(other.keep_price), keep_date(other.keep_date),
      keep_tag(other.keep_tag), only_actuals(other.only_actuals) {
    TRACE_CTOR(keep_details_t, "copy");
  }
  ~keep_details_t() throw() {
    TRACE_DTOR(keep_details_t);
  }

  bool keep_all() const {
    return keep_price && keep_date && keep_tag && ! only_actuals;
  }
  bool keep_all(const commodity_t& comm) const;

  bool keep_any() const {
    return keep_price || keep_date || keep_tag;
  }
  bool keep_any(const commodity_t& comm) const;

#if defined(HAVE_BOOST_SERIALIZATION)
private:
  /** Serialization. */

  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive& ar, const unsigned int /* version */) {
    ar & keep_price;
    ar & keep_date;
    ar & keep_tag;
    ar & only_actuals;
  }
#endif // HAVE_BOOST_SERIALIZATION
};

inline std::ostream& operator<<(std::ostream&       out,
                                const annotation_t& details) {
  details.print(out);
  return out;
}

class annotated_commodity_t
  : public commodity_t,
    public equality_comparable<annotated_commodity_t,
           equality_comparable2<annotated_commodity_t, commodity_t,
                                noncopyable> >
{
protected:
  friend class commodity_pool_t;

  commodity_t * ptr;

  explicit annotated_commodity_t(commodity_t * _ptr,
                                 const annotation_t& _details)
    : commodity_t(_ptr->parent_, _ptr->base), ptr(_ptr), details(_details) {
    annotated = true;
    qualified_symbol = _ptr->qualified_symbol;
    TRACE_CTOR(annotated_commodity_t, "commodity_t *, annotation_t");
  }

public:
  annotation_t  details;

  virtual ~annotated_commodity_t() {
    TRACE_DTOR(annotated_commodity_t);
  }

  virtual bool operator==(const commodity_t& comm) const;
  virtual bool operator==(const annotated_commodity_t& comm) const {
    return *this == static_cast<const commodity_t&>(comm);
  }

  virtual commodity_t& referent() {
    return *ptr;
  }
  virtual const commodity_t& referent() const {
    return *ptr;
  }

  virtual optional<expr_t> value_expr() const {
    if (details.value_expr)
      return details.value_expr;
    return commodity_t::value_expr();
  }

  optional<price_point_t>
  virtual find_price(const commodity_t * commodity = NULL,
                     const datetime_t&   moment    = datetime_t(),
                     const datetime_t&   oldest    = datetime_t()) const;

  virtual commodity_t& strip_annotations(const keep_details_t& what_to_keep);

  virtual void print(std::ostream& out, bool elide_quotes = false,
                     bool print_annotations = false) const {
    if (print_annotations) {
      std::ostringstream buf;
      commodity_t::print(buf, elide_quotes);
      write_annotations(buf);
      out << buf.str();
    } else {
      commodity_t::print(out, elide_quotes);
    }
  }

  virtual void write_annotations(std::ostream& out,
                                 bool no_computed_annotations = false) const;

#if defined(HAVE_BOOST_SERIALIZATION)
private:
  explicit annotated_commodity_t() : ptr(NULL) {
    TRACE_CTOR(annotated_commodity_t, "");
  }

  /** Serialization. */

  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive& ar, const unsigned int /* version */) {
    ar & boost::serialization::base_object<commodity_t>(*this);
    ar & ptr;
    ar & details;
  }
#endif // HAVE_BOOST_SERIALIZATION
};

inline annotated_commodity_t&
as_annotated_commodity(commodity_t& commodity) {
  return downcast<annotated_commodity_t>(commodity);
}
inline const annotated_commodity_t&
as_annotated_commodity(const commodity_t& commodity) {
  return downcast<const annotated_commodity_t>(commodity);
}

} // namespace ledger

#endif // _ANNOTATE_H