summaryrefslogtreecommitdiff
path: root/src/numerics/value.h
blob: a70bf33ecc92c596b09233ba1d2fe303b3079508 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 * Copyright (c) 2003-2007, 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.
 */

#ifndef _VALUE_H
#define _VALUE_H

#include "balpair.h"		// pulls in balance.h and amount.h

namespace ledger {

namespace xml {
  class node_t;
}

// The following type is a polymorphous value type used solely for
// performance reasons.  The alternative is to compute value
// expressions (valexpr.cc) in terms of the largest data type,
// balance_t. This was found to be prohibitively expensive, especially
// when large logic chains were involved, since many temporary
// allocations would occur for every operator.  With value_t, and the
// fact that logic chains only need boolean values to continue, no
// memory allocations need to take place at all.

class value_t
  : public ordered_field_operators<value_t,
	   ordered_field_operators<value_t, balance_pair_t,
	   ordered_field_operators<value_t, balance_t,
	   ordered_field_operators<value_t, amount_t,
	   ordered_field_operators<value_t, double,
	   ordered_field_operators<value_t, unsigned long,
	   ordered_field_operators<value_t, long> > > > > > >
{
public:
  typedef std::vector<value_t> sequence_t;

  typedef sequence_t::iterator	      iterator;
  typedef sequence_t::const_iterator  const_iterator;
  typedef sequence_t::difference_type difference_type;

  enum type_t {
    VOID,
    BOOLEAN,
    DATETIME,
    INTEGER,
    AMOUNT,
    BALANCE,
    BALANCE_PAIR,
    STRING,
    SEQUENCE,
    XML_NODE,
    POINTER
  };

private:
  class storage_t
  {
    char   data[sizeof(amount_t)];
    type_t type;

    explicit storage_t() : type(VOID), refc(0) {
      TRACE_CTOR(value_t::storage_t, "");
    }
    explicit storage_t(const storage_t& rhs)
      : type(rhs.type), refc(0) {
      TRACE_CTOR(value_t::storage_t, "");
      std::memcpy(data, rhs.data, sizeof(data));
    }

  public:			// so `checked_delete' can access it
    ~storage_t() {
      TRACE_DTOR(value_t::storage_t);
      DEBUG("value.storage.refcount", "Destroying " << this);
      assert(refc == 0);
      destroy();
    }

  private:
    storage_t& operator=(const storage_t& rhs) {
      type = rhs.type;
      std::memcpy(data, rhs.data, sizeof(data));
      return *this;
    }

    mutable int refc;

    void acquire() const {
      DEBUG("value.storage.refcount",
	     "Acquiring " << this << ", refc now " << refc + 1);
      assert(refc >= 0);
      refc++;
    }
    void release() const {
      DEBUG("value.storage.refcount",
	     "Releasing " << this << ", refc now " << refc - 1);
      assert(refc > 0);
      if (--refc == 0)
	checked_delete(this);
    }

    void destroy();

    friend class value_t;

    friend inline void intrusive_ptr_add_ref(value_t::storage_t * storage) {
      storage->acquire();
    }
    friend inline void intrusive_ptr_release(value_t::storage_t * storage) {
      storage->release();
    }
  };

  intrusive_ptr<storage_t> storage;

  static intrusive_ptr<storage_t> true_value;
  static intrusive_ptr<storage_t> false_value;

  // jww (2007-05-03): Make this private, and then make
  // ledger::initialize into a member function of session_t.
public:
  static void initialize();
  static void shutdown();

public:
  value_t() {
    TRACE_CTOR(value_t, "");
  }

  value_t(const value_t& val) {
    TRACE_CTOR(value_t, "copy");
    *this = val;
  }
  value_t(const bool val) {
    TRACE_CTOR(value_t, "const bool");
    set_boolean(val);
  }
  value_t(const long val) {
    TRACE_CTOR(value_t, "const long");
    set_long(val);
  }
  value_t(const moment_t val) {
    TRACE_CTOR(value_t, "const moment_t");
    set_datetime(val);
  }
  value_t(const double val) {
    TRACE_CTOR(value_t, "const double");
    set_amount(val);
  }
  value_t(const unsigned long val) {
    TRACE_CTOR(value_t, "const unsigned long");
    set_amount(val);
  }
  explicit value_t(const string& val, bool literal = false) {
    TRACE_CTOR(value_t, "const string&, bool");
    if (literal)
      set_string(val);
    else
      set_amount(amount_t(val));
  }
  explicit value_t(const char * val, bool literal = false) {
    TRACE_CTOR(value_t, "const char *");
    if (literal)
      set_string(val);
    else
      set_amount(amount_t(val));
  }
  value_t(const amount_t& val) {
    TRACE_CTOR(value_t, "const amount_t&");
    set_amount(val);
  }
  value_t(const balance_t& val) {
    TRACE_CTOR(value_t, "const balance_t&");
    set_balance(val);
  }
  value_t(const balance_pair_t& val) {
    TRACE_CTOR(value_t, "const balance_pair_t&");
    set_balance_pair(val);
  }
  value_t(const sequence_t& val) {
    TRACE_CTOR(value_t, "const sequence_t&");
    set_sequence(val);
  }
  value_t(xml::node_t * item) {
    TRACE_CTOR(value_t, "xml::node_t *");
    set_xml_node(item);
  }
  template <typename T>
  explicit value_t(T * item) {
    TRACE_CTOR(value_t, "T *");
    set_pointer(item);
  }
  ~value_t() {
    TRACE_DTOR(value_t);
  }

  value_t& operator=(const value_t& val) {
    if (! (this == &val || storage == val.storage))
      storage = val.storage;
    return *this;
  }

  /**
   * _dup() makes a private copy of the current value so that it can
   * subsequently be modified.
   *
   * _clear() removes our pointer to the current value and initializes
   * a new value for things to be stored in.
   */
  void _dup() {
    assert(storage);
    if (storage->refc > 1) {
      storage = new storage_t(*storage.get());

      // If the data referenced by storage is an allocated pointer, we
      // need to create a new object in order to achieve duplication.
      switch (storage->type) {
      case BALANCE:
	*(balance_t **) storage->data =
	  new balance_t(**(balance_t **) storage->data);
	break;
      case BALANCE_PAIR:
	*(balance_pair_t **) storage->data =
	  new balance_pair_t(**(balance_pair_t **) storage->data);
	break;
      case SEQUENCE:
	*(sequence_t **) storage->data =
	  new sequence_t(**(sequence_t **) storage->data);
	break;
      default:
	break;			// everything else has been duplicated
      }
    }
  }
  void _clear() {
    if (! storage || storage->refc > 1)
      storage = new storage_t;
    else
      storage->destroy();
  }
  void _reset() {
    if (storage)
      storage = intrusive_ptr<storage_t>();
  }

  operator bool() const;

  bool is_null() const {
    if (! storage) {
      return true;
    } else {
      assert(! is_type(VOID));
      return false;
    }
  }
  type_t type() const {
    type_t result = storage ? storage->type : VOID;
    assert(result >= VOID && result <= POINTER);
    return result;
  }

private:
  bool is_type(type_t _type) const {
    return type() == _type;
  }
  void set_type(type_t new_type) {
    assert(new_type >= VOID && new_type <= POINTER);
    if (new_type == VOID) {
      _reset();
      assert(is_null());
    } else {
      _clear();
      storage->type = new_type;
      assert(is_type(new_type));
    }
  }

public:
  bool is_boolean() const {
    return is_type(BOOLEAN);
  }
  bool& as_boolean_lval() {
    assert(is_boolean());
    _dup();
    return *(bool *) storage->data;
  }
  const bool& as_boolean() const {
    assert(is_boolean());
    return *(bool *) storage->data;
  }
  void set_boolean(const bool val) {
    set_type(BOOLEAN);
    storage = val ? true_value : false_value;
  }

  bool is_long() const {
    return is_type(INTEGER);
  }
  long& as_long_lval() {
    assert(is_long());
    _dup();
    return *(long *) storage->data;
  }
  const long& as_long() const {
    assert(is_long());
    return *(long *) storage->data;
  }
  void set_long(const long val) {
    set_type(INTEGER);
    *(long *) storage->data = val;
  }

  bool is_datetime() const {
    return is_type(DATETIME);
  }
  moment_t& as_datetime_lval() {
    assert(is_datetime());
    _dup();
    return *(moment_t *) storage->data;
  }
  const moment_t& as_datetime() const {
    assert(is_datetime());
    return *(moment_t *) storage->data;
  }
  void set_datetime(const moment_t& val) {
    set_type(DATETIME);
    new((moment_t *) storage->data) moment_t(val);
  }

  bool is_amount() const {
    return is_type(AMOUNT);
  }
  amount_t& as_amount_lval() {
    assert(is_amount());
    _dup();
    return *(amount_t *) storage->data;
  }
  const amount_t& as_amount() const {
    assert(is_amount());
    return *(amount_t *) storage->data;
  }
  void set_amount(const amount_t& val) {
    set_type(AMOUNT);
    new((amount_t *) storage->data) amount_t(val);
  }

  bool is_balance() const {
    return is_type(BALANCE);
  }
  balance_t& as_balance_lval() {
    assert(is_balance());
    _dup();
    return **(balance_t **) storage->data;
  }
  const balance_t& as_balance() const {
    assert(is_balance());
    return **(balance_t **) storage->data;
  }
  void set_balance(const balance_t& val) {
    set_type(BALANCE);
    *(balance_t **) storage->data = new balance_t(val);
  }

  bool is_balance_pair() const {
    return is_type(BALANCE_PAIR);
  }
  balance_pair_t& as_balance_pair_lval() {
    assert(is_balance_pair());
    _dup();
    return **(balance_pair_t **) storage->data;
  }
  const balance_pair_t& as_balance_pair() const {
    assert(is_balance_pair());
    return **(balance_pair_t **) storage->data;
  }
  void set_balance_pair(const balance_pair_t& val) {
    set_type(BALANCE_PAIR);
    *(balance_pair_t **) storage->data = new balance_pair_t(val);
  }

  bool is_string() const {
    return is_type(STRING);
  }
  string& as_string_lval() {
    assert(is_string());
    _dup();
    return *(string *) storage->data;
  }
  const string& as_string() const {
    assert(is_string());
    return *(string *) storage->data;
  }
  void set_string(const string& val = "") {
    set_type(STRING);
    new((string *) storage->data) string(val);
  }

  bool is_sequence() const {
    return is_type(SEQUENCE);
  }
  sequence_t& as_sequence_lval() {
    assert(is_sequence());
    _dup();
    return **(sequence_t **) storage->data;
  }
  const sequence_t& as_sequence() const {
    assert(is_sequence());
    return **(sequence_t **) storage->data;
  }
  void set_sequence(const sequence_t& val) {
    set_type(SEQUENCE);
    *(sequence_t **) storage->data = new sequence_t(val);
  }

  bool is_xml_node() const {
    return is_type(XML_NODE);
  }
  xml::node_t *& as_xml_node_lval() {
    assert(is_xml_node());
    _dup();
    return *(xml::node_t **) storage->data;
  }
  xml::node_t * as_xml_node() const {
    assert(is_xml_node());
    return *(xml::node_t **) storage->data;
  }
  void set_xml_node(xml::node_t * val) {
    set_type(XML_NODE);
    *(xml::node_t **) storage->data = val;
  }

  bool is_pointer() const {
    return is_type(POINTER);
  }
  boost::any& as_any_pointer_lval() {
    assert(is_pointer());
    _dup();
    return *(boost::any *) storage->data;
  }
  template <typename T>
  T *& as_pointer_lval() {
    assert(is_pointer());
    _dup();
    return any_cast<T *>(*(boost::any *) storage->data);
  }
  template <typename T>
  T& as_ref_lval() {
    assert(is_pointer());
    _dup();
    return *any_cast<T *>(*(boost::any *) storage->data);
  }
  boost::any as_any_pointer() const {
    assert(is_pointer());
    return *(boost::any *) storage->data;
  }
  template <typename T>
  T * as_pointer() const {
    assert(is_pointer());
    return any_cast<T *>(*(boost::any *) storage->data);
  }
  template <typename T>
  T& as_ref() const {
    assert(is_pointer());
    return *any_cast<T *>(*(boost::any *) storage->data);
  }
  void set_any_pointer(const boost::any& val) {
    set_type(POINTER);
    new((boost::any *) storage->data) boost::any(val);
  }
  template <typename T>
  void set_pointer(T * val) {
    set_type(POINTER);
    new((boost::any *) storage->data) boost::any(val);
  }

  bool		 to_boolean() const;
  long		 to_long() const;
  moment_t       to_datetime() const;
  amount_t	 to_amount() const;
  balance_t	 to_balance() const;
  balance_pair_t to_balance_pair() const;
  string	 to_string() const;
  sequence_t     to_sequence() const;

  value_t simplify() const {
    value_t temp = *this;
    temp.in_place_simplify();
    return temp;
  }
  void in_place_simplify();

  value_t& operator[](const int index) {
    assert(! is_null());
    if (is_sequence())
      return as_sequence_lval()[index];
    else if (index == 0)
      return *this;

    assert(false);
    static value_t null;
    return null;
  }
  const value_t& operator[](const int index) const {
    assert(! is_null());
    if (is_sequence())
      return as_sequence()[index];
    else if (index == 0)
      return *this;

    assert(false);
    static value_t null;
    return null;
  }

  void push_back(const value_t& val) {
    if (! val.is_null()) {
      if (is_null()) {
	*this = val;
      } else {
	if (! is_sequence())
	  in_place_cast(SEQUENCE);

	value_t::sequence_t& seq(as_sequence_lval());
	if (! val.is_sequence()) {
	  if (! val.is_null())
	    seq.push_back(val);
	} else {
	  const value_t::sequence_t& val_seq(val.as_sequence());
	  std::copy(val_seq.begin(), val_seq.end(), back_inserter(seq));
	}
      }
    }
  }

  void pop_back() {
    assert(! is_null());

    if (! is_sequence()) {
      _reset();
    } else {
      as_sequence_lval().pop_back();

      std::size_t new_size = as_sequence().size();
      if (new_size == 0)
	_reset();
      else if (new_size == 1)
	*this = as_sequence().front();
    }
  }

  const std::size_t size() const {
    if (is_null())
      return 0;
    else if (is_sequence())
      return as_sequence().size();
    else
      return 1;
  }

  value_t& operator+=(const value_t& val);
  value_t& operator-=(const value_t& val);
  value_t& operator*=(const value_t& val);
  value_t& operator/=(const value_t& val);

  bool operator==(const value_t& val) const;
  bool operator<(const value_t& val) const;
#if 0
  bool operator>(const value_t& val) const;
#endif

  string label(optional<type_t> the_type = none) const {
    switch (the_type ? *the_type : type()) {
    case VOID:
      return "an uninitialized value";
    case BOOLEAN:
      return "a boolean";
    case INTEGER:
      return "an integer";
    case DATETIME:
      return "a date/time";
    case AMOUNT:
      return "an amount";
    case BALANCE:
      return "a balance";
    case BALANCE_PAIR:
      return "a balance pair";
    case STRING:
      return "a string";
    case SEQUENCE:
      return "a sequence";
    case XML_NODE:
      return "an xml node";
    case POINTER:
      return "a pointer";
    default:
      assert(false);
      break;
    }
    assert(false);
    return "<invalid>";
  }

  value_t operator-() const {
    return negate();
  }
  value_t negate() const {
    value_t temp = *this;
    temp.in_place_negate();
    return temp;
  }
  void in_place_negate();

  bool    is_realzero() const;
  value_t abs() const;
  void    in_place_cast(type_t cast_type);
  value_t cost() const;
  value_t annotated_price() const;
  value_t annotated_date() const;
  value_t annotated_tag() const;

  value_t cast(type_t cast_type) const {
    value_t temp(*this);
    temp.in_place_cast(cast_type);
    return temp;
  }

  value_t strip_annotations(const bool keep_price = amount_t::keep_price,
			    const bool keep_date  = amount_t::keep_date,
			    const bool keep_tag   = amount_t::keep_tag) const;

  value_t& add(const amount_t& amount,
	       const optional<amount_t>& cost = none);
  value_t  value(const optional<moment_t>& moment = none) const;

  void    in_place_reduce();
  value_t reduce() const {
    value_t temp(*this);
    temp.in_place_reduce();
    return temp;
  }

  value_t round() const;
  value_t unround() const;

  void print(std::ostream& out, const int first_width,
	     const int latter_width = -1) const;

  friend std::ostream& operator<<(std::ostream& out, const value_t& val);
};

#define NULL_VALUE (value_t())

inline value_t string_value(const string& str) {
  return value_t(str, true);
}

std::ostream& operator<<(std::ostream& out, const value_t& val);

DECLARE_EXCEPTION(value_error);

} // namespace ledger

#endif // _VALUE_H